import * as i0 from '@angular/core';
import { Component, Attribute, Inject, Optional, NgModule } from '@angular/core';
import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i4 from '@wm/components/base';
import { PROP_STRING, PROP_ANY, PROP_BOOLEAN, register, DatasetAwareNavComponent, APPLY_STYLES_TYPE, styler, provideAsWidgetRef, WmComponentsModule } from '@wm/components/base';
import * as i3 from '@wm/components/navigation/menu';
import { MenuModule } from '@wm/components/navigation/menu';
import * as i1 from '@angular/router';
import { getUrlParams, getRouteNameFromLink, openLink } from '@wm/core';

const registerProps = () => {
    register('wm-breadcrumb', new Map([
        ['class', PROP_STRING],
        ['dataset', PROP_ANY],
        ['itemlabel', PROP_STRING],
        ['itemid', PROP_STRING],
        ['itemlink', PROP_STRING],
        ['itemicon', PROP_STRING],
        ['itemchildren', PROP_STRING],
        ['itemclass', PROP_STRING],
        ['itemtarget', PROP_STRING],
        ['name', PROP_STRING],
        ['show', { value: true, ...PROP_BOOLEAN }]
    ]));
};

const _c0 = ["wmBreadcrumb", ""];
const _c1 = a0 => ({ active: a0 });
function BreadcrumbComponent_li_0_a_2_Template(rf, ctx) { if (rf & 1) {
    const _r1 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "a", 4);
    i0.ɵɵlistener("click", function BreadcrumbComponent_li_0_a_2_Template_a_click_0_listener($event) { i0.ɵɵrestoreView(_r1); const item_r2 = i0.ɵɵnextContext().$implicit; const ctx_r2 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r2.onItemClick($event, item_r2)); });
    i0.ɵɵelementEnd();
} if (rf & 2) {
    const item_r2 = i0.ɵɵnextContext().$implicit;
    const ctx_r2 = i0.ɵɵnextContext();
    i0.ɵɵproperty("title", item_r2.label)("wmNavigationControl", item_r2.link)("disableMenuContext", ctx_r2.disableMenuContext || !!item_r2.action)("textContent", item_r2.label);
} }
function BreadcrumbComponent_li_0_a_3_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelement(0, "a", 5);
} if (rf & 2) {
    const item_r2 = i0.ɵɵnextContext().$implicit;
    i0.ɵɵproperty("title", item_r2.label)("textContent", item_r2.label);
} }
function BreadcrumbComponent_li_0_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "li", 1);
    i0.ɵɵelement(1, "i");
    i0.ɵɵtemplate(2, BreadcrumbComponent_li_0_a_2_Template, 1, 4, "a", 2)(3, BreadcrumbComponent_li_0_a_3_Template, 1, 2, "a", 3);
    i0.ɵɵelementEnd();
} if (rf & 2) {
    const item_r2 = ctx.$implicit;
    const last_r4 = ctx.last;
    i0.ɵɵclassMap(item_r2.class);
    i0.ɵɵproperty("ngClass", i0.ɵɵpureFunction1(9, _c1, last_r4));
    i0.ɵɵadvance();
    i0.ɵɵclassMap(item_r2.icon);
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngIf", !last_r4);
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngIf", last_r4);
} }
const DEFAULT_CLS = 'breadcrumb app-breadcrumb';
const WIDGET_CONFIG = { widgetType: 'wm-breadcrumb', hostClass: DEFAULT_CLS };
class BreadcrumbComponent extends DatasetAwareNavComponent {
    static { this.initializeProps = registerProps(); }
    constructor(inj, route, location, beforeNavigateCB, explicitContext) {
        super(inj, WIDGET_CONFIG, explicitContext);
        this.route = route;
        this.location = location;
        styler(this.nativeElement, this, APPLY_STYLES_TYPE.CONTAINER);
        this.disableMenuContext = !!beforeNavigateCB;
    }
    /**
     * Gets the first path found based on the key provided inside info Object.
     * @param info - Info object which has properties key(Active Page Name) and isPathFound[boolean] is set true if path found.
     * @param children - a child Object form children Array.
     * @param path - final path.
     * @returns {*|Array}: returns array of objects which represents the final path.
     */
    getPath(info, children, path = []) {
        children.forEach((child) => {
            // return if path already found.
            if (info.isPathFound) {
                return path;
            }
            // if key is matched set path found to true and return the path.
            if (child.id === info.key) {
                info.isPathFound = true;
                // only push the child object by omiting the children within it.
                path.push(child);
                return path;
            }
            // if the node has children make a recursive call.
            if (child.children.length) {
                path.push(child);
                this.getPath(info, child.children, path);
                // if path is not found in that node pop the node.
                if (!info.isPathFound) {
                    path.pop();
                }
            }
        });
        // return the path.
        return path;
    }
    getCurrentRoute() {
        return this.location.path().substr(1).split('?')[0];
    }
    // over rides resetNode function, generating path for the breadcrumb.
    resetNodes() {
        super.resetNodes();
        // get path only if the widget have id property.
        if (this.itemid || this.binditemid) {
            this.nodes = this.getPath({ key: this.getCurrentRoute(), isPathFound: false }, this.nodes);
        }
    }
    onItemClick($event, $item) {
        $event.preventDefault();
        const locals = { $item: $item.value, $event };
        const canNavigate = !(this.invokeEventCallback('beforenavigate', locals) === false);
        const linkTarget = $item.target;
        let itemLink = $item.link;
        if (itemLink && canNavigate) {
            if (itemLink.startsWith('#/') && (!linkTarget || linkTarget === '_self')) {
                const queryParams = getUrlParams(itemLink);
                itemLink = getRouteNameFromLink(itemLink);
                this.route.navigate([itemLink], { queryParams });
            }
            else {
                openLink(itemLink, linkTarget);
            }
        }
    }
    static { this.ɵfac = function BreadcrumbComponent_Factory(t) { return new (t || BreadcrumbComponent)(i0.ɵɵdirectiveInject(i0.Injector), i0.ɵɵdirectiveInject(i1.Router), i0.ɵɵdirectiveInject(i2.Location), i0.ɵɵinjectAttribute('beforenavigate.event'), i0.ɵɵdirectiveInject('EXPLICIT_CONTEXT', 8)); }; }
    static { this.ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: BreadcrumbComponent, selectors: [["", "wmBreadcrumb", ""]], features: [i0.ɵɵProvidersFeature([
                provideAsWidgetRef(BreadcrumbComponent)
            ]), i0.ɵɵInheritDefinitionFeature], attrs: _c0, decls: 1, vars: 1, consts: [[3, "ngClass", "class", 4, "ngFor", "ngForOf"], [3, "ngClass"], ["href", "javascript:void(0)", 3, "title", "wmNavigationControl", "disableMenuContext", "textContent", "click", 4, "ngIf"], ["href", "javascript:void(0)", "class", "text-muted", "tabindex", "0", "aria-current", "page", 3, "title", "textContent", 4, "ngIf"], ["href", "javascript:void(0)", 3, "click", "title", "wmNavigationControl", "disableMenuContext", "textContent"], ["href", "javascript:void(0)", "tabindex", "0", "aria-current", "page", 1, "text-muted", 3, "title", "textContent"]], template: function BreadcrumbComponent_Template(rf, ctx) { if (rf & 1) {
            i0.ɵɵtemplate(0, BreadcrumbComponent_li_0_Template, 4, 11, "li", 0);
        } if (rf & 2) {
            i0.ɵɵproperty("ngForOf", ctx.nodes);
        } }, dependencies: [i2.NgClass, i2.NgForOf, i2.NgIf, i3.NavigationControlDirective, i4.TextContentDirective], encapsulation: 2 }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(BreadcrumbComponent, [{
        type: Component,
        args: [{ selector: '[wmBreadcrumb]', providers: [
                    provideAsWidgetRef(BreadcrumbComponent)
                ], template: "<li *ngFor=\"let item of nodes; let last = last; let index = index\" [ngClass]=\"{active: last}\" class=\"{{item.class}}\">\n    <i class=\"{{item.icon}}\"></i>\n    <a [title]=\"item.label\" href=\"javascript:void(0)\" [wmNavigationControl]=\"item.link\" [disableMenuContext]=\"disableMenuContext || !!item.action\" (click)=\"onItemClick($event, item)\" *ngIf=\"!last\" [textContent]=\"item.label\"></a>\n    <a *ngIf=\"last\" [title]=\"item.label\" [textContent]=\"item.label\" href=\"javascript:void(0)\" class=\"text-muted\"\n       tabindex=\"0\" aria-current=\"page\"></a>\n</li>\n" }]
    }], () => [{ type: i0.Injector }, { type: i1.Router }, { type: i2.Location }, { type: undefined, decorators: [{
                type: Attribute,
                args: ['beforenavigate.event']
            }] }, { type: undefined, decorators: [{
                type: Inject,
                args: ['EXPLICIT_CONTEXT']
            }, {
                type: Optional
            }] }], null); })();
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(BreadcrumbComponent, { className: "BreadcrumbComponent", filePath: "breadcrumb.component.ts", lineNumber: 20 }); })();

const components = [
    BreadcrumbComponent
];
class BreadcrumbModule {
    static { this.ɵfac = function BreadcrumbModule_Factory(t) { return new (t || BreadcrumbModule)(); }; }
    static { this.ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: BreadcrumbModule }); }
    static { this.ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({ imports: [CommonModule,
            MenuModule,
            WmComponentsModule] }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(BreadcrumbModule, [{
        type: NgModule,
        args: [{
                imports: [
                    CommonModule,
                    MenuModule,
                    WmComponentsModule
                ],
                declarations: [...components],
                exports: [...components]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(BreadcrumbModule, { declarations: [BreadcrumbComponent], imports: [CommonModule,
        MenuModule,
        WmComponentsModule], exports: [BreadcrumbComponent] }); })();

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

export { BreadcrumbComponent, BreadcrumbModule };
//# sourceMappingURL=index.mjs.map
