import { useEffect, useRef } from "react";
import { normalizeAgentSessionInitialPrompt } from "./useAgentSessionSync";
/**
 * Starts a brand-new draft agent immediately when its profile has
 * "Auto Start On Create" enabled: the run is submitted with an empty prompt,
 * so only the profile instructions and context reach the model and the
 * response streams in without the user typing anything.
 *
 * Defers to the initial-prompt flows (splash screen `initialPrompt` prop or
 * `additionalData.initialPrompt` metadata) — when one of those is present it
 * owns the first submission and auto-start must not race it.
 */
export function useAgentAutoStartOnCreate(input) {
    const { agentStubId, agent, agentMetadata, initialMetadata, activeProfile, selectedModelId, initialPrompt, prompt, messageCount, effectiveIsFocused, isLoading, isSubmitting, readOnly, editContext, handleSubmit, } = input;
    // Once per agent: survives condition flapping while the run starts.
    const autoStartedAgentIdRef = useRef(null);
    useEffect(() => {
        if (!activeProfile?.autoStartOnCreate)
            return;
        if (autoStartedAgentIdRef.current === agentStubId)
            return;
        // Only brand-new local drafts qualify — never agents loaded from the
        // backend or agents that already have transcript content.
        if (agent?.status !== "new" || messageCount > 0)
            return;
        // An initial prompt (splash screen prop or metadata) owns the first
        // submission; a user-typed draft means the user is composing their own.
        const effectiveInitialPrompt = normalizeAgentSessionInitialPrompt(initialPrompt ?? agentMetadata?.additionalData?.initialPrompt);
        if (effectiveInitialPrompt)
            return;
        if (prompt.trim())
            return;
        if (!effectiveIsFocused ||
            isLoading ||
            isSubmitting ||
            readOnly ||
            !editContext ||
            !agent?.id) {
            return;
        }
        // Wait for terminal state the submit path depends on (mirrors
        // useAgentPendingInitialPrompt readiness checks).
        if ((activeProfile.models?.length ?? 0) > 0 && !selectedModelId)
            return;
        if (initialMetadata && !agentMetadata)
            return;
        autoStartedAgentIdRef.current = agentStubId;
        void handleSubmit("");
    }, [
        activeProfile,
        agent?.id,
        agent?.status,
        agentMetadata,
        agentStubId,
        editContext,
        effectiveIsFocused,
        handleSubmit,
        initialMetadata,
        initialPrompt,
        isLoading,
        isSubmitting,
        messageCount,
        prompt,
        readOnly,
        selectedModelId,
    ]);
}
//# sourceMappingURL=useAgentAutoStartOnCreate.js.map