"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.VirtualGridRepeaterState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
class VirtualGridRepeaterState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "gridState", void 0);
    (0, _defineProperty2.default)(this, "gridRef", void 0);
    (0, _defineProperty2.default)(this, "scrollHeight", void 0);
    (0, _defineProperty2.default)(this, "innerElement", null);
    (0, _defineProperty2.default)(this, "_itemKeyRecycler", index => {
      const {
        gridState: {
          itemKeyRecyclerOverride
        }
      } = this;
      const forceKey = itemKeyRecyclerOverride == null ? void 0 : itemKeyRecyclerOverride(index);
      if (forceKey != null) {
        return forceKey;
      }

      // https://github.com/wix-private/cairo/pull/1142
      // const [verticalStartIndex, verticalEndIndex] = this.gridRef?._getVerticalRangeToRender() ?? [];
      // const verticalCount = verticalEndIndex - verticalStartIndex + 1;
      // return index % (verticalCount * columnCount);

      return index;
    });
    this.gridState = params.gridState;
    (0, _mobx.makeObservable)(this, {
      calcGridProps: _mobx.computed,
      rowCount: _mobx.computed,
      height: _mobx.computed,
      width: _mobx.computed,
      itemKeyRecycler: _mobx.computed
    });
  }
  get height() {
    return this.gridState.toolbar.itemsContentHeight;
  }
  get width() {
    return this.gridState.itemsContentWidth;
  }
  get calcGridProps() {
    return this.gridState.calcGridProps;
  }
  get rowCount() {
    const {
      gridState: {
        itemsToRenderCount
      },
      calcGridProps: {
        columnCount
      }
    } = this;
    return Math.ceil(itemsToRenderCount / columnCount);
  }
  get itemKeyRecycler() {
    const {
      gridState: {
        itemKeyRecyclerOverride
      },
      calcGridProps: {
        columnCount
      }
    } = this;
    void itemKeyRecyclerOverride;
    return ({
      rowIndex,
      columnIndex
    }) => {
      const index = rowIndex * columnCount + columnIndex;
      return this._itemKeyRecycler(index);
    };
  }
  init({
    container: scrollableContainer
  }) {
    let animationFrameId;
    if (!scrollableContainer) {
      return;
    }
    const onScroll = ev => {
      if (process.env.NODE_ENV === 'test') {
        if (this.gridRef == null) {
          return;
        }
        const {
          _testScroll
        } = ev ?? {};
        let scrollTopValue = this.gridRef.state.scrollTop + this.gridState.toolbar.itemsContentHeight;
        if (_testScroll === 'top') {
          scrollTopValue = 0;
        }
        if (_testScroll === 'up') {
          scrollTopValue = -this.gridState.toolbar.itemsContentHeight;
        }
        this.gridRef.scrollTo({
          scrollTop: scrollTopValue
        });
        return;
      }
      animationFrameId = window.requestAnimationFrame((0, _mobx.action)(() => {
        if (this.gridRef == null || this.innerElement == null) {
          return;
        }
        const {
          innerElement,
          gridState
        } = this;
        const {
          top: innerElementTop
        } = innerElement.getBoundingClientRect();
        const {
          containerTop
        } = gridState.toolbar;
        if (containerTop == null) {
          return;
        }
        const offsetTop = innerElementTop - containerTop;
        this.gridRef.scrollTo({
          scrollTop: Math.max(-offsetTop, 0)
        });
      }));
    };
    scrollableContainer.addEventListener('scroll', onScroll);
    return () => {
      scrollableContainer.removeEventListener('scroll', onScroll);
      window.cancelAnimationFrame(animationFrameId);
    };
  }
}
exports.VirtualGridRepeaterState = VirtualGridRepeaterState;
//# sourceMappingURL=VirtualGridRepeaterState.js.map