import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { IRouteData } from '@typings/common/route';
import { MenuItem } from 'primeng/api';

export type IBreadCrumbItem = Required<Pick<MenuItem, 'label' | 'url'>>;
const defaultMenuItem: IBreadCrumbItem = {
    label: 'SHARED.MAIN',
    url: 'https://www.aeroflot.ru',
};

@Component({
    selector: 'flights-page-breadcrumbs',
    templateUrl: './page-breadcrumbs.component.html',
})
export class PageBreadcrumbsComponent implements OnInit {
    items: IBreadCrumbItem[] = [];

    constructor(
        private translation: TranslateService,
        private route: ActivatedRoute,
    ) {}

    ngOnInit() {
        this.route.data.subscribe((data: IRouteData) => {
            let items = [defaultMenuItem];
            if (data.breadcrumbs) {
                items = items.concat(data.breadcrumbs);
            }

            this.items = this.translate(items);
        });
    }

    private translate(items: IBreadCrumbItem[]) {
        return items.map((item) => ({
            ...item,
            label: this.translation.instant(item.label) || item.label,
        }));
    }
}
