"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("../../../_vendor/@tanstack/react-virtual/dist-esm-index.cjs");
const index_browser = require("../../../_vendor/nanoid/index.browser.cjs");
const React = require("react");
function useMessageVirtualizer({
  messages,
  scrollContainerRef,
  estimateSize = 120,
  overscan = 5,
  paddingTop = 16,
  paddingBottom = 32
}) {
  const missingKeyCache = React.useRef(/* @__PURE__ */ new Map());
  const virtualizer = index.useVirtualizer({
    count: messages.length,
    getScrollElement: () => scrollContainerRef.current,
    estimateSize: () => estimateSize,
    overscan,
    paddingStart: paddingTop,
    paddingEnd: paddingBottom,
    // 启用自动测量
    measureElement: (element) => {
      return element.getBoundingClientRect().height;
    },
    // 添加 key extractor 来确保消息删除后虚拟列表能正确更新
    getItemKey: (index2) => {
      const message = messages[index2];
      if (message?.id) {
        return message.id;
      }
      if (!missingKeyCache.current.has(index2)) {
        missingKeyCache.current.set(index2, index_browser.nanoid());
      }
      return missingKeyCache.current.get(index2);
    },
    debug: false,
    scrollMargin: 0,
    enabled: true
  });
  return {
    virtualizer
  };
}
exports.useMessageVirtualizer = useMessageVirtualizer;
//# sourceMappingURL=use-message-virtualizer.cjs.map
