"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 config_storage_1 = require("../../lib/config-storage");
const prompts_1 = require("../../lib/prompts");
const key_router_1 = require("../../config/key-router");
class ConfigRemove extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(ConfigRemove);
        try {
            const storage = new config_storage_1.ConfigStorage();
            const kind = (0, key_router_1.classifyKey)(args.key);
            if (kind === 'unknown') {
                console.log(chalk_1.default.red(`\nUnknown configuration key: '${args.key}'\n`));
                console.log((0, key_router_1.formatKnownKeys)());
                console.log();
                this.exit(1);
            }
            if (kind === 'system') {
                if (args.key.toLowerCase() === 'api-url') {
                    storage.removeApiUrl();
                    console.log(chalk_1.default.green(`\nAPI URL removed`));
                    console.log(chalk_1.default.gray(`Location: ${storage.getConfigPath()}\n`));
                }
                return;
            }
            if (kind === 'preference') {
                const key = args.key;
                if (storage.getPreference(key) === null) {
                    console.log(chalk_1.default.yellow(`\nPreference '${key}' is not set\n`));
                    return;
                }
                storage.removePreference(key);
                console.log(chalk_1.default.green(`\nRemoved preference: ${key}`));
                console.log(chalk_1.default.gray(`Location: ${storage.getConfigPath()}\n`));
                return;
            }
            // provider
            const provider = args.key.toLowerCase();
            const hasToken = storage.hasToken(provider);
            const hasCreds = storage.hasCredentials(provider);
            if (!hasToken && !hasCreds) {
                console.log(chalk_1.default.yellow(`\nProvider '${provider}' is not configured`));
                console.log(chalk_1.default.gray("Use 'flui config list' to see configured providers\n"));
                return;
            }
            if (!flags.force) {
                console.log(chalk_1.default.yellow(`\nWarning: This will remove the credentials for '${provider}'`));
                const confirmed = await (0, prompts_1.confirmByTypingPrompt)(chalk_1.default.gray(`Type '${provider}' to confirm`), provider);
                if (!confirmed) {
                    console.log(chalk_1.default.gray('\nCancelled\n'));
                    return;
                }
            }
            if (hasToken)
                storage.removeToken(provider);
            if (hasCreds)
                storage.removeCredentials(provider);
            console.log(chalk_1.default.green(`\nRemoved configuration for: ${provider}`));
            console.log(chalk_1.default.gray(`Location: ${storage.getConfigPath()}\n`));
        }
        catch (error) {
            console.log(chalk_1.default.red('\nFailed to remove configuration'));
            console.log(chalk_1.default.gray(`Error: ${error instanceof Error ? error.message : String(error)}\n`));
            this.exit(1);
        }
    }
}
ConfigRemove.description = 'Remove a stored preference or an encrypted provider token. Dispatch is schema-driven by key name.';
ConfigRemove.examples = [
    '<%= config.bin %> <%= command.id %> hetzner',
    '<%= config.bin %> <%= command.id %> email',
    '<%= config.bin %> <%= command.id %> scaleway --force',
];
ConfigRemove.args = {
    key: core_1.Args.string({
        required: true,
        description: 'Configuration key (preference name or provider name)',
    }),
};
ConfigRemove.flags = {
    force: core_1.Flags.boolean({
        char: 'f',
        description: 'Skip confirmation prompt',
        default: false,
    }),
};
exports.default = ConfigRemove;
