import { useCallback } from "react";
import { mergeViewedContext, toEditorContextSnapshot, } from "./agentContextMerge";
export function useAgentSubmitContextBuilder(input) {
    const { agentMetadata, includeEditorContext = false, includeLiveEditorContext, captureEditorContextOnCreate = false, isNewAgent = false, editContext, buildCurrentContext, addVisibleTestIdsToContext, } = input;
    const liveEditorContextEnabled = includeLiveEditorContext ?? includeEditorContext;
    return useCallback(async (options) => {
        const factoryName = agentMetadata?.additionalData
            ?.contextFactory;
        if (factoryName) {
            const factory = editContext?.getContextFactory?.(factoryName);
            if (factory) {
                try {
                    await Promise.resolve(factory());
                }
                catch (error) {
                    console.warn(`Context factory '${factoryName}' failed: ${error?.message || String(error)}`);
                }
            }
            else {
                console.warn(`Context factory not found: ${factoryName}. Proceeding without it.`);
            }
        }
        let effectiveContext = agentMetadata
            ? {
                ...agentMetadata,
                editorContext: agentMetadata.editorContext
                    ? { ...agentMetadata.editorContext }
                    : undefined,
            }
            : null;
        try {
            const shouldCaptureCreateContext = captureEditorContextOnCreate &&
                isNewAgent &&
                !effectiveContext?.editorContext?.startedFrom;
            if ((liveEditorContextEnabled || shouldCaptureCreateContext) &&
                typeof buildCurrentContext === "function") {
                const freshContext = buildCurrentContext();
                const freshSnapshot = toEditorContextSnapshot(freshContext);
                if (freshSnapshot) {
                    effectiveContext = effectiveContext ?? {};
                    effectiveContext.editorContext = {
                        ...(effectiveContext.editorContext || {}),
                    };
                    if (shouldCaptureCreateContext) {
                        effectiveContext.editorContext.startedFrom = freshSnapshot;
                    }
                    if (liveEditorContextEnabled) {
                        effectiveContext.editorContext.currentlyViewing = freshSnapshot;
                    }
                }
            }
            if (!captureEditorContextOnCreate) {
                delete effectiveContext?.editorContext?.startedFrom;
            }
            if (!liveEditorContextEnabled) {
                delete effectiveContext?.editorContext?.currentlyViewing;
            }
            if (effectiveContext?.editorContext &&
                !effectiveContext.editorContext.startedFrom &&
                !effectiveContext.editorContext.currentlyViewing) {
                delete effectiveContext.editorContext;
            }
            if (captureEditorContextOnCreate) {
                effectiveContext = mergeViewedContext(effectiveContext, effectiveContext?.editorContext?.startedFrom);
            }
            if (liveEditorContextEnabled) {
                effectiveContext = mergeViewedContext(effectiveContext, effectiveContext?.editorContext?.currentlyViewing);
            }
        }
        catch (error) {
            console.warn(options.liveContextWarning, error);
        }
        return addVisibleTestIdsToContext(effectiveContext, options.profileName);
    }, [
        addVisibleTestIdsToContext,
        agentMetadata,
        buildCurrentContext,
        captureEditorContextOnCreate,
        editContext,
        includeEditorContext,
        isNewAgent,
        liveEditorContextEnabled,
    ]);
}
//# sourceMappingURL=useAgentSubmitContextBuilder.js.map