"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildRegisterAuthProfileInput = buildRegisterAuthProfileInput;
exports.unwrapRegisteredAuthProfile = unwrapRegisteredAuthProfile;
exports.getCodexHomePathForSavedAuth = getCodexHomePathForSavedAuth;
exports.getClaudeConfigPathForSavedAuth = getClaudeConfigPathForSavedAuth;
exports.defaultRunCodexLogin = defaultRunCodexLogin;
exports.defaultRunClaudeLogin = defaultRunClaudeLogin;
const node_child_process_1 = require("node:child_process");
const codex_cli_1 = require("../auth-profiles/codex-cli");
const claude_cli_1 = require("../auth-profiles/claude-cli");
const definitions_1 = require("../auth-profiles/definitions");
const command_1 = require("../core/command");
const catalog_1 = require("../i18n/catalog");
function buildRegisterAuthProfileInput(input) {
    if (input.provider === "codex") {
        return input.method === "env-var"
            ? {
                kind: "codex-env-var",
                envVarName: definitions_1.CODEX_API_KEY_ENV_VAR_NAME,
            }
            : {
                kind: "codex-home",
                codexLoginMethod: input.method,
            };
    }
    if (input.method === "config") {
        return {
            kind: "claude-cli",
        };
    }
    if (input.method === "api-key") {
        return {
            kind: "claude-api-key",
        };
    }
    return {
        kind: "claude-env-var",
        envVarName: definitions_1.CLAUDE_API_KEY_ENV_VAR_NAME,
    };
}
function unwrapRegisteredAuthProfile(result, locale) {
    switch (result.kind) {
        case "added":
        case "noop":
            return result.profile;
        case "conflict":
            throw new Error((0, catalog_1.translate)(locale, "auth.command.setup.registrationConflict"));
    }
}
function getCodexHomePathForSavedAuth(profile, locale) {
    if (profile.kind !== "codex-home") {
        throw new Error((0, catalog_1.translate)(locale, "auth.command.setup.expectedCodex"));
    }
    return profile.codexHomePath;
}
function getClaudeConfigPathForSavedAuth(profile, locale) {
    if (profile.kind !== "claude-cli") {
        throw new Error((0, catalog_1.translate)(locale, "auth.command.setup.expectedClaude"));
    }
    return profile.claudeConfigPath;
}
async function defaultRunCodexLogin(codexHome, method, locale = "en", apiKey) {
    const env = (0, codex_cli_1.buildCodexHomeAuthEnvironment)(process.env, codexHome);
    await (0, command_1.runCommand)({
        command: "codex",
        args: ["--version"],
        env,
        locale,
    });
    await new Promise((resolve, reject) => {
        const args = method === "device-auth"
            ? ["login", "--device-auth"]
            : method === "api-key"
                ? ["login", "--with-api-key"]
                : ["login"];
        if (method === "api-key" && !apiKey?.trim()) {
            reject(new Error((0, catalog_1.translate)(locale, "auth.command.setup.prompt.apiKeyRequired")));
            return;
        }
        const child = (0, node_child_process_1.spawn)("codex", args, {
            stdio: method === "api-key" ? ["pipe", "inherit", "inherit"] : "inherit",
            env,
        });
        if (method === "api-key") {
            child.stdin?.end(`${apiKey?.trim() ?? ""}\n`);
        }
        child.on("error", reject);
        child.on("exit", (code) => {
            if (code === 0) {
                resolve();
                return;
            }
            reject(new Error((0, catalog_1.translate)(locale, "auth.command.setup.codexLoginFailed", {
                code: code ?? 1,
            })));
        });
    });
}
async function defaultRunClaudeLogin(claudeConfigPath, locale = "en") {
    const env = (0, claude_cli_1.buildClaudeCliAuthEnvironment)(process.env, claudeConfigPath);
    await (0, command_1.runCommand)({
        command: "claude",
        args: ["--version"],
        env,
        locale,
    });
    await new Promise((resolve, reject) => {
        const child = (0, node_child_process_1.spawn)("claude", ["auth", "login", "--claudeai"], {
            stdio: "inherit",
            env,
        });
        child.on("error", reject);
        child.on("exit", (code) => {
            if (code === 0) {
                resolve();
                return;
            }
            reject(new Error((0, catalog_1.translate)(locale, "auth.command.setup.claudeLoginFailed", {
                code: code ?? 1,
            })));
        });
    });
}
