"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const api_client_1 = require("../../../lib/api-client");
const config_storage_1 = require("../../../lib/config-storage");
const prompts_1 = require("../../../lib/prompts");
class IntegrationGhcrPatRevoke extends core_1.Command {
    async run() {
        const { flags } = await this.parse(IntegrationGhcrPatRevoke);
        const configStorage = new config_storage_1.ConfigStorage();
        const apiUrl = configStorage.getApiUrlOrThrow();
        const apiKey = configStorage.getApiKey();
        if (!apiKey) {
            this.error('Not logged in. Run `flui auth login` first.', { exit: 1 });
        }
        const api = new api_client_1.ApiClient({ baseUrl: apiUrl, apiKey });
        if (!flags.yes) {
            const ok = await (0, prompts_1.confirmPrompt)('Revoke the stored GHCR PAT? Future `flui deploy` will fail until a new one is set.', false);
            if (!ok) {
                console.log(chalk_1.default.dim('\n  Cancelled.\n'));
                return;
            }
        }
        const spinner = (0, ora_1.default)('Revoking GHCR PAT…').start();
        try {
            await api.delete('/repositories/github-app/packages-pat');
            spinner.succeed('GHCR PAT revoked');
            console.log('');
            console.log(chalk_1.default.yellow('  Remember to also delete it on GitHub at https://github.com/settings/tokens'));
            console.log('');
        }
        catch (error) {
            spinner.fail('Failed to revoke GHCR PAT');
            if (error instanceof api_client_1.ApiError && error.statusCode === 404) {
                console.log(chalk_1.default.dim('  No PAT was configured.'));
            }
            else {
                console.log(chalk_1.default.red(`  ${error.message}`));
            }
            this.exit(1);
        }
    }
}
IntegrationGhcrPatRevoke.description = 'Revoke the GHCR PAT stored in Flui. Does NOT revoke the token on GitHub — do that separately at https://github.com/settings/tokens. After revoking, `flui deploy` will fail until a new PAT is set.';
IntegrationGhcrPatRevoke.examples = [
    '<%= config.bin %> <%= command.id %>',
    '<%= config.bin %> <%= command.id %> --yes',
];
IntegrationGhcrPatRevoke.flags = {
    yes: core_1.Flags.boolean({
        char: 'y',
        description: 'Skip the confirmation prompt',
        default: false,
    }),
};
exports.default = IntegrationGhcrPatRevoke;
