import { useCallback, useEffect, useMemo, useState } from "react";
import { selectShouldShowThinkingDots } from "./agentSessionSelectors";
import { selectHasSpawnedAgents, selectHasTodoContent, } from "./agentSessionSelectors";
export const STREAM_PAUSED_THINKING_AFTER_MS = 2_000;
export function useAgentTerminalViewState(input) {
    const { agent, agentStub, messages, error, hideGreeting, isSubmitting, isConnecting, activeInlineDialog, allPendingApprovalsCount, isAgentThinking, isExecuting, isWaitingForResponse, sessionCostLimitExceeded, isLoading, sessionState, } = input;
    const showInitialThinkingSplash = messages.length === 0 &&
        !error &&
        hideGreeting &&
        (isSubmitting || isConnecting);
    // Changes whenever a streaming assistant message receives new content;
    // empty when nothing is streaming.
    const streamingContentSignature = useMemo(() => {
        let signature = "";
        for (const message of messages) {
            if (message.role !== "assistant" || message.isCompleted)
                continue;
            signature += `${message.id}:${message.content?.length || 0}:${message.reasoning?.length || 0}:${message.toolCalls?.length || 0};`;
        }
        return signature;
    }, [messages]);
    const [isStreamPaused, setIsStreamPaused] = useState(false);
    useEffect(() => {
        setIsStreamPaused(false);
        if (!streamingContentSignature)
            return;
        const timeoutId = window.setTimeout(() => setIsStreamPaused(true), STREAM_PAUSED_THINKING_AFTER_MS);
        return () => window.clearTimeout(timeoutId);
    }, [streamingContentSignature]);
    const shouldShowThinkingDots = selectShouldShowThinkingDots({
        activeInlineDialog,
        allPendingApprovalsCount,
        isAgentThinking,
        isConnecting,
        isExecuting,
        isStreamPaused,
        isSubmitting,
        isWaitingForResponse,
        messages,
        sessionCostLimitExceeded,
        showInitialThinkingSplash,
    });
    const parentAgentId = agent?.parentAgentId ||
        agent?.ParentAgentId ||
        agentStub?.parentAgentId ||
        agentStub?.ParentAgentId;
    const handleBackToParent = useCallback(() => {
        if (!parentAgentId)
            return;
        window.dispatchEvent(new CustomEvent("editor:openAgent", {
            detail: { agentId: parentAgentId },
        }));
    }, [parentAgentId]);
    return {
        showInitialThinkingSplash,
        shouldShowThinkingDots,
        parentAgentId,
        handleBackToParent,
        shouldShowLoadingContent: isLoading && !activeInlineDialog,
        hasSpawnedAgents: selectHasSpawnedAgents(sessionState),
        hasTodoContent: selectHasTodoContent(sessionState),
        hasHistoryContent: sessionState.operationHistory.length > 0,
    };
}
//# sourceMappingURL=useAgentTerminalViewState.js.map