import { useEffect, useRef } from "react";
import { hasPromptPlaceholders } from "./agentPromptPlaceholders";
import { normalizeAgentSessionInitialPrompt } from "./useAgentSessionSync";
export function useAgentInitialPrompt(input) {
    const { initialPrompt, agentMetadata, isInteractionReady, isLoading, messageCount, agentId, editContext, activeProfile, profilesCount, setPrompt, submittedInitialPromptKeyRef, handleSubmit, } = input;
    const initialPromptSentRef = useRef(false);
    useEffect(() => {
        const promptFromMetadata = agentMetadata?.additionalData?.initialPrompt;
        const effectiveInitialPrompt = normalizeAgentSessionInitialPrompt(initialPrompt ?? promptFromMetadata);
        const promptKey = effectiveInitialPrompt
            ? `${agentId}:${effectiveInitialPrompt}`
            : null;
        if (effectiveInitialPrompt &&
            isInteractionReady &&
            !isLoading &&
            !initialPromptSentRef.current &&
            submittedInitialPromptKeyRef?.current !== promptKey &&
            messageCount === 0 &&
            agentId &&
            editContext &&
            activeProfile &&
            profilesCount > 0) {
            initialPromptSentRef.current = true;
            if (promptKey && submittedInitialPromptKeyRef) {
                submittedInitialPromptKeyRef.current = promptKey;
            }
            // A suggested prompt (e.g. from a "start fresh chat" hint) opts out of auto-submit so the
            // user can review/edit it; everything else keeps the existing auto-submit behaviour.
            const autoSubmit = agentMetadata?.additionalData?.initialPromptAutoSubmit !== false;
            setTimeout(() => {
                setPrompt(effectiveInitialPrompt);
                const hasPlaceholders = hasPromptPlaceholders(effectiveInitialPrompt);
                if (!hasPlaceholders && autoSubmit) {
                    void handleSubmit(effectiveInitialPrompt);
                }
            }, 200);
        }
    }, [
        initialPrompt,
        agentMetadata,
        isInteractionReady,
        isLoading,
        messageCount,
        agentId,
        editContext,
        activeProfile,
        profilesCount,
        setPrompt,
        submittedInitialPromptKeyRef,
        handleSubmit,
    ]);
}
//# sourceMappingURL=useAgentInitialPrompt.js.map