"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.SidePanelsState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _mobx = require("mobx");
class SidePanelsState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "sidePanelComponents", void 0);
    (0, _defineProperty2.default)(this, "sidePanel", void 0);
    (0, _defineProperty2.default)(this, "panelGroupName", void 0);
    (0, _defineProperty2.default)(this, "closeSidePanel", () => {
      this.sidePanel.close();
    });
    this.container = params.container;
    this.panelGroupName = params.panelGroupName;
    this.sidePanel = new _bexCore.SidePanelState();
    this.sidePanelComponents = _mobx.observable.map([], {
      deep: false
    });
    (0, _mobx.makeObservable)(this, {
      registerSidePanel: _mobx.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 = [(0, _mobx.reaction)(() => sidePanelComponents.entries(), () => {
      if (sidePanel.currentPanelId && !sidePanelComponents.has(sidePanel.currentPanelId)) {
        sidePanel.close();
      }
    }, {
      fireImmediately: true
    })];
    return () => {
      disposers.forEach(d => d());
    };
  }
}
exports.SidePanelsState = SidePanelsState;
//# sourceMappingURL=SidePanelsState.js.map