"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.HorizontalSpaceAvailableState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _bexCore = require("@wix/bex-core");
class HorizontalSpaceAvailableState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "_syncThrottled", void 0);
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "_state", void 0);
    (0, _defineProperty2.default)(this, "_element", void 0);
    (0, _defineProperty2.default)(this, "_child", void 0);
    (0, _defineProperty2.default)(this, "_directChildClientWidth", 0);
    (0, _defineProperty2.default)(this, "_clientWidth", 0);
    const {
      state
    } = params;
    this.container = state.container;
    this._state = state;
    (0, _mobx.makeObservable)(this, {
      _sync: _mobx.action.bound,
      _directChildClientWidth: _mobx.observable.ref,
      _clientWidth: _mobx.observable.ref,
      available: _mobx.computed
    });
    this._syncThrottled = this.container.lodash.throttle(() => {
      window.requestAnimationFrame(this._sync);
    }, 200);
  }
  _sync() {
    const {
      _element,
      _child
    } = this;
    if (!_element || !_child) {
      return;
    }
    this._clientWidth = _element.clientWidth;
    this._directChildClientWidth = _child.scrollWidth;
  }
  get available() {
    return this._clientWidth - this._directChildClientWidth;
  }
  initLayout() {
    const {
      _element,
      _child,
      _state
    } = this;
    if (_state.responsiveDisabled) {
      return;
    }
    if (!_element || !_child) {
      return;
    }
    this._sync();
  }
  init() {
    const {
      _element,
      _child,
      _state
    } = this;
    if (_state.responsiveDisabled) {
      return;
    }
    if (!_element || !_child) {
      return;
    }
    const disposers = [(0, _bexCore.addResizeObserver)(_child, this._syncThrottled)];
    return () => {
      disposers.forEach(d => d == null ? void 0 : d());
    };
  }
}
exports.HorizontalSpaceAvailableState = HorizontalSpaceAvailableState;
//# sourceMappingURL=HorizontalSpaceAvailableState.js.map