"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.authCommand = void 0;
const chalk_1 = __importDefault(require("chalk"));
const helpers_1 = require("./helpers");
const context_1 = require("./context");
exports.authCommand = {
    command: 'auth <command>',
    describe: 'Manage authentication',
    builder: (yargs) => yargs
        .command('status', 'Show current login state (JSON-friendly)', () => { }, (argv) => {
        void (async () => {
            const { getAuthStatus } = await import('../lib/agent-ops.js');
            const { resolveMode } = await import('../lib/mode-config.js');
            const { jsonOutput } = resolveMode({
                json: argv.json,
                human: argv.human,
                isTTY: Boolean(process.stdout.isTTY),
            });
            const result = getAuthStatus();
            if (jsonOutput) {
                process.stdout.write(JSON.stringify(result) + '\n');
            }
            else if (result.loggedIn && result.user) {
                (0, helpers_1.getUI)().log.success(`Logged in as ${chalk_1.default.bold(`${result.user.firstName} ${result.user.lastName}`)} <${result.user.email}>`);
                (0, helpers_1.getUI)().note(`Zone: ${result.user.zone}`);
                if (result.tokenExpiresAt) {
                    (0, helpers_1.getUI)().note(`Token expires: ${result.tokenExpiresAt}`);
                }
            }
            else {
                (0, helpers_1.getUI)().log.warn(`Not logged in. Run \`${context_1.CLI_INVOCATION} login\` to authenticate.`);
            }
            process.exit(result.loggedIn ? 0 : helpers_1.ExitCode.AUTH_REQUIRED);
        })();
    })
        .command('token', 'Print the stored OAuth access token to stdout', () => { }, (argv) => {
        void (async () => {
            const { getAuthToken } = await import('../lib/agent-ops.js');
            const { resolveMode } = await import('../lib/mode-config.js');
            const { jsonOutput } = resolveMode({
                json: argv.json,
                human: argv.human,
                isTTY: Boolean(process.stdout.isTTY),
            });
            const result = getAuthToken();
            if (!result.token) {
                if (jsonOutput) {
                    process.stdout.write(JSON.stringify({
                        error: 'not logged in',
                        code: 'AUTH_REQUIRED',
                    }) + '\n');
                }
                else {
                    (0, helpers_1.getUI)().log.error(`Not logged in. Run \`${context_1.CLI_INVOCATION} login\` first.`);
                }
                process.exit(helpers_1.ExitCode.AUTH_REQUIRED);
            }
            if (jsonOutput) {
                process.stdout.write(JSON.stringify(result) + '\n');
            }
            else {
                // Raw token on stdout so `$(amplitude-wizard auth token)` works
                process.stdout.write(result.token + '\n');
            }
            process.exit(0);
        })();
    })
        .demandCommand(1, 'You must specify a subcommand (status or token)')
        .help(),
    // The handler is unused for command groups (yargs dispatches to the
    // matching subcommand). Kept as a no-op to satisfy the CommandModule type.
    handler: () => { },
};
