var _excluded = ["dataGa", "table", "testPrefix", "testPrefixCallback", "coloredSortedColumn", "meta", "overscan", "getHasNextPage", "getHasPrevPage", "getItemKey", "loading", "loadMore", "onVirtualChange", "virtualRef", "initialOffset"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
import React, { memo, useCallback, useRef, useEffect } from "react";
import { useVirtualizer, defaultRangeExtractor } from "@tanstack/react-virtual";
import identity from "lodash/identity";
import { useTableState } from "../provider";
import Row from "./row";
import Header from "./header";
var noop = function noop() {};
var rerenderSelector = function rerenderSelector(state) {
  return {
    sorting: state.sorting,
    sizing: state.columnSizing,
    rowsById: state.rowsById,
    expanded: state.expanded,
    grouping: state.grouping
  };
};
var Body = /*#__PURE__*/memo(function (_ref) {
  var dataGa = _ref.dataGa,
    table = _ref.table,
    testPrefix = _ref.testPrefix,
    testPrefixCallback = _ref.testPrefixCallback,
    coloredSortedColumn = _ref.coloredSortedColumn,
    meta = _ref.meta,
    overscan = _ref.overscan,
    _ref$getHasNextPage = _ref.getHasNextPage,
    getHasNextPage = _ref$getHasNextPage === void 0 ? noop : _ref$getHasNextPage,
    _ref$getHasPrevPage = _ref.getHasPrevPage,
    getHasPrevPage = _ref$getHasPrevPage === void 0 ? noop : _ref$getHasPrevPage,
    _ref$getItemKey = _ref.getItemKey,
    _getItemKey = _ref$getItemKey === void 0 ? identity : _ref$getItemKey,
    loading = _ref.loading,
    _ref$loadMore = _ref.loadMore,
    loadMore = _ref$loadMore === void 0 ? noop : _ref$loadMore,
    onVirtualChange = _ref.onVirtualChange,
    virtualRef = _ref.virtualRef,
    _ref$initialOffset = _ref.initialOffset,
    initialOffset = _ref$initialOffset === void 0 ? 0 : _ref$initialOffset,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  useTableState(rerenderSelector);
  var ref = useRef();
  var _table$getRowModel = table.getRowModel(),
    rows = _table$getRowModel.rows;
  var rowVirtualizer = useVirtualizer({
    count: rows.length ? rows.length + 1 : 1,
    getScrollElement: function getScrollElement() {
      return ref.current;
    },
    enableSmoothScroll: false,
    estimateSize: function estimateSize() {
      var _meta$styles, _meta$cellStyles;
      return !!((_meta$styles = meta.styles) != null && _meta$styles.height) && parseInt(meta.styles.height) || !!((_meta$cellStyles = meta.cellStyles) != null && _meta$cellStyles.height) && parseInt(meta.cellStyles.height) || 35;
    },
    overscan: overscan || 15,
    onChange: onVirtualChange,
    initialOffset: initialOffset,
    getItemKey: function getItemKey(index) {
      return index === 0 ? "header" : _getItemKey(index - 1);
    },
    rangeExtractor: useCallback(function (range) {
      if (range.count && range.startIndex >= 0) {
        var next = new Set([0].concat(_toConsumableArray(defaultRangeExtractor(range))));
        return _toConsumableArray(next).sort(function (a, b) {
          return a - b;
        });
      }
      return defaultRangeExtractor(range);
    }, [])
  });
  if (virtualRef) virtualRef.current = rowVirtualizer;
  var virtualRows = rowVirtualizer.getVirtualItems();
  useEffect(function () {
    if (!loadMore) return;
    var lastItem = virtualRows[virtualRows.length - 1];
    if (!lastItem) return;
    if (lastItem.index >= rows.length && getHasNextPage() && !loading) {
      loadMore("backward");
    }
  }, [virtualRows, loading]);
  useEffect(function () {
    if (!loadMore) return;
    var first = virtualRows[1];
    if (!first) return;
    if (first.index <= 1 && getHasPrevPage() && !loading) {
      loadMore("forward");
    }
  }, [virtualRows, getHasPrevPage(), loading]);
  return /*#__PURE__*/React.createElement("div", {
    ref: ref,
    style: {
      display: "flex",
      height: "100%",
      overflow: "auto",
      flex: "1"
      // maxWidth: `min(${table.getTotalSize()}, 100%)`, // WHY: Removing since it causes issues with fullWidth cells
    },
    "data-testid": "netdata-table" + testPrefix
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      height: rowVirtualizer.getTotalSize() + "px",
      position: "relative",
      display: "flex",
      flex: "1 0 auto"
    }
  }, virtualRows.map(function (virtualRow) {
    return /*#__PURE__*/React.createElement("div", {
      key: virtualRow.key,
      style: {
        transform: "translateY(" + virtualRow.start + "px)",
        top: 0,
        left: 0,
        position: virtualRow.key === "header" ? "sticky" : "absolute",
        zIndex: virtualRow.key === "header" ? 1 : 0,
        minWidth: "100%",
        alignSelf: "start",
        flex: "1 1 auto",
        display: "flex"
      },
      "data-index": virtualRow.index,
      ref: rowVirtualizer.measureElement
    }, virtualRow.index === 0 ? /*#__PURE__*/React.createElement(Header, _extends({
      dataGa: dataGa,
      table: table,
      testPrefix: testPrefix,
      coloredSortedColumn: coloredSortedColumn,
      index: virtualRow.index
    }, rest)) : /*#__PURE__*/React.createElement(Row, _extends({
      dataGa: dataGa,
      table: table,
      testPrefix: testPrefix,
      testPrefixCallback: testPrefixCallback,
      coloredSortedColumn: coloredSortedColumn,
      meta: meta,
      row: rows[virtualRow.index - 1],
      index: virtualRow.index
    }, rest)));
  })));
});
export default Body;