"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander = require("commander");
const I18n_1 = require("../modules/I18n");
const Logger_1 = require("../modules/Logger");
const Config_1 = require("../modules/Config");
const Auth_1 = require("../modules/Auth");
const inquirer = require("inquirer");
const validateEwizardInstance_1 = require("./utils/validateEwizardInstance");
const ramda_1 = require("ramda");
commander
    .name('wiz config')
    .option('-w, --ewizard <ewizard>', 'set ewizard authorization server', validateEwizardInstance_1.default)
    .option('-r, --reset [key]', 'reset configuration')
    .parse(process.argv);
config(commander)
    .catch(err => Logger_1.default.error(err.message));
function config(command) {
    return __awaiter(this, void 0, void 0, function* () {
        const options = command.opts();
        return run(options);
    });
}
function run(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const isNotNill = ramda_1.complement(ramda_1.isNil);
        return ramda_1.compose(doConfig, ramda_1.filter(isNotNill))(options);
    });
}
function doConfig(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const isNotEmpty = ramda_1.complement(ramda_1.isEmpty);
        const hasReset = ramda_1.has('reset');
        return ramda_1.cond([
            [hasReset, resetConfig],
            [isNotEmpty, writeConfig],
            [ramda_1.T, readConfig],
        ])(options);
    });
}
function resetConfig(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const { reset } = options;
        const isString = ramda_1.is(String);
        if (isString(reset)) {
            yield shouldLogout(reset);
            return doReset(reset);
        }
        const shouldReset = yield confirmReset();
        if (shouldReset.reset) {
            return doReset();
        }
    });
}
function doReset(fields) {
    return __awaiter(this, void 0, void 0, function* () {
        yield Config_1.Config.reset(fields);
        return Logger_1.default.info(I18n_1.default.info('CONFIG_RESET_OK'));
    });
}
function shouldLogout(fields = '') {
    return __awaiter(this, void 0, void 0, function* () {
        if (fields.includes('ewizard')) {
            yield new Auth_1.default().logout(true);
        }
    });
}
function confirmReset() {
    return inquirer.prompt({
        type: 'expand',
        name: 'reset',
        message: 'Reset all custom settings?',
        choices: [{
                key: 'y',
                name: 'reset',
                value: 'true',
            }, {
                key: 'n',
                name: 'do not reset',
                value: '',
            }],
    });
}
function writeConfig(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const data = wrapEwizardUrl(options);
        const auth = new Auth_1.default();
        yield auth.logout(true);
        yield Config_1.Config.set(data);
        printData('%s set to %s', options);
    });
}
function wrapEwizardUrl(data) {
    const setEwizardUrl = ramda_1.over(ramda_1.lensProp('ewizard'), ramda_1.objOf('url'));
    return ramda_1.when(ramda_1.has('ewizard'), setEwizardUrl)(data);
}
function readConfig() {
    return __awaiter(this, void 0, void 0, function* () {
        const config = yield Config_1.Config.get();
        const getEwizardUrl = ramda_1.path(['ewizard', 'url']);
        const print = ramda_1.curry(printData)('%s: %s');
        return ramda_1.compose(print, ramda_1.objOf('ewizard'), getEwizardUrl)(config);
    });
}
function printData(format, data) {
    const ewizardInstance = ramda_1.path(['ewizard'])(data);
    if (ewizardInstance) {
        const print = ramda_1.curry(printValueWithFormat)(format);
        ramda_1.forEachObjIndexed(print, data);
    }
    else {
        printError();
    }
}
function printValueWithFormat(format, value, key) {
    Logger_1.default.info(format, key, value);
}
function printError() {
    Logger_1.default.error(I18n_1.default.error('EWIZARD_INSTANCE_NOT_DEFINED'));
}
//# sourceMappingURL=wiz-config.js.map