import { buildCloudflareAiGatewayModelDefinition, resolveCloudflareAiGatewayBaseUrl, } from "../agents/cloudflare-ai-gateway.js";
import { CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF, VERCEL_AI_GATEWAY_DEFAULT_MODEL_REF, } from "./onboard-auth.credentials.js";
export function applyVercelAiGatewayProviderConfig(cfg) {
    const models = { ...cfg.agents?.defaults?.models };
    models[VERCEL_AI_GATEWAY_DEFAULT_MODEL_REF] = {
        ...models[VERCEL_AI_GATEWAY_DEFAULT_MODEL_REF],
        alias: models[VERCEL_AI_GATEWAY_DEFAULT_MODEL_REF]?.alias ?? "Vercel AI Gateway",
    };
    return {
        ...cfg,
        agents: {
            ...cfg.agents,
            defaults: {
                ...cfg.agents?.defaults,
                models,
            },
        },
    };
}
export function applyCloudflareAiGatewayProviderConfig(cfg, params) {
    const models = { ...cfg.agents?.defaults?.models };
    models[CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF] = {
        ...models[CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF],
        alias: models[CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF]?.alias ?? "Cloudflare AI Gateway",
    };
    const providers = { ...cfg.models?.providers };
    const existingProvider = providers["cloudflare-ai-gateway"];
    const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : [];
    const defaultModel = buildCloudflareAiGatewayModelDefinition();
    const hasDefaultModel = existingModels.some((model) => model.id === defaultModel.id);
    const mergedModels = hasDefaultModel ? existingModels : [...existingModels, defaultModel];
    const baseUrl = params?.accountId && params?.gatewayId
        ? resolveCloudflareAiGatewayBaseUrl({
            accountId: params.accountId,
            gatewayId: params.gatewayId,
        })
        : existingProvider?.baseUrl;
    if (!baseUrl) {
        return {
            ...cfg,
            agents: {
                ...cfg.agents,
                defaults: {
                    ...cfg.agents?.defaults,
                    models,
                },
            },
        };
    }
    const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {});
    const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined;
    const normalizedApiKey = resolvedApiKey?.trim();
    providers["cloudflare-ai-gateway"] = {
        ...existingProviderRest,
        baseUrl,
        api: "anthropic-messages",
        ...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}),
        models: mergedModels.length > 0 ? mergedModels : [defaultModel],
    };
    return {
        ...cfg,
        agents: {
            ...cfg.agents,
            defaults: {
                ...cfg.agents?.defaults,
                models,
            },
        },
        models: {
            mode: cfg.models?.mode ?? "merge",
            providers,
        },
    };
}
export function applyVercelAiGatewayConfig(cfg) {
    const next = applyVercelAiGatewayProviderConfig(cfg);
    const existingModel = next.agents?.defaults?.model;
    return {
        ...next,
        agents: {
            ...next.agents,
            defaults: {
                ...next.agents?.defaults,
                model: {
                    ...(existingModel && "fallbacks" in existingModel
                        ? {
                            fallbacks: existingModel.fallbacks,
                        }
                        : undefined),
                    primary: VERCEL_AI_GATEWAY_DEFAULT_MODEL_REF,
                },
            },
        },
    };
}
export function applyCloudflareAiGatewayConfig(cfg, params) {
    const next = applyCloudflareAiGatewayProviderConfig(cfg, params);
    const existingModel = next.agents?.defaults?.model;
    return {
        ...next,
        agents: {
            ...next.agents,
            defaults: {
                ...next.agents?.defaults,
                model: {
                    ...(existingModel && "fallbacks" in existingModel
                        ? {
                            fallbacks: existingModel.fallbacks,
                        }
                        : undefined),
                    primary: CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF,
                },
            },
        },
    };
}
