// NEVER convert to top-level imports - breaks browser/Vite builds (web-ui)
let _existsSync = null;
let _homedir = null;
let _join = null;
// Eagerly load in Node.js/Bun environment only
if (typeof process !== 'undefined' && (process.versions?.node || process.versions?.bun)) {
    import('node:fs').then((m) => {
        _existsSync = m.existsSync;
    });
    import('node:os').then((m) => {
        _homedir = m.homedir;
    });
    import('node:path').then((m) => {
        _join = m.join;
    });
}
let cachedVertexAdcCredentialsExists = null;
function hasVertexAdcCredentials() {
    if (cachedVertexAdcCredentialsExists === null) {
        // If node modules haven't loaded yet (async import race at startup),
        // return false WITHOUT caching so the next call retries once they're ready.
        // Only cache false permanently in a browser environment where fs is never available.
        if (!_existsSync || !_homedir || !_join) {
            const isNode = typeof process !== 'undefined' && (process.versions?.node || process.versions?.bun);
            if (!isNode) {
                // Definitively in a browser — safe to cache false permanently
                cachedVertexAdcCredentialsExists = false;
            }
            return false;
        }
        // Check GOOGLE_APPLICATION_CREDENTIALS env var first (standard way)
        const gacPath = process.env.GOOGLE_APPLICATION_CREDENTIALS;
        if (gacPath) {
            cachedVertexAdcCredentialsExists = _existsSync(gacPath);
        }
        else {
            // Fall back to default ADC path (lazy evaluation)
            cachedVertexAdcCredentialsExists = _existsSync(_join(_homedir(), '.config', 'gcloud', 'application_default_credentials.json'));
        }
    }
    return cachedVertexAdcCredentialsExists;
}
export function getEnvApiKey(provider) {
    if (provider === 'anthropic') {
        return process.env.ANTHROPIC_API_KEY;
    }
    // Vertex AI uses Application Default Credentials, not API keys.
    // Auth is configured via `gcloud auth application-default login`.
    if (provider === 'google-vertex') {
        const hasCredentials = hasVertexAdcCredentials();
        const hasProject = !!(process.env.GOOGLE_CLOUD_PROJECT || process.env.GCLOUD_PROJECT);
        const hasLocation = !!process.env.GOOGLE_CLOUD_LOCATION;
        if (hasCredentials && hasProject && hasLocation) {
            return '<authenticated>';
        }
    }
    const envMap = {
        openai: 'OPENAI_API_KEY',
        'azure-openai-responses': 'AZURE_OPENAI_API_KEY',
        google: 'GEMINI_API_KEY',
        groq: 'GROQ_API_KEY',
        cerebras: 'CEREBRAS_API_KEY',
        xai: 'XAI_API_KEY',
        openrouter: 'OPENROUTER_API_KEY',
        'vercel-ai-gateway': 'AI_GATEWAY_API_KEY',
        zai: 'ZAI_API_KEY',
        minimax: 'MINIMAX_API_KEY',
        'minimax-cn': 'MINIMAX_CN_API_KEY',
        huggingface: 'HF_TOKEN',
        opencode: 'OPENCODE_API_KEY',
        'kimi-coding': 'KIMI_API_KEY'
    };
    const envVar = envMap[provider];
    return envVar ? process.env[envVar] : undefined;
}
//# sourceMappingURL=env-api-keys.js.map