"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cli_core_1 = require("@igniteui/cli-core");
const command = {
    // tslint:disable:object-literal-sort-keys
    command: "config",
    desc: "gets, sets or adds a configuration property",
    builder: yargs => {
        yargs.command({
            command: "get <property>",
            desc: "Gets a configuration property",
            builder: {
                property: {
                    describe: "Config property to get",
                    type: "string"
                }
            },
            handler: command.getHandler
        }).command({
            command: "set <property> <value>",
            desc: "Sets a configuration property",
            builder: {
                property: {
                    describe: "Config property to set",
                    type: "string"
                },
                value: {
                    describe: "New value for the property",
                    type: "string"
                }
            },
            handler: command.setHandler
        }).command({
            command: "add <property> <value>",
            desc: "Adds a value to an existing configuration array",
            builder: {
                property: {
                    describe: "Config property to add to",
                    type: "string"
                },
                value: {
                    describe: "New value to add",
                    type: "string"
                }
            },
            handler: argv => {
                command.addHandler(argv);
            }
        }).option("global", {
            alias: "g",
            type: "boolean",
            global: true,
            describe: "Specify if the global configuration should be used"
        })
            // at least one command is required
            .demandCommand(1, "Please select command");
    },
    // tslint:enable:object-literal-sort-keys
    getHandler(argv) {
        cli_core_1.GoogleAnalytics.post({
            t: "screenview",
            cd: "Config"
        });
        if (!argv.global && !cli_core_1.ProjectConfig.hasLocalConfig()) {
            cli_core_1.Util.error("No configuration file found in this folder!", "red");
            return;
        }
        const config = cli_core_1.ProjectConfig.getConfig(argv.global);
        cli_core_1.GoogleAnalytics.post({
            t: "event",
            ec: "$ig config",
            el: "subcommand: get",
            ea: `property to get: ${argv.property}, is global: ${argv.global}`,
            cd9: "get",
            cd11: !!config.skipGit,
            cd12: !!argv.global
        });
        if (config[argv.property] !== undefined) {
            cli_core_1.Util.log(config[argv.property]);
        }
        else {
            cli_core_1.Util.error(`No value found for "${argv.property}" property`, "red");
        }
    },
    setHandler(argv) {
        cli_core_1.GoogleAnalytics.post({
            t: "screenview",
            cd: "Config"
        });
        let config;
        if (argv.global) {
            config = cli_core_1.ProjectConfig.globalConfig();
        }
        else {
            if (!cli_core_1.ProjectConfig.hasLocalConfig()) {
                cli_core_1.Util.error("No configuration file found in this folder!", "red");
                return;
            }
            config = cli_core_1.ProjectConfig.localConfig();
        }
        const validationResult = cli_core_1.ProjectConfig.validateProperty(argv.property, argv.value);
        if (!validationResult.valid) {
            cli_core_1.Util.error(validationResult.message, "red");
            return;
        }
        cli_core_1.GoogleAnalytics.post({
            t: "event",
            ec: "$ig config",
            el: "subcommand: set",
            ea: `property to set: ${argv.property}, value to set: ${argv.value}, is global: ${argv.global}`,
            cd9: "set",
            cd11: !!config.skipGit,
            cd12: !!argv.global
        });
        config[argv.property] = validationResult.value;
        cli_core_1.ProjectConfig.setConfig(config, argv.global);
        cli_core_1.Util.log(`Property "${argv.property}" set.`);
    },
    addHandler(argv) {
        if (!argv.skipAnalytics) {
            cli_core_1.GoogleAnalytics.post({
                t: "screenview",
                cd: "Config"
            });
        }
        let config;
        if (argv.global) {
            config = cli_core_1.ProjectConfig.globalConfig();
        }
        else {
            if (!cli_core_1.ProjectConfig.hasLocalConfig()) {
                cli_core_1.Util.error("No configuration file found in this folder!", "red");
                return;
            }
            config = cli_core_1.ProjectConfig.localConfig();
        }
        if (!config[argv.property]) {
            config[argv.property] = [];
        }
        else if (!Array.isArray(config[argv.property])) {
            cli_core_1.Util.error(`Configuration property "${argv.property}" is not an array, use config set instead.`, "red");
            return;
        }
        if (config[argv.property].indexOf(argv.value) !== -1) {
            cli_core_1.Util.log(`Value already exists in "${argv.property}".`);
            return;
        }
        config[argv.property].push(argv.value);
        cli_core_1.ProjectConfig.setConfig(config, argv.global);
        if (!argv.skipAnalytics) {
            cli_core_1.GoogleAnalytics.post({
                t: "event",
                ec: "$ig config",
                el: "subcommand: add",
                ea: `property to add: ${argv.property}, value to add: ${argv.value}, is global: ${argv.global}`,
                cd9: "add",
                cd11: !!config.skipGit,
                cd12: !!argv.global
            });
        }
        cli_core_1.Util.log(`Property "${argv.property}" updated.`);
    }
};
exports.default = command;
