import { useEffect, useRef } from "react";
import { normalizeAgentSessionInitialPrompt } from "./useAgentSessionSync";
export function useAgentPendingInitialPrompt(input) {
    const { initialPrompt, agentStubId, agent, agentMetadata, initialMetadata, activeProfile, selectedModelId, effectiveIsFocused, isInteractionReady, isLoading, isSubmitting, readOnly, editContext, submittedInitialPromptKeyRef, handleSubmit, } = input;
    // The splash screen clears initialPrompt after first render; keep a local
    // copy until the terminal is ready to submit it.
    const pendingInitialPromptRef = useRef(normalizeAgentSessionInitialPrompt(initialPrompt));
    const localSubmittedInitialPromptKeyRef = useRef(null);
    const submittedKeyRef = submittedInitialPromptKeyRef ?? localSubmittedInitialPromptKeyRef;
    useEffect(() => {
        const normalizedPrompt = normalizeAgentSessionInitialPrompt(initialPrompt);
        if (!normalizedPrompt)
            return;
        const promptKey = `${agentStubId}:${normalizedPrompt}`;
        if (submittedKeyRef.current === promptKey)
            return;
        pendingInitialPromptRef.current = normalizedPrompt;
    }, [agentStubId, initialPrompt, submittedKeyRef]);
    useEffect(() => {
        const promptToSubmit = pendingInitialPromptRef.current;
        if (!promptToSubmit)
            return;
        const promptKey = `${agentStubId}:${promptToSubmit}`;
        if (submittedKeyRef.current === promptKey) {
            pendingInitialPromptRef.current = null;
            return;
        }
        if (!effectiveIsFocused ||
            !isInteractionReady ||
            isLoading ||
            isSubmitting ||
            readOnly ||
            !editContext ||
            !agent?.id ||
            !activeProfile) {
            return;
        }
        if ((activeProfile.models?.length ?? 0) > 0 && !selectedModelId)
            return;
        if (initialMetadata && !agentMetadata)
            return;
        submittedKeyRef.current = promptKey;
        pendingInitialPromptRef.current = null;
        void handleSubmit(promptToSubmit);
    }, [
        activeProfile,
        agent?.id,
        agentMetadata,
        agentStubId,
        editContext,
        effectiveIsFocused,
        handleSubmit,
        initialMetadata,
        isInteractionReady,
        isLoading,
        isSubmitting,
        readOnly,
        selectedModelId,
        submittedKeyRef,
    ]);
}
//# sourceMappingURL=useAgentPendingInitialPrompt.js.map