import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useLayoutEffect, useRef, useState } from "react";
import { Loader2 } from "lucide-react";
import { AgentGreeting } from "./AgentGreeting";
import { AgentProfileSwitcher } from "./AgentProfileSwitcher";
import { InitialThinkingSplash } from "./InitialThinkingSplash";
import { QueuedPromptsPanel } from "./QueuedPromptsPanel";
import { AgentRunStatusBanners } from "./AgentRunStatusBanners";
import { AgentTerminalMessageGroups, } from "./AgentTerminalMessageGroups";
import { AgentThinkingIndicator } from "./AgentThinkingIndicator";
import { AgentTerminalContextPanels } from "./AgentTerminalContextPanels";
export function AgentTerminalFullUpperContent({ agent, agentStub, error, lastRunStatusReason, messagesContainerRef, onScroll, messages, hideGreeting, isLoading, activeProfile, onGreetingPromptClick, showInitialThinkingSplash, operations, isExecuting, defaultCollapseJson, allPendingApprovals, onSwitchToAutonomous, readOnly, onQuickAction, shouldShowThinkingDots, isInitialGreetingMode, simpleMode, costLimitBanner, messagesEndRef, hideContext, isMobile, contextChrome, documentListRef, hasTodoContent, hasSpawnedAgents, hasHistoryContent, agentMetadata, contextPanelsActiveTab, setContextPanelsActiveTab, queuedPrompts, onPushQueuedPrompt, onDeleteQueuedPrompt, queuePushDisabled, queueDeleteDisabled, conversationInlineDialog, conversationInlineDialogKey, profiles, onProfileChange, canSwitchProfile, }) {
    const conversationPanelRef = useRef(null);
    const previousLayoutRef = useRef({
        isInitialGreetingMode,
        height: 0,
    });
    const [isExpandingHeightFromGreeting, setIsExpandingHeightFromGreeting] = useState(false);
    // Scroll the conversation so the questionnaire's TOP is in view once per
    // dialog (title and first question visible — a tall form scrolled to the
    // bottom would open on its footer). Covers both the live request path and
    // recovery after reload. Re-renders while the same dialog stays open must
    // not yank a user who scrolled away.
    const conversationInlineDialogRef = useRef(null);
    const lastScrolledDialogKeyRef = useRef(null);
    useLayoutEffect(() => {
        const panel = conversationPanelRef.current;
        if (!panel) {
            return;
        }
        const currentHeight = panel.getBoundingClientRect().height;
        const previousLayout = previousLayoutRef.current;
        previousLayoutRef.current = {
            isInitialGreetingMode,
            height: currentHeight,
        };
        if (!previousLayout.isInitialGreetingMode ||
            isInitialGreetingMode ||
            previousLayout.height <= 0 ||
            currentHeight <= previousLayout.height + 1) {
            return;
        }
        panel.style.setProperty("--agent-terminal-expand-from-height", `${previousLayout.height}px`);
        panel.style.setProperty("--agent-terminal-expand-to-height", `${currentHeight}px`);
        setIsExpandingHeightFromGreeting(true);
        const timeoutId = window.setTimeout(() => setIsExpandingHeightFromGreeting(false), 560);
        return () => window.clearTimeout(timeoutId);
    }, [isInitialGreetingMode]);
    useLayoutEffect(() => {
        if (!conversationInlineDialogKey) {
            lastScrolledDialogKeyRef.current = null;
            return;
        }
        if (lastScrolledDialogKeyRef.current === conversationInlineDialogKey) {
            return;
        }
        lastScrolledDialogKeyRef.current = conversationInlineDialogKey;
        const scrollDialogIntoView = () => {
            const container = messagesContainerRef.current;
            const dialogElement = conversationInlineDialogRef.current;
            if (!container || !dialogElement) {
                return;
            }
            const containerRect = container.getBoundingClientRect();
            const dialogRect = dialogElement.getBoundingClientRect();
            container.scrollTop += dialogRect.top - containerRect.top - 8;
        };
        scrollDialogIntoView();
        let secondAnimationFrameId = 0;
        const firstAnimationFrameId = window.requestAnimationFrame(() => {
            scrollDialogIntoView();
            secondAnimationFrameId =
                window.requestAnimationFrame(scrollDialogIntoView);
        });
        const timeoutIds = [50, 150, 300, 600, 1000].map((delay) => window.setTimeout(scrollDialogIntoView, delay));
        return () => {
            window.cancelAnimationFrame(firstAnimationFrameId);
            if (secondAnimationFrameId) {
                window.cancelAnimationFrame(secondAnimationFrameId);
            }
            timeoutIds.forEach((timeoutId) => window.clearTimeout(timeoutId));
        };
    }, [conversationInlineDialogKey, messagesContainerRef]);
    return (_jsxs("div", { ref: conversationPanelRef, className: [
            isInitialGreetingMode
                ? "flex min-h-0 w-full min-w-0 shrink-0 flex-col"
                : "flex min-h-0 w-full min-w-0 flex-1 flex-col overflow-hidden",
            isExpandingHeightFromGreeting
                ? "agent-terminal-height-expand-from-greeting"
                : "",
        ]
            .filter(Boolean)
            .join(" "), children: [_jsx(AgentRunStatusBanners, { agent: agent, agentStub: agentStub, error: error, lastRunStatusReason: lastRunStatusReason }), _jsxs("div", { ref: messagesContainerRef, className: isInitialGreetingMode
                    ? "w-full min-w-0 overflow-visible"
                    : "min-h-0 w-full min-w-0 flex-1 overflow-y-auto", onScroll: onScroll, children: [messages.length === 0 && !error && !hideGreeting && !isLoading ? (_jsx("div", { className: isInitialGreetingMode
                            ? "flex w-full min-w-0 items-center justify-center"
                            : "flex h-full w-full min-w-0 items-center justify-center", children: !activeProfile ? (_jsx(Loader2, { strokeWidth: 1, className: "text-neutral-grey-50 mx-auto h-8 w-8 animate-spin" })) : (_jsx(AgentGreeting, { profile: activeProfile, onPromptClick: onGreetingPromptClick, className: isInitialGreetingMode ? "h-auto p-0" : undefined, profileSwitcher: canSwitchProfile && profiles?.length && onProfileChange ? (_jsx(AgentProfileSwitcher, { profiles: profiles, currentProfileId: activeProfile.id, onSelectProfile: onProfileChange })) : undefined })) })) : null, showInitialThinkingSplash ? _jsx(InitialThinkingSplash, {}) : null, _jsxs("div", { className: "space-y-0 select-text", children: [_jsx(AgentTerminalMessageGroups, { messages: messages, operations: operations, isExecuting: isExecuting, defaultCollapseJson: defaultCollapseJson, agentId: agent?.id || agentStub.id, allPendingApprovals: allPendingApprovals, onSwitchToAutonomous: onSwitchToAutonomous, readOnly: readOnly, onQuickAction: onQuickAction }), shouldShowThinkingDots ? _jsx(AgentThinkingIndicator, {}) : null] }), conversationInlineDialog ? (_jsx("div", { ref: conversationInlineDialogRef, "data-testid": "agent-conversation-inline-dialog", className: "px-4 py-3", children: conversationInlineDialog })) : null, !simpleMode && costLimitBanner, _jsx("div", { ref: messagesEndRef })] }), _jsx(AgentTerminalContextPanels, { hideContext: hideContext, simpleMode: simpleMode, isMobile: isMobile, contextChrome: contextChrome, agent: agent, activeProfile: activeProfile, documentListRef: documentListRef, hasTodoContent: hasTodoContent, hasSpawnedAgents: hasSpawnedAgents, hasHistoryContent: hasHistoryContent, messages: messages, agentMetadata: agentMetadata, operations: operations, activeTab: contextPanelsActiveTab, setActiveTab: setContextPanelsActiveTab }), !simpleMode ? (_jsx(QueuedPromptsPanel, { queuedPrompts: queuedPrompts, onPushQueuedPrompt: onPushQueuedPrompt, onDeleteQueuedPrompt: onDeleteQueuedPrompt, pushDisabled: queuePushDisabled, deleteDisabled: queueDeleteDisabled })) : null] }));
}
//# sourceMappingURL=AgentTerminalFullUpperContent.js.map