import { getCliDefinition } from '../cli-registry.js';
export const RELAY_PROXY_URL_ENV = 'RELAY_LLM_PROXY';
export const RELAY_PROXY_URL_ENV_ALIAS = 'RELAY_LLM_PROXY_URL';
export const RELAY_PROXY_TOKEN_ENV = 'CREDENTIAL_PROXY_TOKEN';
export const RELAY_PROXY_TOKEN_ENV_ALIAS = 'RELAY_LLM_PROXY_TOKEN';
const OPENAI_COMPATIBLE_BINDINGS = [
    { baseUrlVar: 'OPENAI_BASE_URL', apiKeyVar: 'OPENAI_API_KEY' },
];
const ANTHROPIC_BINDINGS = [
    { baseUrlVar: 'ANTHROPIC_BASE_URL', apiKeyVar: 'ANTHROPIC_API_KEY' },
];
const AIDER_BINDINGS = [
    { baseUrlVar: 'OPENAI_API_BASE', apiKeyVar: 'OPENAI_API_KEY' },
];
const GEMINI_BINDINGS = [
    { baseUrlVar: 'GOOGLE_API_BASE', apiKeyVar: 'GOOGLE_API_KEY' },
];
const GENERIC_FALLBACK_BINDINGS = [
    ...OPENAI_COMPATIBLE_BINDINGS,
    ...ANTHROPIC_BINDINGS,
];
const STRIPPED_API_KEY_VARS = [
    'OPENAI_API_KEY',
    'ANTHROPIC_API_KEY',
    'OPENROUTER_API_KEY',
    'GOOGLE_API_KEY',
    'OPENAI_BASE_URL',
    'ANTHROPIC_BASE_URL',
    'OPENAI_API_BASE',
    'GOOGLE_API_BASE',
];
const CLI_ALIASES = {
    agent: 'cursor',
    'cursor-agent': 'cursor',
};
export const DEFAULT_PROXY_ENV_REGISTRY = {
    claude: ANTHROPIC_BINDINGS,
    codex: OPENAI_COMPATIBLE_BINDINGS,
    opencode: OPENAI_COMPATIBLE_BINDINGS,
    aider: AIDER_BINDINGS,
    gemini: GEMINI_BINDINGS,
    goose: OPENAI_COMPATIBLE_BINDINGS,
    droid: OPENAI_COMPATIBLE_BINDINGS,
    cursor: OPENAI_COMPATIBLE_BINDINGS,
};
function normalizeCli(cli) {
    const baseCli = cli.includes(':') ? cli.split(':')[0] : cli;
    return CLI_ALIASES[baseCli] ?? baseCli;
}
function buildProxyEnv(bindings, proxyUrl, proxyToken) {
    return bindings.reduce((env, binding) => {
        env[binding.baseUrlVar] = proxyUrl;
        env[binding.apiKeyVar] = proxyToken;
        return env;
    }, {});
}
export function createProxyEnvResolver(registry = DEFAULT_PROXY_ENV_REGISTRY) {
    return (cli, proxyUrl, proxyToken) => {
        const normalizedCli = normalizeCli(cli);
        const bindings = registry[normalizedCli];
        if (bindings) {
            return buildProxyEnv(bindings, proxyUrl, proxyToken);
        }
        const knownCli = getCliDefinition(normalizedCli);
        const warningPrefix = knownCli ? 'No proxy env registry entry' : 'Unknown CLI';
        console.warn(`[proxy-env] ${warningPrefix} for "${normalizedCli}". ` +
            'Falling back to generic OpenAI/Anthropic proxy env overrides.');
        return buildProxyEnv(GENERIC_FALLBACK_BINDINGS, proxyUrl, proxyToken);
    };
}
export const resolveProxyEnv = createProxyEnvResolver();
export function resolveProxyUrlFromEnv(env = process.env) {
    return env[RELAY_PROXY_URL_ENV] ?? env[RELAY_PROXY_URL_ENV_ALIAS];
}
export function resolveProxyTokenFromEnv(env = process.env) {
    return env[RELAY_PROXY_TOKEN_ENV] ?? env[RELAY_PROXY_TOKEN_ENV_ALIAS];
}
export function buildNormalizedProxyEnv(proxyUrl, proxyToken) {
    return {
        [RELAY_PROXY_URL_ENV]: proxyUrl,
        [RELAY_PROXY_URL_ENV_ALIAS]: proxyUrl,
        [RELAY_PROXY_TOKEN_ENV]: proxyToken,
        [RELAY_PROXY_TOKEN_ENV_ALIAS]: proxyToken,
    };
}
export function getStrippedApiKeyVars() {
    return [...STRIPPED_API_KEY_VARS];
}
export function isProxyEnabled(agentDef, swarmConfig) {
    return Boolean(agentDef?.credentials?.proxy && swarmConfig?.credentialProxy);
}
//# sourceMappingURL=proxy-env.js.map