import { jsx } from "react/jsx-runtime";
import { observer, use$ as useSelector } from "../../_vendor/@legendapp/state/react.js";
import { useRef } from "react";
import { usePagination } from "./hooks/use-pagination.js";
import { useMessageVirtualizer } from "./hooks/use-message-virtualizer.js";
import { ScrollContainer } from "./components/scroll-container.js";
import { messageComputed$ } from "../../stores/message-store.js";
import { paginationStore$ } from "../../stores/pagination-store.js";
const Conversation = observer(function Conversation2(props) {
  const {
    className,
    children,
    estimateSize = 120,
    overscan = 5,
    paddingTop = 16,
    paddingBottom = 32,
    chatId,
    onLoadMessages,
    pageSize = 10
  } = props;
  const currentMessages = useSelector(messageComputed$.currentMessages);
  const totalCount = useSelector(paginationStore$.totalCount);
  const scrollContainerRef = useRef(null);
  const { loadMoreMessages, isLoading, hasMore } = usePagination({
    chatId,
    onLoadMessages,
    scrollContainerRef,
    pageSize
  });
  const { virtualizer } = useMessageVirtualizer({
    messages: currentMessages,
    scrollContainerRef,
    estimateSize,
    overscan,
    paddingTop,
    paddingBottom
  });
  const handleLoadMore = async () => {
    await loadMoreMessages();
    virtualizer.scrollToIndex(0, { align: "start", behavior: "smooth" });
  };
  const shouldShowLoadMore = hasMore && totalCount > pageSize;
  const shouldShowWelcome = useSelector(messageComputed$.shouldShowWelcome);
  if (shouldShowWelcome) return null;
  return /* @__PURE__ */ jsx(
    ScrollContainer,
    {
      className,
      scrollContainerRef,
      virtualizer,
      messages: currentMessages,
      shouldShowLoadMore,
      hasMore,
      isLoading,
      totalCount,
      onLoadMore: handleLoadMore,
      children
    }
  );
});
export {
  Conversation
};
//# sourceMappingURL=conversation.js.map
