"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));
function easeInOutCubic(t) {
  return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
}
const VirtualList = ({
  gap,
  hasNextPage = false,
  isLoading,
  itemHeight,
  items,
  LoadingComponent,
  onVisibleItemsScrollEnd,
  overscan = 5,
  renderItem,
  scrollToTop = false,
  bottomComponent,
  threshold = 5
}) => {
  const parentRef = (0, import_react.useRef)(null);
  const scrollToFn = (0, import_react.useCallback)(
    (offset, options, instance) => {
      const element = instance.scrollElement;
      if (!element)
        return;
      const duration = options.behavior === "smooth" ? 100 : 0;
      if (duration === 0) {
        element.scrollTop = offset;
        return;
      }
      const startTime = performance.now();
      const startTop = element.scrollTop;
      const distanceToScroll = offset - startTop;
      const scrollStep = (currentTime) => {
        const elapsed = currentTime - startTime;
        const progress = Math.min(elapsed / duration, 1);
        const easedProgress = easeInOutCubic(progress);
        element.scrollTop = startTop + distanceToScroll * easedProgress;
        if (progress < 1) {
          requestAnimationFrame(scrollStep);
        }
      };
      requestAnimationFrame(scrollStep);
    },
    []
  );
  const rowVirtualizer = (0, import_react_virtual.useVirtualizer)({
    gap,
    count: hasNextPage ? items.length + 1 : items.length,
    overscan,
    getScrollElement: () => parentRef.current,
    estimateSize: () => itemHeight,
    scrollToFn
  });
  (0, import_react.useEffect)(() => {
    if (scrollToTop && parentRef.current) {
      scrollToFn(0, { behavior: "smooth" }, { scrollElement: parentRef.current });
    }
  }, [scrollToTop, scrollToFn]);
  const virtualItems = rowVirtualizer.getVirtualItems();
  (0, import_react.useEffect)(() => {
    if (!virtualItems.length)
      return;
    const lastItem = virtualItems[virtualItems.length - 1];
    if (lastItem.index >= items.length - 1 - threshold && hasNextPage && !isLoading && onVisibleItemsScrollEnd) {
      onVisibleItemsScrollEnd();
    }
  }, [hasNextPage, onVisibleItemsScrollEnd, items.length, isLoading, threshold, virtualItems]);
  const showCustomLoadingComponent = !!LoadingComponent;
  const Loading = (0, import_react.useCallback)(
    () => showCustomLoadingComponent ? LoadingComponent : /* @__PURE__ */ import_react.default.createElement(DefaultLoadingComponent, null),
    [showCustomLoadingComponent, LoadingComponent]
  );
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      ref: parentRef,
      style: { width: "100%", height: "100%", overflow: "auto", scrollbarWidth: "none" }
    },
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        style: {
          height: `${rowVirtualizer.getTotalSize()}px`,
          width: "100%",
          display: "flex",
          flexDirection: "column",
          position: "relative"
        }
      },
      rowVirtualizer.getVirtualItems().map((virtualRow) => {
        const item = items[virtualRow.index];
        return /* @__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%"
            }
          },
          item ? renderItem(item) : /* @__PURE__ */ import_react.default.createElement(Loading, null)
        );
      }),
      bottomComponent && /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          style: {
            position: "absolute",
            top: `${rowVirtualizer.getTotalSize()}px`,
            left: 0,
            width: "100%"
          }
        },
        bottomComponent
      )
    ),
    isLoading && /* @__PURE__ */ import_react.default.createElement(Loading, null)
  );
};
//# sourceMappingURL=VirtualList.js.map
