"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 key_router_1 = require("../../config/key-router");
const preferences_resolver_1 = require("../../config/preferences-resolver");
class ConfigGet extends core_1.Command {
    async run() {
        const { args } = await this.parse(ConfigGet);
        const kind = (0, key_router_1.classifyKey)(args.key);
        if (kind === 'provider') {
            console.log(chalk_1.default.yellow(`\n'${args.key}' is a provider token; tokens are encrypted and not printable.`));
            console.log(chalk_1.default.gray('Use `flui config list --tokens` instead.\n'));
            return;
        }
        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);
        }
        const resolver = new preferences_resolver_1.PreferencesResolver(new config_storage_1.ConfigStorage());
        const resolved = resolver.resolve(args.key);
        if (resolved.value === null) {
            console.log(chalk_1.default.yellow(`\n'${args.key}' is not set (no value at any layer)\n`));
            return;
        }
        const sourceLabel = chalk_1.default.dim(`(source: ${resolved.source})`);
        console.log(`${chalk_1.default.bold(args.key)} = ${resolved.value}  ${sourceLabel}`);
    }
}
ConfigGet.description = 'Print the resolved value of a preference (and the layer it came from). Provider tokens are not printable for security — use `flui config list` to see which providers are configured.';
ConfigGet.examples = [
    '<%= config.bin %> <%= command.id %> email',
    '<%= config.bin %> <%= command.id %> certificateMode',
];
ConfigGet.args = {
    key: core_1.Args.string({
        required: true,
        description: 'Preference key',
    }),
};
exports.default = ConfigGet;
