"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.NARROW_PAGE_BREAKPOINT = exports.CollectionPageState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _PageState = require("../PageState");
var _resizeObserverCompat = require("../resizeObserverCompat");
var _CollectionPageStateBIReporter = require("./CollectionPageStateBIReporter");
var _throttle = _interopRequireDefault(require("lodash/throttle"));
const NARROW_PAGE_BREAKPOINT = exports.NARROW_PAGE_BREAKPOINT = 960;
class CollectionPageState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "pageTitle", void 0);
    (0, _defineProperty2.default)(this, "hasHeader", false);
    (0, _defineProperty2.default)(this, "total", false);
    (0, _defineProperty2.default)(this, "hideHeaderTotal", false);
    (0, _defineProperty2.default)(this, "scrollTopBeforeUnmount", null);
    (0, _defineProperty2.default)(this, "withinRouter", void 0);
    (0, _defineProperty2.default)(this, "isNarrow", false);
    (0, _defineProperty2.default)(this, "startTime", performance.now());
    (0, _defineProperty2.default)(this, "bi", void 0);
    (0, _defineProperty2.default)(this, "pageState", new _PageState.PageState());
    (0, _defineProperty2.default)(this, "primaryToolbar", null);
    this.pageTitle = params.pageTitle;
    this.hasHeader = params.hasHeader;
    this.hideHeaderTotal = params.hideHeaderTotal;
    this.withinRouter = params.withinRouter;
    if (params.reportBI) {
      this.bi = new _CollectionPageStateBIReporter.CollectionPageStateBIReporter({
        reportBI: params.reportBI,
        collectionPageState: this
      });
    }
    (0, _mobx.makeObservable)(this, {
      pageTitle: _mobx.observable.ref,
      hasHeader: _mobx.observable.ref,
      hideHeaderTotal: _mobx.observable.ref,
      total: _mobx.observable.ref,
      primaryToolbar: _mobx.observable.ref,
      isNarrow: _mobx.observable.ref,
      _updateIsNarrow: _mobx.action.bound
    });
  }
  _updateIsNarrow() {
    const el = this.pageState.stackingRootRef.current;
    this.isNarrow = el ? el.clientWidth < NARROW_PAGE_BREAKPOINT : false;
  }
  init() {
    var _this$bi, _this$bi2;
    (_this$bi = this.bi) == null || _this$bi.init();
    const scrollableContent = this.pageState.scrollableContentRef.current;
    if (!scrollableContent) {
      return () => {};
    }
    const scrollDisposer = (0, _resizeObserverCompat.addResizeObserver)(scrollableContent, () => {
      if (this.scrollTopBeforeUnmount === null) {
        return;
      }
      if (scrollableContent.scrollHeight >= this.scrollTopBeforeUnmount) {
        scrollableContent.scrollTo({
          top: this.scrollTopBeforeUnmount
        });
        this.scrollTopBeforeUnmount = null;
      }
    });
    this._updateIsNarrow();
    const narrowDisposer = (0, _resizeObserverCompat.addResizeObserver)(this.pageState.stackingRootRef.current, (0, _throttle.default)(this._updateIsNarrow, 100));
    (_this$bi2 = this.bi) == null || _this$bi2.appLoaded == null || _this$bi2.appLoaded();
    return () => {
      scrollDisposer == null || scrollDisposer();
      narrowDisposer == null || narrowDisposer();
    };
  }
}
exports.CollectionPageState = CollectionPageState;
//# sourceMappingURL=CollectionPageState.js.map