"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require$$0 = require("react/jsx-runtime");
const designSystem = require("@choiceform/design-system");
const react = require("../../_vendor/@legendapp/state/react.cjs");
const React = require("react");
const tv = require("./tv.cjs");
const usePagination = require("./hooks/use-pagination.cjs");
const useMessageVirtualizer = require("./hooks/use-message-virtualizer.cjs");
const useStickToBottom = require("../../_vendor/use-stick-to-bottom/dist-useStickToBottom.cjs");
const loadMoreButton = require("./components/load-more-button.cjs");
const scrollToBottomButton = require("./components/scroll-to-bottom-button.cjs");
const messageStore = require("../../stores/message-store.cjs");
const paginationStore = require("../../stores/pagination-store.cjs");
const LOAD_MORE_BUTTON_HEIGHT = 40;
const Conversation = react.observer(function Conversation2(props) {
  const {
    className,
    children,
    estimateSize = 120,
    overscan = 5,
    paddingTop = 16,
    paddingBottom = 32,
    chatId,
    onLoadMessages,
    pageSize = 10
  } = props;
  const currentMessages = react.use$(messageStore.messageComputed$.currentMessages);
  const totalCount = react.use$(paginationStore.paginationStore$.totalCount);
  const scrollContainerRef = React.useRef(null);
  const { loadMoreMessages, isLoading, hasMore } = usePagination.usePagination({
    chatId,
    onLoadMessages,
    scrollContainerRef,
    pageSize
  });
  const { virtualizer } = useMessageVirtualizer.useMessageVirtualizer({
    messages: currentMessages,
    scrollContainerRef,
    estimateSize,
    overscan,
    paddingTop,
    paddingBottom
  });
  const tv$1 = tv.ConversationTv();
  const handleLoadMore = async () => {
    await loadMoreMessages();
    virtualizer.scrollToIndex(0, { align: "start", behavior: "smooth" });
  };
  const shouldShowLoadMore = hasMore && totalCount > pageSize;
  const shouldShowWelcome = react.use$(messageStore.messageComputed$.shouldShowWelcome);
  const { scrollRef, contentRef, isAtBottom, scrollToBottom } = useStickToBottom.useStickToBottom({
    resize: "smooth",
    initial: "smooth"
  });
  if (shouldShowWelcome) return null;
  return /* @__PURE__ */ require$$0.jsxs("div", { className: tv$1.root({ className }), children: [
    /* @__PURE__ */ require$$0.jsx(designSystem.ScrollArea, { children: /* @__PURE__ */ require$$0.jsx(
      designSystem.ScrollArea.Viewport,
      {
        ref: designSystem.mergeRefs(scrollRef, scrollContainerRef),
        className: tv$1.viewport(),
        children: /* @__PURE__ */ require$$0.jsxs(
          designSystem.ScrollArea.Content,
          {
            ref: contentRef,
            className: tv$1.content(),
            style: {
              height: virtualizer.getTotalSize() + (shouldShowLoadMore ? LOAD_MORE_BUTTON_HEIGHT : 0),
              // 为LoadMore按钮预留空间
              maxWidth: "var(--max-width, 100%)"
            },
            children: [
              shouldShowLoadMore && /* @__PURE__ */ require$$0.jsx(
                loadMoreButton.LoadMoreButton,
                {
                  hasMore,
                  isLoading,
                  onLoadMore: handleLoadMore,
                  totalCount,
                  loadedCount: currentMessages.length
                }
              ),
              virtualizer.getVirtualItems().map((virtualItem) => {
                const message = currentMessages[virtualItem.index];
                return /* @__PURE__ */ require$$0.jsx(
                  "div",
                  {
                    "data-index": virtualItem.index,
                    ref: virtualizer.measureElement,
                    className: tv$1.virtualItems(),
                    style: {
                      transform: `translateY(${virtualItem.start + (shouldShowLoadMore ? LOAD_MORE_BUTTON_HEIGHT : 0)}px)`
                    },
                    children: children ? children(message, message.contentSegments || [], virtualItem.index) : null
                  },
                  virtualItem.key
                );
              })
            ]
          }
        )
      }
    ) }),
    /* @__PURE__ */ require$$0.jsx(
      scrollToBottomButton.ScrollToBottomButton,
      {
        shouldShow: !isAtBottom,
        scrollToBottom
      }
    )
  ] });
});
exports.Conversation = Conversation;
//# sourceMappingURL=conversation.cjs.map
