import * as i0 from '@angular/core';
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { isNullOrUndefined } from '@ironsource/fusion-ui/utils';

class SidebarMenuService {
    constructor() {
        this.openedMenuItem$ = new BehaviorSubject(null);
        this.selectedMenuItem$ = new BehaviorSubject(null);
        this.activeMenuItem$ = new BehaviorSubject(null);
    }
    normalizeActiveRoute(activeRoute, regExp) {
        return !isNullOrUndefined(regExp) ? activeRoute.replace(regExp, '') : activeRoute;
    }
    /**
     * Found in menu-items three open, selected and active menu item by route (ignore part after menu item route)
     * route '/XXX/YYY/ZZZ' will match for menuItem route '/XXX/YYY'
     * - menuItems menu items array
     * - activeRoute
     * - rootItem (first parent in three)
     * - deepLevel (optional need just for not replace root parent if level of parents more than one)
     */
    setMenuByActiveRoute(menuItems = [], activeRoute, rootItem, deepLevel = 0) {
        deepLevel++;
        menuItems.forEach(item => {
            if ((item.route && activeRoute.startsWith(item.route)) ||
                this.checkBySubRoutes(item, activeRoute) ||
                (item.additionalAction?.route && activeRoute.startsWith(item.additionalAction.route))) {
                this.setMenuItemsState(item, rootItem ?? item, rootItem ?? item);
                return false;
            }
            else if (!!item.children) {
                this.setMenuByActiveRoute(item.children, activeRoute, deepLevel === 1 ? item : rootItem, deepLevel);
            }
        });
    }
    checkBySubRoutes(item, route) {
        if (Array.isArray(item.subRoutes)) {
            return item.subRoutes.some(subRoute => route.startsWith(subRoute));
        }
        return false;
    }
    setMenuItemsState(active, selected, opened) {
        this.activeMenuItem$.next(active);
        this.selectedMenuItem$.next(selected);
        this.openedMenuItem$.next(opened);
    }
    static { this.ɵfac = function SidebarMenuService_Factory(t) { return new (t || SidebarMenuService)(); }; }
    static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: SidebarMenuService, factory: SidebarMenuService.ɵfac, providedIn: 'root' }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(SidebarMenuService, [{
        type: Injectable,
        args: [{
                providedIn: 'root'
            }]
    }], () => [], null); })();

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

export { SidebarMenuService };
//# sourceMappingURL=ironsource-fusion-ui-components-sidebar-common-services.mjs.map
