import * as i0 from '@angular/core';
import { Component, Input, NgModule } from '@angular/core';
import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i3 from 'primeng/breadcrumb';
import { BreadcrumbModule as BreadcrumbModule$1 } from 'primeng/breadcrumb';
import * as i1 from '@angular/router';
import { NavigationEnd, PRIMARY_OUTLET, RouterModule } from '@angular/router';
import { Subject } from 'rxjs';
import { takeUntil, filter } from 'rxjs/operators';

class BreadcrumbComponent {
    _activatedRoute;
    _router;
    homeUrl;
    targetHomeUrl = '_self';
    home;
    items;
    title;
    _ngUnsubscribe = new Subject();
    constructor(_activatedRoute, _router) {
        this._activatedRoute = _activatedRoute;
        this._router = _router;
        this._router.events
            .pipe(takeUntil(this._ngUnsubscribe), filter((event) => event instanceof NavigationEnd))
            .subscribe(() => this._onNavigationEnd());
    }
    ngOnDestroy() {
        this._ngUnsubscribe.next();
        this._ngUnsubscribe.complete();
    }
    _onNavigationEnd() {
        const currentRoute = this._activatedRoute.root;
        const validRoutes = this._getValidChildren(currentRoute);
        this.title = this._getCurrentTitle(validRoutes) ?? '';
        this.home = this._getCurrentHome(validRoutes);
        this.items = this._getCurrentItems(validRoutes) || [];
    }
    _getValidChildren(route) {
        const valid = route.routeConfig?.path && route.routeConfig?.component && route.outlet === PRIMARY_OUTLET;
        return route.children.reduce((validRoutes, child) => [...validRoutes, ...this._getValidChildren(child)], valid ? [route] : []);
    }
    _getCurrentTitle(routes) {
        const last = routes[routes.length - 1];
        if (last?.routeConfig) {
            return this._getTitle(last);
        }
        return null;
    }
    _getCurrentHome(routes) {
        let home;
        if (this.homeUrl) {
            home = { url: this.homeUrl, icon: 'fa fa-home', target: this.targetHomeUrl };
        }
        else if (routes.length) {
            home = { ...this._getMenuItem(routes[0], routes.length === 1), icon: 'fa fa-home' };
        }
        else {
            home = undefined;
        }
        return home;
    }
    _getCurrentItems(routes) {
        if (!routes.length) {
            return null;
        }
        const items = this.homeUrl ? routes : routes.slice(1);
        if (items.length) {
            return items.map((route, i) => this._getMenuItem(route, i === routes.length - 1));
        }
        else {
            return null;
        }
    }
    _getMenuItem(route, disabled = false) {
        const label = this._getTitle(route);
        const routes = route.pathFromRoot;
        const routerLink = routes
            .slice(0, routes.indexOf(route) + 1)
            .reduce((result, item) => [...result, ...item.snapshot.url.map((url) => url.path)], ['/']);
        return {
            label,
            routerLink,
            disabled,
            queryParams: route.snapshot.queryParams,
            routerLinkActiveOptions: { exact: true },
        };
    }
    _getTitle(route) {
        const { snapshot, routeConfig } = route;
        const title = snapshot.data['routeTitle'];
        if (!title) {
            throw new Error(`No routeTitle defined for the route /${routeConfig?.path}`);
        }
        return title;
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: BreadcrumbComponent, deps: [{ token: i1.ActivatedRoute }, { token: i1.Router }], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "18.2.14", type: BreadcrumbComponent, selector: "s-breadcrumb", inputs: { homeUrl: "homeUrl", targetHomeUrl: "targetHomeUrl" }, ngImport: i0, template: "<h1 class=\"sds-page-title\">{{ title }}</h1>\n<p-breadcrumb\n    [home]=\"home\"\n    [model]=\"items\"\n>\n    <ng-template\n        #item\n        let-menuitem\n    >\n        @if (menuitem.routerLink) {\n            <a\n                [routerLink]=\"menuitem?.routerLink\"\n                routerLinkActive=\"p-breadcrumb-item-link-active\"\n                [queryParams]=\"menuitem?.queryParams\"\n                [routerLinkActiveOptions]=\"menuitem?.routerLinkActiveOptions || { exact: false }\"\n                class=\"p-breadcrumb-item-link\"\n                [target]=\"menuitem?.target\"\n                [attr.title]=\"menuitem?.title\"\n                [attr.tabindex]=\"menuitem?.disabled ? null : '0'\"\n                [fragment]=\"menuitem?.fragment\"\n                [queryParamsHandling]=\"menuitem?.queryParamsHandling\"\n                [preserveFragment]=\"menuitem?.preserveFragment\"\n                [skipLocationChange]=\"menuitem?.skipLocationChange\"\n                [replaceUrl]=\"menuitem?.replaceUrl\"\n                [state]=\"menuitem?.state\"\n            >\n                @if (menuitem?.icon) {\n                    <span\n                        class=\"p-breadcrumb-item-icon\"\n                        [ngClass]=\"menuitem?.icon\"\n                        [ngStyle]=\"menuitem?.iconStyle\"\n                    ></span>\n                }\n                @if (menuitem?.label) {\n                    @if (menuitem?.escape !== false) {\n                        <span class=\"p-breadcrumb-item-label'\">{{ menuitem?.label }}</span>\n                    } @else {\n                        ><span\n                            class=\"p-breadcrumb-item-label'\"\n                            [innerHTML]=\"menuitem?.label\"\n                        ></span>\n                    }\n                }\n            </a>\n        } @else {\n            <a\n                [attr.href]=\"menuitem?.url ? menuitem?.url : null\"\n                class=\"p-breadcrumb-item-link\"\n                [target]=\"menuitem?.target\"\n                [attr.title]=\"menuitem?.title\"\n                [attr.tabindex]=\"menuitem?.disabled ? null : '0'\"\n            >\n                @if (menuitem?.icon) {\n                    <span\n                        class=\"p-breadcrumb-item-icon\"\n                        [ngClass]=\"menuitem?.icon\"\n                        [ngStyle]=\"menuitem?.iconStyle\"\n                    ></span>\n                }\n                @if (menuitem?.label) {\n                    @if (menuitem?.escape !== false) {\n                        <span class=\"p-breadcrumb-item-label'\">{{ menuitem?.label }}</span>\n                    } @else {\n                        ><span\n                            class=\"p-breadcrumb-item-label'\"\n                            [innerHTML]=\"menuitem?.label\"\n                        ></span>\n                    }\n                }\n            </a>\n        }\n    </ng-template>\n</p-breadcrumb>\n", dependencies: [{ kind: "directive", type: i2.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i2.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }, { kind: "component", type: i3.Breadcrumb, selector: "p-breadcrumb", inputs: ["model", "style", "styleClass", "home", "homeAriaLabel"], outputs: ["onItemClick"] }, { kind: "directive", type: i1.RouterLink, selector: "[routerLink]", inputs: ["target", "queryParams", "fragment", "queryParamsHandling", "state", "info", "relativeTo", "preserveFragment", "skipLocationChange", "replaceUrl", "routerLink"] }, { kind: "directive", type: i1.RouterLinkActive, selector: "[routerLinkActive]", inputs: ["routerLinkActiveOptions", "ariaCurrentWhenActive", "routerLinkActive"], outputs: ["isActiveChange"], exportAs: ["routerLinkActive"] }] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: BreadcrumbComponent, decorators: [{
            type: Component,
            args: [{ selector: 's-breadcrumb', template: "<h1 class=\"sds-page-title\">{{ title }}</h1>\n<p-breadcrumb\n    [home]=\"home\"\n    [model]=\"items\"\n>\n    <ng-template\n        #item\n        let-menuitem\n    >\n        @if (menuitem.routerLink) {\n            <a\n                [routerLink]=\"menuitem?.routerLink\"\n                routerLinkActive=\"p-breadcrumb-item-link-active\"\n                [queryParams]=\"menuitem?.queryParams\"\n                [routerLinkActiveOptions]=\"menuitem?.routerLinkActiveOptions || { exact: false }\"\n                class=\"p-breadcrumb-item-link\"\n                [target]=\"menuitem?.target\"\n                [attr.title]=\"menuitem?.title\"\n                [attr.tabindex]=\"menuitem?.disabled ? null : '0'\"\n                [fragment]=\"menuitem?.fragment\"\n                [queryParamsHandling]=\"menuitem?.queryParamsHandling\"\n                [preserveFragment]=\"menuitem?.preserveFragment\"\n                [skipLocationChange]=\"menuitem?.skipLocationChange\"\n                [replaceUrl]=\"menuitem?.replaceUrl\"\n                [state]=\"menuitem?.state\"\n            >\n                @if (menuitem?.icon) {\n                    <span\n                        class=\"p-breadcrumb-item-icon\"\n                        [ngClass]=\"menuitem?.icon\"\n                        [ngStyle]=\"menuitem?.iconStyle\"\n                    ></span>\n                }\n                @if (menuitem?.label) {\n                    @if (menuitem?.escape !== false) {\n                        <span class=\"p-breadcrumb-item-label'\">{{ menuitem?.label }}</span>\n                    } @else {\n                        ><span\n                            class=\"p-breadcrumb-item-label'\"\n                            [innerHTML]=\"menuitem?.label\"\n                        ></span>\n                    }\n                }\n            </a>\n        } @else {\n            <a\n                [attr.href]=\"menuitem?.url ? menuitem?.url : null\"\n                class=\"p-breadcrumb-item-link\"\n                [target]=\"menuitem?.target\"\n                [attr.title]=\"menuitem?.title\"\n                [attr.tabindex]=\"menuitem?.disabled ? null : '0'\"\n            >\n                @if (menuitem?.icon) {\n                    <span\n                        class=\"p-breadcrumb-item-icon\"\n                        [ngClass]=\"menuitem?.icon\"\n                        [ngStyle]=\"menuitem?.iconStyle\"\n                    ></span>\n                }\n                @if (menuitem?.label) {\n                    @if (menuitem?.escape !== false) {\n                        <span class=\"p-breadcrumb-item-label'\">{{ menuitem?.label }}</span>\n                    } @else {\n                        ><span\n                            class=\"p-breadcrumb-item-label'\"\n                            [innerHTML]=\"menuitem?.label\"\n                        ></span>\n                    }\n                }\n            </a>\n        }\n    </ng-template>\n</p-breadcrumb>\n" }]
        }], ctorParameters: () => [{ type: i1.ActivatedRoute }, { type: i1.Router }], propDecorators: { homeUrl: [{
                type: Input
            }], targetHomeUrl: [{
                type: Input
            }] } });

class BreadcrumbModule {
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: BreadcrumbModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
    static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.14", ngImport: i0, type: BreadcrumbModule, declarations: [BreadcrumbComponent], imports: [CommonModule, BreadcrumbModule$1, RouterModule], exports: [BreadcrumbComponent, RouterModule] });
    static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: BreadcrumbModule, imports: [CommonModule, BreadcrumbModule$1, RouterModule, RouterModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: BreadcrumbModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule, BreadcrumbModule$1, RouterModule],
                    declarations: [BreadcrumbComponent],
                    exports: [BreadcrumbComponent, RouterModule],
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { BreadcrumbComponent, BreadcrumbModule };
//# sourceMappingURL=seniorsistemas-angular-components-breadcrumb.mjs.map
