"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildAuthProfileEnvironment = buildAuthProfileEnvironment;
exports.readAuthProfileEnvVar = readAuthProfileEnvVar;
const claude_cli_1 = require("./claude-cli");
const credential_store_1 = require("./credential-store");
const definitions_1 = require("./definitions");
const codex_cli_1 = require("./codex-cli");
function buildAuthProfileEnvironment(profile, baseEnv, credentialStore) {
    switch (profile.kind) {
        case "codex-home":
            return (0, codex_cli_1.buildCodexHomeAuthEnvironment)(baseEnv, profile.codexHomePath);
        case "codex-env-var": {
            const value = readAuthProfileEnvVar(profile, baseEnv);
            return value
                ? (0, codex_cli_1.buildCodexEnvVarAuthEnvironment)(baseEnv, profile.envVarName, value)
                : null;
        }
        case "claude-cli":
            return (0, claude_cli_1.buildClaudeCliAuthEnvironment)(baseEnv, profile.claudeConfigPath);
        case "claude-api-key": {
            let value = null;
            try {
                value = (0, credential_store_1.readStoredCredential)(profile, credentialStore);
            }
            catch {
                value = null;
            }
            return value
                ? (0, claude_cli_1.buildClaudeEnvVarAuthEnvironment)(baseEnv, definitions_1.CLAUDE_API_KEY_ENV_VAR_NAME, value)
                : null;
        }
        case "claude-env-var": {
            const value = readAuthProfileEnvVar(profile, baseEnv);
            return value
                ? (0, claude_cli_1.buildClaudeEnvVarAuthEnvironment)(baseEnv, profile.envVarName, value)
                : null;
        }
    }
}
function readAuthProfileEnvVar(profile, baseEnv) {
    return baseEnv[profile.envVarName]?.trim() || null;
}
