"use client";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { forwardRef, useCallback, useMemo, useRef, useState, } from "react";
import { Virtuoso } from "react-virtuoso";
import { cn } from "@stigmer/theme";
import { ThreadItemRenderer, } from "../execution/MessageThread";
import { FilePathContext } from "../execution/FilePathContext";
import { SandboxContext } from "../execution/SandboxContext";
import { DevProfiler, useDomNodeCount } from "./dev";
import { JumpToLatestButton } from "./JumpToLatestButton";
import { ThreadItemWrapper } from "./ThreadItemWrapper";
// ---------------------------------------------------------------------------
// Scrollbar classes shared with the non-virtualized path
// ---------------------------------------------------------------------------
const SCROLLBAR_CLASSES = cn("[scrollbar-width:thin] [scrollbar-color:var(--color-border)_transparent]", "[&::-webkit-scrollbar]:w-1.5", "[&::-webkit-scrollbar-track]:bg-transparent", "[&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-border/40");
// ---------------------------------------------------------------------------
// Custom Virtuoso components
// ---------------------------------------------------------------------------
const ScrollerWithA11y = forwardRef(function ScrollerWithA11y(props, ref) {
    return (_jsx("div", { ...props, ref: ref, role: "log", "aria-live": "polite", "aria-relevant": "additions", className: cn(props.className, SCROLLBAR_CLASSES) }));
});
// ---------------------------------------------------------------------------
// VirtualizedThread
// ---------------------------------------------------------------------------
const NEAR_BOTTOM_THRESHOLD_PX = 80;
/**
 * Internal component that renders a `MessageThread` item list via
 * `react-virtuoso`. Handles bottom-anchored chat layout,
 * follow-output scroll behavior, and the "Jump to latest" button.
 *
 * Not part of the public API — used only when `MessageThread` receives
 * `virtualized={true}`.
 *
 * @internal
 */
export function VirtualizedThread({ items, formatToolCallSummary, onApprovalSubmit, submittingApprovalIds, filePathCtx, sandboxCtx, }) {
    const virtuosoRef = useRef(null);
    const scrollerRef = useRef(null);
    const [isAtBottom, setIsAtBottom] = useState(true);
    useDomNodeCount(scrollerRef, "MessageThread:virtualized");
    const handleFollowOutput = useCallback((atBottom) => (atBottom ? "smooth" : false), []);
    const handleScrollerRef = useCallback((ref) => {
        scrollerRef.current = ref instanceof HTMLDivElement ? ref : null;
    }, []);
    const jumpToLatest = useCallback(() => {
        virtuosoRef.current?.scrollToIndex({
            index: "LAST",
            behavior: "smooth",
        });
    }, []);
    const renderProps = useMemo(() => ({
        formatToolCallSummary,
        onApprovalSubmit,
        submittingApprovalIds,
    }), [formatToolCallSummary, onApprovalSubmit, submittingApprovalIds]);
    return (_jsxs(_Fragment, { children: [_jsx(SandboxContext.Provider, { value: sandboxCtx, children: _jsx(FilePathContext.Provider, { value: filePathCtx, children: _jsx(DevProfiler, { id: "MessageThread:virtualized", children: _jsx(Virtuoso, { ref: virtuosoRef, data: items, alignToBottom: true, followOutput: handleFollowOutput, atBottomStateChange: setIsAtBottom, atBottomThreshold: NEAR_BOTTOM_THRESHOLD_PX, computeItemKey: (_index, item) => item.key, increaseViewportBy: { top: 200, bottom: 200 }, scrollerRef: handleScrollerRef, className: "h-full", components: { Scroller: ScrollerWithA11y }, itemContent: (index, item) => (_jsx("div", { className: "pb-4 pt-0 first:pt-6", children: _jsx(ThreadItemWrapper, { animate: index >= items.length - 2, children: _jsx(ThreadItemRenderer, { item: item, ...renderProps }) }) })) }) }) }) }), _jsx(JumpToLatestButton, { onClick: jumpToLatest, visible: !isAtBottom })] }));
}
//# sourceMappingURL=VirtualizedThread.js.map