import * as i0 from '@angular/core';
import { EventEmitter, Directive, Input, Output, HostBinding } from '@angular/core';
import { BehaviorSubject, Subject, fromEvent, merge } from 'rxjs';
import { isNullOrUndefined } from '@ironsource/fusion-ui/utils';
import { takeUntil } from 'rxjs/operators';
import * as i2 from '@ironsource/fusion-ui/components/sidebar/common/services';
import * as i1 from '@ironsource/fusion-ui/services/window';

class SidebarBaseComponent {
    set configuration(value) {
        this.menuItems = value?.menuItems ?? [];
        this.user = value?.user;
        this.dynamicContent = value?.sidebarDynamicContent;
        this.pathNameNormalizeRegex = value?.pathNameNormalizeRegex;
        this.isLogoClickable = !!value?.logoClickable;
    }
    set sidebarOpen(value) {
        this.setSidebarState(value);
    }
    // Must be set to host!
    get sidebarClosed() {
        return !this.isSidebarOpen;
    }
    constructor(windowRef, elementRef, sidebarMenuService) {
        this.windowRef = windowRef;
        this.elementRef = elementRef;
        this.sidebarMenuService = sidebarMenuService;
        this.saveSidebarState = true;
        this.sidebarOpenChange = new EventEmitter();
        this.sidebarMenuItemClicked = new EventEmitter();
        this.navigationEnded = new EventEmitter();
        this.sidebarClosedHovered = new EventEmitter();
        this.logoutIconClicked = new EventEmitter();
        this.logoClicked = new EventEmitter();
        this.menuItems = [];
        this.isLogoClickable = false;
        this.openedMenuItem$ = this.sidebarMenuService.openedMenuItem$;
        // sidebar closed but need expand it by mouse over
        this.isSidebarClosedHovered$ = new BehaviorSubject(false);
        // for sidebar closed mouse events (closed-open)
        this.listenHostMouseEvents$ = new BehaviorSubject(true);
        this.onDestroy$ = new Subject();
    }
    ngOnInit() {
        // emit state to host
        this.sidebarOpenChange.emit(this.isSidebarOpen);
        // init mouse events on host (if closed)
        this.listenHostMouseEvents$.asObservable().pipe(takeUntil(this.onDestroy$)).subscribe(this.initHostMouseEventListeners.bind(this));
        if (!this.saveSidebarState) {
            this.windowRef.nativeWindow.localStorage.removeItem('sidebarState');
        }
    }
    ngOnDestroy() {
        this.onDestroy$.next();
        this.onDestroy$.complete();
    }
    onLogoClicked() {
        if (this.isLogoClickable) {
            this.logoClicked.emit();
        }
    }
    onMenuItemClicked(menuItem) {
        this.sidebarMenuItemClicked.emit(menuItem);
    }
    onNavigationEnded() {
        this.navigationEnded.emit();
    }
    onLogoutIconClicked() {
        this.logoutIconClicked.emit(this.user);
    }
    initHostMouseEventListeners(isClosed) {
        if (isClosed) {
            this.sidebarMouseEventSubscription$ = merge(fromEvent(this.elementRef.nativeElement, 'mouseenter'), fromEvent(this.elementRef.nativeElement, 'mouseleave'))
                .pipe(takeUntil(this.onDestroy$))
                .subscribe(($event) => {
                const sidebarClosedHovered = this.isSidebarOpen ? false : $event.type === 'mouseenter';
                // for mouseenter
                this.isSidebarClosedHovered$.next(sidebarClosedHovered);
                this.sidebarClosedHovered.emit(sidebarClosedHovered);
            });
        }
        else {
            if (this.sidebarMouseEventSubscription$ && typeof this.sidebarMouseEventSubscription$.unsubscribe === 'function') {
                this.sidebarMouseEventSubscription$.unsubscribe();
                this.sidebarMouseEventSubscription$ = null;
            }
        }
    }
    setSidebarState(isOpen) {
        this.isSidebarOpen = isNullOrUndefined(isOpen) ? this.getSidebarState() : isOpen;
        if (this.saveSidebarState) {
            this.windowRef.nativeWindow.localStorage.setItem('sidebarState', JSON.stringify(this.isSidebarOpen));
        }
        // activate sidebar mouse listeners
        this.listenHostMouseEvents$.next(!this.isSidebarOpen);
    }
    getSidebarState() {
        const storedState = JSON.parse(this.windowRef.nativeWindow.localStorage.getItem('sidebarState') ?? 'true');
        return !isNullOrUndefined(storedState) ? storedState : true;
    }
    static { this.ɵfac = function SidebarBaseComponent_Factory(t) { return new (t || SidebarBaseComponent)(i0.ɵɵdirectiveInject(i1.WindowService), i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i2.SidebarMenuService)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: SidebarBaseComponent, hostVars: 2, hostBindings: function SidebarBaseComponent_HostBindings(rf, ctx) { if (rf & 2) {
            i0.ɵɵclassProp("fu-sidebar-closed", ctx.sidebarClosed);
        } }, inputs: { configuration: "configuration", sidebarOpen: "sidebarOpen", saveSidebarState: "saveSidebarState" }, outputs: { sidebarOpenChange: "sidebarOpenChange", sidebarMenuItemClicked: "sidebarMenuItemClicked", navigationEnded: "navigationEnded", sidebarClosedHovered: "sidebarClosedHovered", logoutIconClicked: "logoutIconClicked", logoClicked: "logoClicked" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(SidebarBaseComponent, [{
        type: Directive
    }], () => [{ type: i1.WindowService }, { type: i0.ElementRef }, { type: i2.SidebarMenuService }], { configuration: [{
            type: Input
        }], sidebarOpen: [{
            type: Input
        }], saveSidebarState: [{
            type: Input
        }], sidebarOpenChange: [{
            type: Output
        }], sidebarMenuItemClicked: [{
            type: Output
        }], navigationEnded: [{
            type: Output
        }], sidebarClosedHovered: [{
            type: Output
        }], logoutIconClicked: [{
            type: Output
        }], logoClicked: [{
            type: Output
        }], sidebarClosed: [{
            type: HostBinding,
            args: ['class.fu-sidebar-closed']
        }] }); })();

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

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