"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("reflect-metadata");
const core_1 = require("@oclif/core");
const inquirer = require("inquirer");
const _ = require("lodash");
const command_base_tenantless_1 = require("../command-base-tenantless");
class Cli extends command_base_tenantless_1.CommandBaseTenantless {
    run() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                try {
                    this.bootstrap();
                    const { args, flags } = yield this.parse(Cli);
                    let kvps = flags.set;
                    let avails = [
                        "authApiBaseUrl",
                        "toyhaulerApiBaseUrl",
                        "verbosity",
                        "addBranchNameToPrereleases"
                    ];
                    if (!kvps) {
                        let result = yield inquirer.prompt({ type: 'list', name: 'key', message: 'Available settings:', choices: avails });
                        if (result) {
                            let key = result.key;
                            let existingVal = _.get(this._cliCfg, key);
                            let valResult = yield inquirer.prompt({ type: 'input', name: 'val', message: 'Enter a value:', default: existingVal });
                            if (valResult) {
                                let val = valResult.val;
                                kvps = [`${key}=${val}`]; // Yeah yeah stupid data structure approach to this thing.  needs to be fixed.  AAAAAND THIS WHOLE THING NEEDS SOME UX FINESSE.
                            }
                        }
                    }
                    if (kvps && kvps.length > 0) {
                        let isDirty = false;
                        let keys = [];
                        let badKeys = [];
                        for (let index in kvps) {
                            let kv = kvps[index].split("=");
                            let key = kv[0];
                            let val = (kv.length > 0) ? kv[1] : undefined;
                            switch (key) {
                                case "addBranchNameToPrereleases":
                                    this._cliCfg.addBranchNameToPrereleases = ((val === null || val === void 0 ? void 0 : val.toLowerCase()) === 'true');
                                    isDirty = true;
                                    keys.push(key);
                                    break;
                                case "authApiBaseUrl":
                                    this._cliCfg.authApiBaseUrl = val;
                                    isDirty = true;
                                    keys.push(key);
                                    break;
                                case "toyhaulerApiBaseUrl":
                                    this._cliCfg.codiacApiBaseUrl = val;
                                    isDirty = true;
                                    keys.push(key);
                                    break;
                                case "verbosity":
                                    if (val && ["normal", "detail", "debug"].includes(val.toLowerCase())) {
                                        this._cliCfg.verbosity = val.toLowerCase();
                                        isDirty = true;
                                        keys.push(key);
                                    }
                                    else {
                                        throw new Error(`Invalid value for [${key}]: must be "true" or "false".`);
                                    }
                                    break;
                                default:
                                    badKeys.push(key);
                                    break;
                            }
                        }
                        if (badKeys.length > 0) {
                            throw new Error(`Did not recognize settings [${badKeys.join(', ')}].`);
                        }
                        if (isDirty) {
                            yield this._cfgRepo.upsert(this._cliCfg);
                            this._ui.userMessage(`Saved new values for [${keys.map(k => this._styler.colorGood(k)).join(', ')}].`);
                        }
                        else {
                            this._ui.userMessage(`No values to write: No work was done.`);
                        }
                    }
                    resolve();
                }
                catch (err) {
                    reject(err);
                }
            }));
        });
    }
}
exports.default = Cli;
Cli.description = 'Applies settings to the Codiac CLI itself (scoped to the local machine).';
Cli.flags = {
    help: core_1.Flags.help({ char: 'h', description: "Show the help docs for this command." }),
    set: core_1.Flags.string({ char: 's', required: false, multiple: true, description: 'A key=value expression for the setting to apply.  Leave this argument out of your call to get a list of the available settings keys and their types.' }),
};
Cli.args = {};
//# sourceMappingURL=cli.js.map