import { useCallback } from "react";
export function useAgentRunStartSocketHandler(input) {
    const { agent, isStoppingRef, currentRunIdRef, lastSettledRunIdRef, lastSeqRef, seenMessageIdsRef, lastResyncedMessageIdsRef, messagesRef, shouldCreateNewMessageRef, isWaitingRef, actions, normalizeRunId, noteMissingRunId, setAgent, setLastRunStatusReason, setIsConnecting, setIsWaitingForResponse, setIsAgentThinking, } = input;
    return useCallback((messageType, payload) => {
        if (messageType !== "agent:run:start") {
            return false;
        }
        if (isStoppingRef.current) {
            console.log("[AgentTerminal] Ignoring agent:run:start during stop operation");
            return true;
        }
        const startRunId = normalizeRunId(payload?.runId);
        if (startRunId === null) {
            noteMissingRunId("agent:run:start");
        }
        if (startRunId !== null) {
            if (lastSettledRunIdRef.current === startRunId) {
                return true;
            }
            if (currentRunIdRef.current === startRunId) {
                return true;
            }
            currentRunIdRef.current = startRunId;
            lastSeqRef.current = 0;
            seenMessageIdsRef.current.clear();
            for (const message of messagesRef.current) {
                if (message.id) {
                    seenMessageIdsRef.current.add(message.id.toLowerCase());
                }
            }
            lastResyncedMessageIdsRef.current.clear();
        }
        else {
            lastSeqRef.current = 0;
        }
        actions.runStart({
            runId: payload?.runId,
        });
        const currentStatus = agent?.status;
        if (currentStatus === "waitingForInput" ||
            currentStatus === "waitingForApproval" ||
            currentStatus === "costLimitReached") {
            return true;
        }
        setLastRunStatusReason(null);
        setIsConnecting(true);
        setIsWaitingForResponse(true);
        isWaitingRef.current = true;
        shouldCreateNewMessageRef.current = false;
        setIsAgentThinking(true);
        setAgent((prev) => (prev ? { ...prev, status: "running" } : prev));
        return true;
    }, [
        actions,
        agent,
        currentRunIdRef,
        isStoppingRef,
        isWaitingRef,
        lastResyncedMessageIdsRef,
        lastSeqRef,
        lastSettledRunIdRef,
        messagesRef,
        normalizeRunId,
        noteMissingRunId,
        seenMessageIdsRef,
        setAgent,
        setIsAgentThinking,
        setIsConnecting,
        setIsWaitingForResponse,
        setLastRunStatusReason,
        shouldCreateNewMessageRef,
    ]);
}
//# sourceMappingURL=useAgentRunStartSocketHandler.js.map