import { useCallback } from "react";
import { completeOpenAssistantMessagesInTranscript, normalizeSessionRunId, } from "./agentSessionState";
export function useAgentSettleCompletedRun(input) {
    const { currentRunIdRef, lastSettledRunIdRef, messagesRef, clearStopGuard, setError, clearHeartbeatMessages, setAgent, isWaitingRef, shouldCreateNewMessageRef, sessionActions, } = input;
    return useCallback((finalStatus = "completed", runId) => {
        const normalized = normalizeSessionRunId(runId);
        if (normalized !== null) {
            if (lastSettledRunIdRef.current === normalized) {
                return;
            }
            if (currentRunIdRef.current !== null &&
                currentRunIdRef.current !== normalized) {
                return;
            }
            lastSettledRunIdRef.current = normalized;
        }
        clearStopGuard();
        setError(null);
        clearHeartbeatMessages();
        messagesRef.current = completeOpenAssistantMessagesInTranscript(messagesRef.current);
        sessionActions.completeOpenAssistantMessages();
        setAgent((prev) => prev
            ? {
                ...prev,
                status: "idle",
                statusMessage: finalStatus === "cancelled" ? "Run cancelled" : undefined,
            }
            : prev);
        isWaitingRef.current = false;
        shouldCreateNewMessageRef.current = false;
        sessionActions.patchRunState({
            isAgentThinking: false,
            isConnecting: false,
            isSubmitting: false,
            isWaitingForResponse: false,
            lastStatusReason: null,
        });
    }, [
        clearHeartbeatMessages,
        clearStopGuard,
        currentRunIdRef,
        isWaitingRef,
        lastSettledRunIdRef,
        messagesRef,
        sessionActions,
        setAgent,
        setError,
        shouldCreateNewMessageRef,
    ]);
}
//# sourceMappingURL=useAgentSettleCompletedRun.js.map