"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ToolbarState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _mobx = require("mobx");
var _ToolbarResponsiveState = require("../../state/Toolbar/ToolbarResponsiveState");
class ToolbarState {
  constructor(toolbar) {
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    (0, _defineProperty2.default)(this, "responsive", void 0);
    this.toolbar = toolbar;
    this.responsive = new _ToolbarResponsiveState.ToolbarResponsiveState({
      toolbar: this
    });
    (0, _mobx.makeObservable)(this, {
      init: _mobx.action
    });
  }
  _subscribeResizeOrMoved({
    scrollElement
  }) {
    const {
      toolbar: table
    } = this;
    if (scrollElement == null) {
      return;
    }
    const onResizedOrMoved = (0, _mobx.action)(function onResizedOrMoved() {
      const scrollElementRect = scrollElement.getBoundingClientRect();
      table.scrollElementRect.height = scrollElementRect.height;
      table.scrollElementRect.top = scrollElementRect.top;
    });
    const onResizedOrMovedThrottled = table.container.lodash.throttle(() => window.requestAnimationFrame(onResizedOrMoved), 300);
    const onScroll = () => {
      onResizedOrMovedThrottled();
    };
    scrollElement.addEventListener('scroll', onScroll);
    const dispose = (0, _bexCore.addResizeObserver)(scrollElement, onResizedOrMovedThrottled);
    const onResizedOrMovedAnimationFrame = window.requestAnimationFrame(() => {
      window.requestAnimationFrame(onResizedOrMoved);
    });
    return () => {
      dispose == null || dispose();
      window.cancelAnimationFrame(onResizedOrMovedAnimationFrame);
      scrollElement.removeEventListener('scroll', onScroll);
    };
  }
  init({
    scrollElement
  }) {
    const {
      toolbar,
      responsive
    } = this;
    const disposers = [toolbar.toolbarRect.init({
      scrollElement
    }), this._subscribeResizeOrMoved({
      scrollElement
    }), responsive.init()];
    return () => {
      disposers.forEach(d => d == null ? void 0 : d());
    };
  }
}
exports.ToolbarState = ToolbarState;
//# sourceMappingURL=ToolbarState.js.map