"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ToolbarResponsiveState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _mobx = require("mobx");
var _HorizontalSpaceAvailableState = require("../Responsive/HorizontalSpaceAvailableState");
var _ToolbarResponsivePriorityState = require("./ToolbarResponsivePriorityState");
var _HorizontalOverflowState = require("../Responsive/HorizontalOverflowState");
class ToolbarResponsiveState {
  get container() {
    return this.toolbar.toolbar.container;
  }
  constructor(params) {
    (0, _defineProperty2.default)(this, "actions", void 0);
    (0, _defineProperty2.default)(this, "search", void 0);
    (0, _defineProperty2.default)(this, "filters", void 0);
    (0, _defineProperty2.default)(this, "_nextToShrink", void 0);
    (0, _defineProperty2.default)(this, "_nextToExpand", void 0);
    (0, _defineProperty2.default)(this, "actionsAvailableSpace", void 0);
    (0, _defineProperty2.default)(this, "overflow", void 0);
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    this.toolbar = params.toolbar;
    this.overflow = new _HorizontalOverflowState.HorizontalOverflowState({
      state: this
    });
    this.search = new _ToolbarResponsivePriorityState.ToolbarResponsivePriorityState({
      responsive: this,
      getNextToShrink: () => this.actions,
      getNextToExpand: () => this.search,
      count: 1,
      minWidth: 140
    });
    this.actions = new _ToolbarResponsivePriorityState.ToolbarResponsivePriorityState({
      responsive: this,
      getNextToShrink: () => this.filters,
      getNextToExpand: () => this.search,
      count: 2
    });
    this.filters = new _ToolbarResponsivePriorityState.ToolbarResponsivePriorityState({
      responsive: this,
      getNextToShrink: () => this.filters,
      getNextToExpand: () => this.actions,
      count: 3
    });
    this._nextToShrink = this.search;
    this._nextToExpand = this.search;
    this.actionsAvailableSpace = new _HorizontalSpaceAvailableState.HorizontalSpaceAvailableState({
      state: this
    });
    (0, _mobx.makeObservable)(this, {
      _nextToShrink: _mobx.observable.ref,
      _nextToExpand: _mobx.observable.ref,
      responsiveDisabled: _mobx.computed
    });
  }
  get responsiveDisabled() {
    const {
      toolbar: {
        toolbar: {
          _syncedProps
        }
      },
      container: {
        internalExperiments
      }
    } = this;
    return !(_syncedProps != null && _syncedProps.useResponsiveLayout) || !(internalExperiments != null && internalExperiments.enabled('specs.cairo.EnableResponsiveToolbar11420'));
  }
  init() {
    const {
      toolbar: {
        toolbar
      },
      overflow,
      actionsAvailableSpace,
      responsiveDisabled
    } = this;
    if (responsiveDisabled) {
      return;
    }
    const disposers = [(0, _bexCore.addResizeObserver)(toolbar.toolbarRect.element, actionsAvailableSpace._syncThrottled), (0, _mobx.reaction)(() => ({
      _isOverflowing: overflow._isOverflowing,
      _scrollWidth: overflow._scrollWidth,
      _clientWidth: overflow._clientWidth
    }), () => {
      this._nextToShrink._shrinkIfNeeded();
    }, {
      fireImmediately: true
    }), (0, _mobx.reaction)(() => actionsAvailableSpace.available, () => {
      this._nextToExpand._expandIfNeeded();
    }, {
      fireImmediately: true
    })];
    return () => {
      disposers.forEach(d => d == null ? void 0 : d());
    };
  }
}
exports.ToolbarResponsiveState = ToolbarResponsiveState;
//# sourceMappingURL=ToolbarResponsiveState.js.map