"use strict";
/**
 * Codex auth command router.
 *
 * Exports runCodexAuth(argv) which routes argv[0] (the subcommand) to
 * the appropriate handler. Returns an exit code (0 = success, non-zero = error).
 *
 * Phase 3 wires this into src/bin/codex-runtime.ts for argv[2]==='auth'.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.runCodexAuth = void 0;
const codex_profile_registry_1 = require("./codex-profile-registry");
const codex_auth_help_1 = require("./codex-auth-help");
const index_1 = require("./commands/index");
const packageJson = require('../../package.json');
/**
 * Route a `ccsx auth <subcommand> [...args]` invocation.
 *
 * @param argv - Arguments after `auth`, e.g. ['create', 'work'] or ['--help']
 * @returns Exit code (0 success, 1 user error, 2+ system error)
 */
async function runCodexAuth(argv) {
    const [subcommand, ...rest] = argv;
    // Help / no-arg
    if (!subcommand || subcommand === '--help' || subcommand === '-h' || subcommand === 'help') {
        (0, codex_auth_help_1.printCodexAuthHelp)();
        return 0;
    }
    // Version passthrough
    if (subcommand === '--version' || subcommand === '-v') {
        process.stdout.write(`ccsx auth ${packageJson.version}\n`);
        return 0;
    }
    const registry = new codex_profile_registry_1.CodexProfileRegistry();
    const ctx = {
        registry,
        version: packageJson.version,
    };
    try {
        switch (subcommand) {
            case 'create':
                await (0, index_1.handleCreateCodex)(ctx, rest);
                return 0;
            case 'login':
                await (0, index_1.handleLoginCodex)(ctx, rest);
                return 0;
            case 'switch':
                await (0, index_1.handleSwitchCodex)(ctx, rest);
                return 0;
            case 'use':
                await (0, index_1.handleUseCodex)(ctx, rest);
                return 0;
            case 'show':
                await (0, index_1.handleShowCodex)(ctx, rest);
                return 0;
            case 'remove':
                await (0, index_1.handleRemoveCodex)(ctx, rest);
                return 0;
            case 'import-default':
                await (0, index_1.handleImportDefaultCodex)(ctx, rest);
                return 0;
            default:
                process.stderr.write(`[X] Unknown command: ${subcommand}\n`);
                process.stderr.write(`    ccsx auth --help\n`);
                return 1;
        }
    }
    catch (err) {
        // Unhandled errors from handlers (e.g. process.exit called inside)
        // These should be rare — handlers use exitWithError() which calls process.exit
        const msg = err instanceof Error ? err.message : String(err);
        process.stderr.write(`[X] Unexpected error in ccsx auth ${subcommand}: ${msg}\n`);
        return 1;
    }
}
exports.runCodexAuth = runCodexAuth;
//# sourceMappingURL=codex-auth-router.js.map