import * as i0 from '@angular/core';
import { Injectable, EventEmitter, Directive, Optional, Output, Input } from '@angular/core';
import * as i1 from '@ironsource/fusion-ui/services/window';
import * as i3 from '@angular/common';

class MenuService {
    constructor(windowService) {
        this.windowService = windowService;
    }
    /**
     * Set menu items collection from JSON data
     */
    setMenuItems(menuItems, currentPath = '') {
        this.menuItems = menuItems || [];
        this.setSelectedByRoute(currentPath);
    }
    setSelectedByRoute(route) {
        if (this.menuItems.length !== 0) {
            const findMenuItem = (items, path) => {
                let itemFound = null;
                items.forEach((curItem) => {
                    if (!itemFound) {
                        if (path.startsWith(curItem.route) || this.checkBySubRoutes(curItem, path) || path.startsWith(curItem.redirect)) {
                            itemFound = curItem;
                        }
                        if (curItem.children && !itemFound) {
                            itemFound = findMenuItem(curItem.children, path);
                        }
                    }
                });
                return itemFound;
            };
            // check menu item to be selected by active route (location)
            const item = findMenuItem(this.menuItems, route);
            if (item) {
                this.setSelected(item);
            }
            else {
                this.selectedItem = null;
            }
        }
    }
    checkBySubRoutes(item, route) {
        if (Array.isArray(item.subRoutes)) {
            return item.subRoutes.some(subRoute => route.startsWith(subRoute));
        }
        return false;
    }
    /**
     * return menu items collection to the menu controller
     * here we can use some manipulation if needed
     */
    getMenuItems() {
        return this.menuItems;
    }
    /**
     * Just set menu item that was clicked to selected
     */
    setSelected(menuItem) {
        this.selectedItem = menuItem;
    }
    /**
     * Check if item or one of it's child selected
     */
    isSelected(menuItem) {
        if (menuItem === this.selectedItem) {
            return true; // current item selected
        }
        // need check child if any selected
        if (menuItem.children) {
            return menuItem.children.some(item => item === this.selectedItem);
        }
        return false;
    }
    /**
     * Check if menu item need to be opened (selected and has child)
     */
    isOpened(menuItem) {
        // same as is Selected
        return this.isSelected(menuItem);
        // return (!!menuItem.children && (menuItem === this.selectedItem));
    }
    /**
     * Store pre-selected menu for using in platform (DSI 1.5)
     */
    setPlatformMenuPath(menuPath) {
        if (menuPath) {
            this.windowService.nativeWindow.sessionStorage.setItem('menuSelected', menuPath);
        }
    }
    static { this.ɵfac = function MenuService_Factory(t) { return new (t || MenuService)(i0.ɵɵinject(i1.WindowService)); }; }
    static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: MenuService, factory: MenuService.ɵfac, providedIn: 'root' }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(MenuService, [{
        type: Injectable,
        args: [{
                providedIn: 'root'
            }]
    }], () => [{ type: i1.WindowService }], null); })();

class MenuBaseComponent {
    set menuItems(menuItems) {
        this._menuItems = menuItems;
        this.menuService.setMenuItems(this._menuItems, this.currentPath);
    }
    get menuItems() {
        return this._menuItems;
    }
    get currentPath() {
        if (this.location) {
            return this.location.path();
        }
        else {
            return this.windowService.nativeWindow.location.pathname;
        }
    }
    constructor(menuService, windowService, location) {
        this.menuService = menuService;
        this.windowService = windowService;
        this.location = location;
        this.routeChanged = new EventEmitter();
        this.menuAdditionalItemClicked = new EventEmitter();
        this.menuItemClicked = new EventEmitter();
        this.mouseLeave = new EventEmitter();
        this.mouseEnter = new EventEmitter();
    }
    static { this.ɵfac = function MenuBaseComponent_Factory(t) { return new (t || MenuBaseComponent)(i0.ɵɵdirectiveInject(MenuService), i0.ɵɵdirectiveInject(i1.WindowService), i0.ɵɵdirectiveInject(i3.Location, 8)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: MenuBaseComponent, inputs: { menuItems: "menuItems" }, outputs: { routeChanged: "routeChanged", menuAdditionalItemClicked: "menuAdditionalItemClicked", menuItemClicked: "menuItemClicked", mouseLeave: "mouseLeave", mouseEnter: "mouseEnter" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(MenuBaseComponent, [{
        type: Directive
    }], () => [{ type: MenuService }, { type: i1.WindowService }, { type: i3.Location, decorators: [{
                type: Optional
            }] }], { routeChanged: [{
            type: Output
        }], menuItems: [{
            type: Input
        }], menuAdditionalItemClicked: [{
            type: Output
        }], menuItemClicked: [{
            type: Output
        }], mouseLeave: [{
            type: Output
        }], mouseEnter: [{
            type: Output
        }] }); })();

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

export { MenuBaseComponent, MenuService };
//# sourceMappingURL=ironsource-fusion-ui-components-menu-common-base.mjs.map
