import { useEffect, useRef } from "react";
import { hasPromptPlaceholders } from "./agentPromptPlaceholders";
import { normalizeAgentSessionInitialPrompt } from "./useAgentSessionSync";
export function useAgentInitialPrompt(input) {
    const { initialPrompt, agentMetadata, 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 &&
            !isLoading &&
            !initialPromptSentRef.current &&
            submittedInitialPromptKeyRef?.current !== promptKey &&
            messageCount === 0 &&
            agentId &&
            editContext &&
            activeProfile &&
            profilesCount > 0) {
            initialPromptSentRef.current = true;
            if (promptKey && submittedInitialPromptKeyRef) {
                submittedInitialPromptKeyRef.current = promptKey;
            }
            setTimeout(() => {
                setPrompt(effectiveInitialPrompt);
                const hasPlaceholders = hasPromptPlaceholders(effectiveInitialPrompt);
                if (!hasPlaceholders) {
                    void handleSubmit(effectiveInitialPrompt);
                }
            }, 200);
        }
    }, [
        initialPrompt,
        agentMetadata,
        isLoading,
        messageCount,
        agentId,
        editContext,
        activeProfile,
        profilesCount,
        setPrompt,
        submittedInitialPromptKeyRef,
        handleSubmit,
    ]);
}
//# sourceMappingURL=useAgentInitialPrompt.js.map