"use strict";
var cli_commands_1 = require("./cli.commands");
var _ = require('../lodash.custom');
var cli_parse_1 = require("./cli.parse");
function default_1(args) {
    var command = args[0];
    var match = getCommand(command, cli_commands_1.commands);
    if (!match.length) {
        // first look if the user provided a --version flag
        var cli_1 = cli_parse_1.default(['no-command'].concat(args), require('../opts/global-common.json'));
        if (cli_1.flags.version) {
            return { cli: cli_1, execute: false, output: [require('../package.json').version] };
        }
        var commandOptions_1 = cli_commands_1.commands['run'].opts.map(require);
        var opts_1 = _.merge.apply(_, [{}].concat(commandOptions_1));
        var cli2 = cli_parse_1.default(['run'].concat(args), opts_1);
        /**
         * If there was additional input, try to run a task
         */
        if (cli2.input.length > 1) {
            return { cli: cli2, execute: true, output: [] };
        }
        return { cli: cli2, execute: false, output: [printHelp(cli_commands_1.commands)] };
    }
    var commandName = match[0];
    var commandOptions = cli_commands_1.commands[commandName].opts.map(require);
    var opts = _.merge.apply(_, [{}].concat(commandOptions));
    var cli = cli_parse_1.default(args, opts);
    /**
     * Here, the user gave the --help flag along with a valid
     * command. So we show command-specific help
     */
    if (cli.flags.help) {
        return { cli: cli, execute: false, output: [cli_commands_1.commands[match[0]].help] };
    }
    return { cli: cli, execute: true, output: [] };
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
function printHelp(commands) {
    return "Usage: crossbow [command] [..args] [OPTIONS]\n\n{bold:Crossbow Commands:} \n\n" + cli_commands_1.twoColFromJson(commands, 'description') + "\n\n{bold:Example: Run the task 'build-js'}\n\n    $ crossbow run build-js\n    \n{bold:Example: Run the tasks build-css and build-js in sequence}\n\n    $ crossbow run build-css build-js\n        \n{bold:For more detailed help, use the command name + the --help flag}\n\n    $ crossbow run --help\n    $ crossbow init --help\n";
}
function getCommand(incoming, commands) {
    return Object.keys(commands).reduce(function (acc, item) {
        var selected = commands[item];
        /**
         * A direct match - this means the typed command matches
         * an command name exactly.
         *
         * eg:
         *  $ crossbow run
         *
         * -> ['run']
         */
        if (item === incoming) {
            return acc.concat(item);
        }
        /**
         * An alias match is when a short-hand command was given
         * and it existed in the 'alias' array for a command.
         *
         * eg:
         *
         *  $ crossbow run
         *
         * commands:
         *
         * run: { alias:['run', 'r'] }
         *
         * -> ['run']
         *
         */
        if (selected.alias && selected.alias.indexOf(incoming) > -1) {
            return acc.concat(item);
        }
        return acc;
    }, []);
}
exports.getCommand = getCommand;
//# sourceMappingURL=cli.js.map