import { useCallback } from "react";
import { MACHINE_CAPACITY_REASON } from "../agentMessageHelpers";
export function useAgentSubmitResponseHandler(input) {
    const { agentStub, isWaitingRef, onAgentUpdate, clearSuppressedQueuedPrompt, removeOptimisticTranscriptMessage, setAgent, setIsWaitingForResponse, setIsConnecting, setIsAgentThinking, setLastRunStatusReason, } = input;
    return useCallback((input) => {
        const { response, tempMessageId, suppressedQueuedPromptToken } = input;
        if (agentStub.status === "new") {
            onAgentUpdate?.({ status: "running" });
        }
        const isQueuedForCapacity = response.reason === MACHINE_CAPACITY_REASON ||
            response.message?.toLowerCase().includes("machine slot") ||
            false;
        const wasQueued = !isQueuedForCapacity &&
            response.message?.toLowerCase().includes("queued");
        if (isQueuedForCapacity) {
            setLastRunStatusReason(MACHINE_CAPACITY_REASON);
            setAgent((prev) => prev
                ? {
                    ...prev,
                    status: "running",
                    statusMessage: response.message ||
                        "Waiting for capacity. The agent will start automatically when a slot becomes available.",
                }
                : prev);
            setIsWaitingForResponse(false);
            isWaitingRef.current = false;
            setIsConnecting(false);
            setIsAgentThinking(false);
            return;
        }
        if (wasQueued) {
            console.log("[AgentTerminal] Prompt queued for processing");
            setIsWaitingForResponse(false);
            isWaitingRef.current = false;
            setIsAgentThinking(false);
            removeOptimisticTranscriptMessage(tempMessageId);
            return;
        }
        setLastRunStatusReason(null);
        clearSuppressedQueuedPrompt(suppressedQueuedPromptToken);
        setIsWaitingForResponse(true);
        isWaitingRef.current = true;
        setIsAgentThinking(true);
    }, [
        agentStub.status,
        clearSuppressedQueuedPrompt,
        isWaitingRef,
        onAgentUpdate,
        removeOptimisticTranscriptMessage,
        setAgent,
        setIsAgentThinking,
        setIsConnecting,
        setIsWaitingForResponse,
        setLastRunStatusReason,
    ]);
}
//# sourceMappingURL=useAgentSubmitResponseHandler.js.map