"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ToolbarLeftGroupState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
class ToolbarLeftGroupState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "_responsive", void 0);
    (0, _defineProperty2.default)(this, "_getTabsState", void 0);
    (0, _defineProperty2.default)(this, "_element", null);
    (0, _defineProperty2.default)(this, "_prevWidth", 0);
    (0, _defineProperty2.default)(this, "mode", void 0);
    (0, _defineProperty2.default)(this, "minWidth", void 0);
    (0, _defineProperty2.default)(this, "EXPANSION_THRESHOLD", 20);
    (0, _defineProperty2.default)(this, "_handleResize", () => {
      const element = this._element;
      if (!element) {
        return;
      }
      const width = element.clientWidth;
      const prevWidth = this._prevWidth;
      const isAtMinWidth = width <= this.minWidth + 2; // 2px tolerance
      const isGrowing = width > prevWidth;
      const hasSpaceToExpand = width > this.minWidth + this.EXPANSION_THRESHOLD;
      if (isAtMinWidth) {
        this._responsive.overflow._scrollWidth = this._responsive.overflow._clientWidth + 1;
        this._responsive._nextToShrink._shrinkIfNeeded();
      } else if (isGrowing && hasSpaceToExpand) {
        const availableSpace = width - this.minWidth;
        this._responsive._nextToExpand._expandIfNeeded({
          preCalculatedAvailableSpace: availableSpace
        });
      }
      if (this.mode === 'tabs') {
        var _this$_getTabsState;
        const tabsState = (_this$_getTabsState = this._getTabsState) == null ? void 0 : _this$_getTabsState.call(this);
        tabsState == null || tabsState.responsive.setAvailableWidth(width);
      }
      this._prevWidth = width;
    });
    this._responsive = params.responsive;
    this.mode = params.mode;
    this.minWidth = params.minWidth;
    this._getTabsState = params.getTabsState;
  }
  init(element) {
    this._element = null;
    this._prevWidth = 0;
    if (!element) {
      return () => {};
    }
    this._element = element;
    const disposer = (0, _bexCore.addResizeObserver)(element, this._handleResize);
    this._handleResize();
    return () => {
      disposer == null || disposer();
      this._element = null;
      this._prevWidth = 0;
    };
  }
}
exports.ToolbarLeftGroupState = ToolbarLeftGroupState;
//# sourceMappingURL=ToolbarLeftGroupState.js.map