"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ToolbarResponsivePriorityState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
class ToolbarResponsivePriorityState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "responsive", void 0);
    (0, _defineProperty2.default)(this, "getNextToShrink", void 0);
    (0, _defineProperty2.default)(this, "getNextToExpand", void 0);
    (0, _defineProperty2.default)(this, "_count", void 0);
    (0, _defineProperty2.default)(this, "_minWidth", void 0);
    (0, _defineProperty2.default)(this, "_collapsedWidth", void 0);
    (0, _defineProperty2.default)(this, "_shouldShrink", false);
    (0, _defineProperty2.default)(this, "_groupElement", void 0);
    this._count = params.count;
    this._minWidth = params.minWidth ?? 0;
    this._collapsedWidth = params.minWidth ?? 0;
    this.responsive = params.responsive;
    this.getNextToShrink = params.getNextToShrink;
    this.getNextToExpand = params.getNextToExpand;
    (0, _mobx.makeObservable)(this, {
      _minWidth: _mobx.observable.ref,
      _shouldShrink: _mobx.observable.ref,
      _syncWidth: _mobx.action,
      _expandIfNeeded: _mobx.action,
      _shrinkIfNeeded: _mobx.action
    });
  }
  _syncWidth() {
    const {
      _groupElement,
      _shouldShrink,
      responsive
    } = this;
    if (responsive.responsiveDisabled) {
      return;
    }
    if (!_groupElement) {
      return;
    }
    if (_shouldShrink) {
      this._collapsedWidth = _groupElement.scrollWidth;
    } else {
      this._minWidth = _groupElement.scrollWidth;
    }
  }
  _expandIfNeeded({
    preCalculatedAvailableSpace
  } = {}) {
    const {
      responsive,
      _collapsedWidth
    } = this;
    const {
      _nextToExpand,
      actionsAvailableSpace: {
        available
      }
    } = responsive;
    if (_nextToExpand !== this || (preCalculatedAvailableSpace ?? available) + _collapsedWidth <= this._minWidth || !this._shouldShrink) {
      return;
    }
    this._shouldShrink = false;
    responsive._nextToExpand = _nextToExpand.getNextToExpand();
    responsive._nextToShrink = this;
    responsive.actionsAvailableSpace._syncThrottled();

    // experimental - expanding the next item without waiting for the layout tick
    // const availableAfterExpand = available + _collapsedWidth - this._minWidth;
    // responsive._nextToExpand._expandIfNeeded({
    //   preCalculatedAvailableSpace: availableAfterExpand,
    // });
  }
  _shrinkIfNeeded() {
    const {
      responsive
    } = this;
    const {
      _nextToShrink,
      overflow
    } = responsive;
    if (_nextToShrink !== this || !overflow._isOverflowing || this._shouldShrink) {
      return;
    }
    this._shouldShrink = true;
    responsive._nextToShrink = _nextToShrink.getNextToShrink();
    responsive._nextToExpand = this;
    responsive.overflow._syncThrottled();
  }
}
exports.ToolbarResponsivePriorityState = ToolbarResponsivePriorityState;
//# sourceMappingURL=ToolbarResponsivePriorityState.js.map