"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleShowKey = void 0;
const fs = __importStar(require("fs"));
const docker_1 = require("../../docker");
const docker_key_rotation_1 = require("../../docker/docker-key-rotation");
const ui_1 = require("../../utils/ui");
const options_1 = require("./options");
const KNOWN_FLAGS = ['--host', '--full', '--container-scope', '--banner-only'];
function shouldReadContainerVolume(args) {
    return args.includes('--container-scope') || fs.existsSync('/.dockerenv');
}
async function handleShowKey(args) {
    const bannerOnly = args.includes('--banner-only');
    if (!bannerOnly) {
        await (0, ui_1.initUI)();
    }
    const parsed = (0, options_1.parseDockerTarget)(args, KNOWN_FLAGS);
    const remainingArgs = parsed.remainingArgs.filter((arg) => arg !== '--full' && arg !== '--container-scope' && arg !== '--banner-only');
    const errors = [
        ...parsed.errors,
        ...(0, options_1.collectUnexpectedDockerArgs)(remainingArgs, {
            knownFlags: [],
            maxPositionals: 0,
        }),
    ];
    if (errors.length > 0) {
        console.error((0, ui_1.box)((0, ui_1.fail)(errors.join('\n')), { title: 'Docker', padding: 1 }));
        process.exitCode = 1;
        return;
    }
    const full = args.includes('--full');
    if (!shouldReadContainerVolume(args)) {
        try {
            const output = new docker_1.DockerExecutor().showKey({ host: parsed.host }, full);
            if (output.trim()) {
                console.log(output.trim());
            }
        }
        catch (error) {
            console.error((0, ui_1.box)((0, ui_1.fail)(error instanceof Error ? error.message : String(error)), {
                title: 'Docker',
                padding: 1,
            }));
            process.exitCode = 1;
        }
        return;
    }
    const status = (0, docker_key_rotation_1.getDockerKeyRotationStatus)();
    if (bannerOnly) {
        const banner = (0, docker_key_rotation_1.renderDockerKeyRotationBanner)(status);
        if (banner) {
            console.log(banner);
        }
        return;
    }
    const displayKey = full ? status.apiKey : status.maskedApiKey;
    console.log((0, ui_1.header)('Docker CLIProxy API Key'));
    console.log('');
    console.log((0, ui_1.info)(`API key: ${displayKey ?? '(not configured)'}`));
    if (status.legacyGraceActive && status.legacyGrace) {
        console.log((0, ui_1.info)(`Legacy key: ${status.legacyGrace.legacyKey}`));
        console.log((0, ui_1.info)(`Legacy key expires: ${status.legacyGrace.expiresAt}`));
    }
    else {
        console.log((0, ui_1.info)('Legacy key grace: inactive'));
    }
    if (status.stateCorrupted) {
        console.log((0, ui_1.info)(`State marker was unreadable and will be recreated: ${status.statePath}`));
    }
}
exports.handleShowKey = handleShowKey;
//# sourceMappingURL=show-key-subcommand.js.map