import { SidePanelState } from '@wix/bex-core';
import { action, makeObservable, observable, reaction } from 'mobx';
export class SidePanelsState {
    constructor(params) {
        this.closeSidePanel = () => {
            this.sidePanel.close();
        };
        this.container = params.container;
        this.panelGroupName = params.panelGroupName;
        this.sidePanel = new SidePanelState();
        this.sidePanelComponents = observable.map([], {
            deep: false,
        });
        makeObservable(this, {
            registerSidePanel: action,
        });
    }
    _getPanelUniqueId(panelId) {
        const { container: { environment: { componentId }, }, panelGroupName, } = this;
        return [componentId, panelGroupName, panelId].filter(Boolean).join('/');
    }
    get isFiltersPanelOpen() {
        return this.sidePanel.transition.data === this._getPanelUniqueId('filters');
    }
    registerSidePanel(panelId, component) {
        const { sidePanelComponents } = this;
        sidePanelComponents.set(this._getPanelUniqueId(panelId), component);
    }
    openSidePanel(panelId) {
        const { sidePanel } = this;
        sidePanel._open(this._getPanelUniqueId(panelId));
    }
    toggleSidePanel(panelId) {
        const { sidePanel } = this;
        const panelUniqueId = this._getPanelUniqueId(panelId);
        if (sidePanel.currentPanelId === panelUniqueId && sidePanel.isOpen) {
            this.closeSidePanel();
        }
        else {
            this.openSidePanel(panelId);
        }
        return sidePanel.isOpen;
    }
    init() {
        const { sidePanel, sidePanelComponents } = this;
        const disposers = [
            reaction(() => sidePanelComponents.entries(), () => {
                if (sidePanel.currentPanelId &&
                    !sidePanelComponents.has(sidePanel.currentPanelId)) {
                    sidePanel.close();
                }
            }, {
                fireImmediately: true,
            }),
        ];
        return () => {
            disposers.forEach((d) => d());
        };
    }
}
//# sourceMappingURL=SidePanelsState.js.map