"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const designSystem = require("@choiceform/design-system");
const React = require("react");
const tv = require("../tv.cjs");
const loadMoreButton = require("./load-more-button.cjs");
const scrollToBottomButton = require("./scroll-to-bottom-button.cjs");
const index = require("../../../_vendor/usehooks-ts/dist-index.cjs");
const useStickToBottom = require("../../../_vendor/use-stick-to-bottom/dist-useStickToBottom.cjs");
const LOAD_MORE_BUTTON_HEIGHT = 40;
const ScrollContainer = React.memo(function ScrollContainer2(props) {
  const {
    scrollContainerRef,
    virtualizer,
    messages,
    shouldShowLoadMore,
    hasMore,
    isLoading,
    totalCount,
    onLoadMore,
    children,
    className
  } = props;
  const tv$1 = tv.ConversationTv();
  const { scrollRef, contentRef, isAtBottom, scrollToBottom } = useStickToBottom.useStickToBottom({
    resize: "smooth",
    initial: "instant"
  });
  const { height = 0 } = index.useResizeObserver({
    ref: scrollContainerRef,
    box: "border-box"
  });
  const lastIndex = messages.length - 1;
  const secondLastIndex = messages.length - 2;
  const lastTwoItemsHeight = React.useMemo(() => {
    return (lastIndex >= 0 ? virtualizer.measurementsCache[lastIndex]?.size || 0 : 0) + (secondLastIndex >= 0 ? virtualizer.measurementsCache[secondLastIndex]?.size || 0 : 0);
  }, [lastIndex, secondLastIndex, virtualizer.measurementsCache]);
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: tv$1.root({ className }), children: [
    /* @__PURE__ */ jsxRuntime.jsx(designSystem.ScrollArea, { hoverBoundary: "none", children: /* @__PURE__ */ jsxRuntime.jsx(
      designSystem.ScrollArea.Viewport,
      {
        ref: designSystem.mergeRefs(scrollRef, scrollContainerRef),
        className: tv$1.viewport(),
        style: {
          height: "var(--viewport-height, 100%)",
          paddingBottom: `calc(var(--viewport-height, ${height}px) - ${lastTwoItemsHeight}px - 40px)`
        },
        children: /* @__PURE__ */ jsxRuntime.jsxs(
          designSystem.ScrollArea.Content,
          {
            ref: contentRef,
            className: tv$1.content(),
            style: {
              height: virtualizer.getTotalSize() + (shouldShowLoadMore ? LOAD_MORE_BUTTON_HEIGHT : 0),
              maxWidth: "var(--max-width, 100%)"
            },
            children: [
              shouldShowLoadMore && /* @__PURE__ */ jsxRuntime.jsx(
                loadMoreButton.LoadMoreButton,
                {
                  hasMore,
                  isLoading,
                  onLoadMore,
                  totalCount,
                  loadedCount: messages.length
                }
              ),
              virtualizer.getVirtualItems().map((virtualItem) => {
                const message = messages[virtualItem.index];
                return /* @__PURE__ */ jsxRuntime.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__ */ jsxRuntime.jsx(
      scrollToBottomButton.ScrollToBottomButton,
      {
        shouldShow: !isAtBottom,
        scrollToBottom
      }
    )
  ] });
});
exports.ScrollContainer = ScrollContainer;
//# sourceMappingURL=scroll-container.cjs.map
