"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyWebSearchApiKeyUpdates = exports.getWebSearchApiKeyStates = exports.WEBSEARCH_API_KEY_PROVIDERS = void 0;
const sensitive_keys_1 = require("../sensitive-keys");
const config_loader_facade_1 = require("../../config/config-loader-facade");
exports.WEBSEARCH_API_KEY_PROVIDERS = {
    exa: {
        envVar: 'EXA_API_KEY',
        alternateEnvVar: 'CCS_WEBSEARCH_EXA_API_KEY',
    },
    tavily: {
        envVar: 'TAVILY_API_KEY',
        alternateEnvVar: 'CCS_WEBSEARCH_TAVILY_API_KEY',
    },
    brave: {
        envVar: 'BRAVE_API_KEY',
        alternateEnvVar: 'CCS_WEBSEARCH_BRAVE_API_KEY',
    },
};
function getTrimmedEnvValue(env, names) {
    for (const name of names) {
        const value = env[name]?.trim();
        if (value) {
            return value;
        }
    }
    return undefined;
}
function resolveSource(hasGlobalEnvValue, hasProcessEnvValue) {
    if (hasGlobalEnvValue && hasProcessEnvValue) {
        return 'both';
    }
    if (hasGlobalEnvValue) {
        return 'global_env';
    }
    if (hasProcessEnvValue) {
        return 'process_env';
    }
    return 'none';
}
function getWebSearchApiKeyStates() {
    const globalEnvConfig = (0, config_loader_facade_1.getGlobalEnvConfig)();
    const storedEnv = globalEnvConfig.env ?? {};
    return Object.entries(exports.WEBSEARCH_API_KEY_PROVIDERS).reduce((acc, [providerId, descriptor]) => {
        const names = [descriptor.envVar, descriptor.alternateEnvVar];
        const globalEnvValue = getTrimmedEnvValue(storedEnv, names);
        const processEnvValue = getTrimmedEnvValue(process.env, names);
        const configured = Boolean(globalEnvValue || processEnvValue);
        const available = Boolean(processEnvValue || (globalEnvConfig.enabled && globalEnvValue));
        const visibleValue = globalEnvValue || processEnvValue;
        acc[providerId] = {
            envVar: descriptor.envVar,
            configured,
            available,
            source: resolveSource(Boolean(globalEnvValue), Boolean(processEnvValue)),
            maskedValue: visibleValue ? (0, sensitive_keys_1.maskSensitiveValue)(visibleValue) : undefined,
        };
        return acc;
    }, {});
}
exports.getWebSearchApiKeyStates = getWebSearchApiKeyStates;
function applyWebSearchApiKeyUpdates(env, apiKeys) {
    if (!apiKeys) {
        return env;
    }
    const nextEnv = { ...env };
    for (const [providerId, rawValue] of Object.entries(apiKeys)) {
        if (rawValue === undefined) {
            continue;
        }
        const descriptor = exports.WEBSEARCH_API_KEY_PROVIDERS[providerId];
        delete nextEnv[descriptor.alternateEnvVar];
        const normalizedValue = typeof rawValue === 'string' ? rawValue.trim() : '';
        if (!normalizedValue) {
            delete nextEnv[descriptor.envVar];
            continue;
        }
        nextEnv[descriptor.envVar] = normalizedValue;
    }
    return nextEnv;
}
exports.applyWebSearchApiKeyUpdates = applyWebSearchApiKeyUpdates;
//# sourceMappingURL=provider-secrets.js.map