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