"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useVirtual = useVirtual;
var _reactVirtual = require("@tanstack/react-virtual");
var _types = require("../types");
function useVirtual(tableRef, tableModel, size) {
  const {
    rows
  } = tableModel.getRowModel();
  const expanded = tableModel.getState().expanded;

  // we need to count how many expanded rows are currently on the page
  // to update the total number of items for virtualizer appropriately
  const expandedRowsOnCurrentPage = rows.filter(row => expanded[row.id]);
  const rowVirtualizer = (0, _reactVirtual.useVirtualizer)({
    getScrollElement: () => tableRef.current,
    count: rows.length + expandedRowsOnCurrentPage.length * 2,
    // we have to double number of expanded rows, cause we have one extra row after expanded one for correct padding and shadow
    estimateSize: () => _types.ROW_SIZE[size],
    // for tests we want to make overscan bigger to render more rows, as there are some issues with row sizes in the latest react-virtual releases
    // you can override REACT_VIRTUAL_TEST_OVERSCAN value in your test using jest.mock. (see table-react.test.tsx for reference)
    overscan: process.env.NODE_ENV === 'test' ? _types.REACT_VIRTUAL_TEST_OVERSCAN : _types.REACT_VIRTUAL_OVERSCAN
  });
  const virtualRows = rowVirtualizer.getVirtualItems();
  const totalSize = rowVirtualizer.getTotalSize();
  const paddingTop = virtualRows.length > 0 ? virtualRows?.[0]?.start || 0 : 0;
  const paddingBottom = virtualRows.length > 0 ? totalSize - (virtualRows?.[virtualRows.length - 1]?.end || 0) : 0;
  return {
    paddingTop,
    paddingBottom,
    virtualRows,
    measureElement: rowVirtualizer.measureElement
  };
}