"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var VirtualList_exports = {};
__export(VirtualList_exports, {
  VirtualList: () => VirtualList
});
module.exports = __toCommonJS(VirtualList_exports);
var import_react = __toESM(require("react"));
var import_react_virtual = require("@tanstack/react-virtual");
var import_components = require("../../../components");
const DefaultLoadingComponent = () => /* @__PURE__ */ import_react.default.createElement(import_components.Flex, { justifyContent: "center", alignItems: "center", height: "76px" }, /* @__PURE__ */ import_react.default.createElement(import_components.InfiniteLoader, null));
const VirtualList = ({
  itemHeight,
  overscan = 5,
  LoadingComponent,
  isLoading,
  hasNextPage = false,
  threshold = 5,
  onVisibleItemsScrollEnd,
  items,
  renderItem
}) => {
  const parentRef = (0, import_react.useRef)(null);
  const rowVirtualizer = (0, import_react_virtual.useVirtualizer)({
    count: hasNextPage ? items.length + 1 : items.length,
    getScrollElement: () => parentRef.current,
    estimateSize: () => itemHeight,
    overscan
  });
  (0, import_react.useEffect)(() => {
    const items2 = rowVirtualizer.getVirtualItems();
    if (!items2.length)
      return;
    const lastItem = items2[items2.length - 1];
    if (lastItem.index >= items2.length - 1 - threshold && hasNextPage && !isLoading && onVisibleItemsScrollEnd) {
      onVisibleItemsScrollEnd();
    }
  }, [
    hasNextPage,
    onVisibleItemsScrollEnd,
    items.length,
    isLoading,
    rowVirtualizer.getVirtualItems()
  ]);
  const showCustomLoadingComponent = !!LoadingComponent;
  return /* @__PURE__ */ import_react.default.createElement("div", { ref: parentRef, style: { width: "100%", height: "100%", overflow: "auto" } }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: {
        height: `${rowVirtualizer.getTotalSize()}px`,
        width: "100%",
        position: "relative"
      }
    },
    rowVirtualizer.getVirtualItems().map((virtualRow) => /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: virtualRow.index,
        "data-index": virtualRow.index,
        ref: rowVirtualizer.measureElement,
        style: {
          position: "absolute",
          top: 0,
          left: 0,
          transform: `translateY(${virtualRow.start}px)`,
          height: `${itemHeight}px`,
          width: "100%"
        }
      },
      renderItem(items[virtualRow.index])
    ))
  ), isLoading && (showCustomLoadingComponent ? LoadingComponent : /* @__PURE__ */ import_react.default.createElement(DefaultLoadingComponent, null)));
};
//# sourceMappingURL=VirtualList.js.map
