import { APP_NAME } from "../config.js";
import { isInstallTelemetryEnabled } from "./telemetry.js";
const OPENROUTER_HOST = "openrouter.ai";
const NVIDIA_NIM_HOST = "integrate.api.nvidia.com";
const CLOUDFLARE_API_HOST = "api.cloudflare.com";
const CLOUDFLARE_AI_GATEWAY_HOST = "gateway.ai.cloudflare.com";
const OPENCODE_HOST = "opencode.ai";
function matchesHost(baseUrl, expectedHost) {
    try {
        return new URL(baseUrl).hostname === expectedHost;
    }
    catch {
        return false;
    }
}
function isOpenRouterModel(model) {
    return model.provider === "openrouter" || matchesHost(model.baseUrl, OPENROUTER_HOST);
}
function isNvidiaNimModel(model) {
    return model.provider === "nvidia" || matchesHost(model.baseUrl, NVIDIA_NIM_HOST);
}
function isCloudflareModel(model) {
    return (model.provider === "cloudflare-workers-ai" ||
        model.provider === "cloudflare-ai-gateway" ||
        matchesHost(model.baseUrl, CLOUDFLARE_API_HOST) ||
        matchesHost(model.baseUrl, CLOUDFLARE_AI_GATEWAY_HOST));
}
function getDefaultAttributionHeaders(model, settingsManager) {
    if (!isInstallTelemetryEnabled(settingsManager)) {
        return undefined;
    }
    if (isOpenRouterModel(model)) {
        return {
            "HTTP-Referer": "https://atomic.sh",
            "X-OpenRouter-Title": APP_NAME,
            "X-OpenRouter-Categories": "cli-agent",
        };
    }
    if (isNvidiaNimModel(model)) {
        return {
            "X-BILLING-INVOKE-ORIGIN": "Atomic",
        };
    }
    if (isCloudflareModel(model)) {
        return {
            "User-Agent": APP_NAME,
        };
    }
    return undefined;
}
function getSessionHeaders(model, sessionId) {
    if (!sessionId)
        return undefined;
    if (model.provider !== "opencode" &&
        model.provider !== "opencode-go" &&
        !matchesHost(model.baseUrl, OPENCODE_HOST)) {
        return undefined;
    }
    return { "x-opencode-session": sessionId, "x-opencode-client": APP_NAME };
}
function mergeHeaderSource(merged, headers) {
    if (!headers)
        return;
    for (const [key, value] of Object.entries(headers)) {
        const normalizedKey = key.toLowerCase();
        for (const existingKey of Object.keys(merged)) {
            if (existingKey.toLowerCase() === normalizedKey) {
                delete merged[existingKey];
            }
        }
        // Preserve `null` verbatim: under pi-ai 0.80.2 a `null` header value is the
        // documented suppression signal for a provider/API default header. Collapsing
        // it to `undefined` or dropping it would let pi-ai re-add its own default and
        // silently defeat the caller's suppression request.
        merged[key] = value;
    }
}
export function mergeProviderAttributionHeaders(model, settingsManager, sessionId, ...headerSources) {
    const merged = {};
    mergeHeaderSource(merged, getSessionHeaders(model, sessionId));
    mergeHeaderSource(merged, getDefaultAttributionHeaders(model, settingsManager));
    for (const headers of headerSources) {
        mergeHeaderSource(merged, headers);
    }
    return Object.keys(merged).length > 0 ? merged : undefined;
}
//# sourceMappingURL=provider-attribution.js.map