"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.TableVirtualState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
class TableVirtualState {
  get container() {
    return this.table.toolbar.container;
  }
  constructor(params) {
    (0, _defineProperty2.default)(this, "gridRef", void 0);
    (0, _defineProperty2.default)(this, "table", void 0);
    (0, _defineProperty2.default)(this, "getVirtualItemKey", void 0);
    (0, _defineProperty2.default)(this, "gap", 0);
    (0, _defineProperty2.default)(this, "rowHeight", 0);
    (0, _defineProperty2.default)(this, "estimatedRowHeight", 80);
    (0, _defineProperty2.default)(this, "rowHeights", {});
    (0, _defineProperty2.default)(this, "setVariableRowHeight", ({
      size,
      key
    }) => {
      this.rowHeights = {
        ...this.rowHeights,
        [key]: size
      };
    });
    this.table = params.table;
    this.getVirtualItemKey = params.getVirtualItemKey;
    (0, _mobx.makeObservable)(this, {
      rowHeight: _mobx.observable.ref,
      estimatedRowHeight: _mobx.observable.ref,
      rowHeights: _mobx.observable.ref,
      calcGridProps: _mobx.computed,
      rowHeightFunc: _mobx.computed,
      itemsToRenderCount: _mobx.computed,
      itemKeyRecyclerOverride: _mobx.computed,
      init: _mobx.action,
      setVariableRowHeight: _mobx.action
    });
    this.init();
  }
  get forceRenderIndexes() {
    return this.toolbar.forceRenderIndexes;
  }
  init() {
    const disposers = [(0, _mobx.reaction)(() => this.rowHeights, async () => {
      var _this$gridRef;
      (_this$gridRef = this.gridRef) == null || _this$gridRef.resetAfterIndices({
        columnIndex: 0,
        rowIndex: 0,
        shouldForceUpdate: true
      });
    })];
    return () => {
      disposers.forEach(disposer => disposer == null ? void 0 : disposer());
    };
  }
  get toolbar() {
    return this.table.toolbar;
  }
  get itemsContentWidth() {
    return this.table.toolbar.itemsContentRect.rect.width ?? 0;
  }
  get itemKeyRecyclerOverride() {
    const {
      getVirtualItemKey,
      table: {
        keyedItems
      }
    } = this;
    return index => {
      const keyedItem = keyedItems[index];
      return (getVirtualItemKey == null ? void 0 : getVirtualItemKey(keyedItem)) ?? (keyedItem == null ? void 0 : keyedItem.key) ?? index;
    };
  }
  get itemsToRenderCount() {
    return this.table.keyedItems.length;
  }
  get rowHeightFunc() {
    const {
      rowHeight: rowHeightProp,
      table: {
        keyedItems
      }
    } = this;
    if (process.env.NODE_ENV === 'test' && rowHeightProp == null) {
      // need to provide rowHeight for tests
      return () => 20;
    }
    if (rowHeightProp == null) {
      return undefined;
    }
    if (typeof rowHeightProp === 'number') {
      return () => rowHeightProp;
    }
    return (0, _mobx.action)(index => rowHeightProp(keyedItems[index].item, index));
  }
  get calcGridProps() {
    const {
      rowHeightFunc: rowHeight,
      itemsContentWidth,
      table: {
        toolbar
      },
      estimatedRowHeight
    } = this;
    const {
      itemsContentHeight
    } = toolbar;
    const avgRowHeight = (rowHeight == null ? void 0 : rowHeight(0)) ?? estimatedRowHeight;
    const overscanCount = avgRowHeight == null ? 10 : Math.round(itemsContentHeight / avgRowHeight * 1.5);
    return {
      columnCount: 1,
      width: itemsContentWidth,
      columnWidth: () => itemsContentWidth,
      columnWidthIncludingGap: () => itemsContentWidth,
      rowHeight,
      rowHeightIncludingGap: ({
        index,
        key
      }) => {
        if (rowHeight != null) {
          return rowHeight(index);
        }
        return this.rowHeights[key];
      },
      estimatedRowHeight,
      overscanCount
    };
  }
}
exports.TableVirtualState = TableVirtualState;
//# sourceMappingURL=TableVirtualState.js.map