"use strict";
/**
 * codex-auth switch command.
 * Sets the persistent default Codex profile in the registry.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleSwitchCodex = void 0;
const ui_1 = require("../../utils/ui");
const errors_1 = require("../../errors");
const exit_codes_1 = require("../../errors/exit-codes");
const types_1 = require("./types");
async function handleSwitchCodex(ctx, args) {
    await (0, ui_1.initUI)();
    const parsed = (0, types_1.parseArgs)(args);
    (0, types_1.rejectUnsupportedOptions)(parsed, 'ccsx auth switch <name>');
    const { profileName } = parsed;
    if (!profileName) {
        console.log('Usage: ccsx auth switch <name>');
        (0, errors_1.exitWithError)('Profile name required', exit_codes_1.ExitCode.PROFILE_ERROR);
        return;
    }
    const nameError = (0, types_1.getProfileNameError)(profileName);
    if (nameError) {
        (0, errors_1.exitWithError)(nameError, exit_codes_1.ExitCode.PROFILE_ERROR);
        return;
    }
    const { registry } = ctx;
    if (!registry.hasProfile(profileName)) {
        const available = registry.listProfiles();
        const availableStr = available.length > 0 ? available.join(', ') : '<none>';
        (0, errors_1.exitWithError)(`Profile not found: ${profileName}. Available: ${availableStr}`, exit_codes_1.ExitCode.PROFILE_ERROR);
        return;
    }
    registry.setDefault(profileName);
    const meta = registry.getProfile(profileName);
    const emailStr = meta.email ? `\n    Email: ${meta.email}` : '';
    const planStr = meta.plan_type ? `\n    Plan : ${meta.plan_type}` : '';
    console.log((0, ui_1.ok)(`Default Codex profile: ${profileName}`));
    if (emailStr)
        process.stdout.write(emailStr + '\n');
    if (planStr)
        process.stdout.write(planStr + '\n');
    console.log('');
    console.log('[i] This is the persistent default. To use a different profile in the');
    console.log(`    current shell only, run: eval "$(ccsx auth use <other>)"`);
}
exports.handleSwitchCodex = handleSwitchCodex;
//# sourceMappingURL=switch-command.js.map