"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 RepoDisconnect extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(RepoDisconnect);
        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 });
        const all = await api
            .get('/repositories')
            .catch((err) => {
            this.error(`Failed to look up repositories: ${err.message}`, { exit: 1 });
        });
        const match = all?.find((r) => r.repositoryFullName === args.repo);
        if (!match) {
            this.error(`Repository "${args.repo}" is not connected. Run \`flui repo list\` to see what is.`, { exit: 1 });
        }
        if (!flags.yes) {
            const ok = await (0, prompts_1.confirmPrompt)(`Disconnect ${chalk_1.default.bold(args.repo)} from Flui?`, false);
            if (!ok) {
                console.log(chalk_1.default.dim('\n  Cancelled.\n'));
                return;
            }
        }
        const spinner = (0, ora_1.default)(`Disconnecting ${args.repo}…`).start();
        try {
            await api.delete(`/repositories/${match.id}`);
            spinner.succeed(`Disconnected ${args.repo}`);
        }
        catch (error) {
            spinner.fail('Failed to disconnect repository');
            if (error instanceof api_client_1.ApiError && error.statusCode === 403) {
                console.log(chalk_1.default.yellow('  Admin privileges required for this operation.'));
            }
            else {
                console.log(chalk_1.default.red(`  ${error.message}`));
            }
            this.exit(1);
        }
    }
}
RepoDisconnect.description = 'Disconnect a repository from your Flui account. Requires admin privileges. Does not delete the repository on GitHub.';
RepoDisconnect.examples = [
    '<%= config.bin %> <%= command.id %> acme/my-app',
    '<%= config.bin %> <%= command.id %> acme/my-app --yes',
];
RepoDisconnect.args = {
    repo: core_1.Args.string({
        description: 'Full repository name `owner/repo`',
        required: true,
    }),
};
RepoDisconnect.flags = {
    yes: core_1.Flags.boolean({
        char: 'y',
        description: 'Skip the confirmation prompt',
        default: false,
    }),
};
exports.default = RepoDisconnect;
