import { useCallback } from "react";
export function useAgentSubmitContextBuilder(input) {
    const { agent, agentMetadata, activeProfile, profiles, editContext, buildCurrentContext, addVisibleTestIdsToContext, } = input;
    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;
        try {
            const normalizedAgentProfileId = agent?.profileId?.toLowerCase();
            const profile = activeProfile ||
                profiles.find((candidate) => candidate.id?.toLowerCase() === normalizedAgentProfileId);
            const isLiveMode = profile?.editorContextMode === "live";
            if (isLiveMode && typeof buildCurrentContext === "function") {
                const freshContext = buildCurrentContext();
                if (freshContext) {
                    effectiveContext = {
                        ...(agentMetadata || {}),
                        items: freshContext.items,
                        currentItemId: freshContext.currentItemId,
                        components: freshContext.components,
                        field: freshContext.field,
                        activeWorkspace: freshContext.activeWorkspace,
                        hasPageLoaded: freshContext.hasPageLoaded,
                    };
                }
            }
        }
        catch (error) {
            console.warn(options.liveContextWarning, error);
        }
        return addVisibleTestIdsToContext(effectiveContext, options.profileName);
    }, [
        activeProfile,
        addVisibleTestIdsToContext,
        agent?.profileId,
        agentMetadata,
        buildCurrentContext,
        editContext,
        profiles,
    ]);
}
//# sourceMappingURL=useAgentSubmitContextBuilder.js.map