"use strict";
var merge = require('../lodash.custom').merge;
var assign = require('../lodash.custom').assign;
function getModifiedWatchContext(trigger) {
    /**
     * First, unwrap each item. If it has a <pattern> -> <task> syntax
     * then we split it, otherwise just return empty arrays for
     * both patterns and tasks
     */
    var unwrapped = trigger.cli.input.slice(1).map(unwrapShorthand);
    /**
     * Next take any items that were split and
     * generate a fake watch options object
     * @type
     */
    var fakeWatchConfig = unwrapped.reduce(function (acc, item) {
        if (item.tasks.length) {
            acc[item.name] = {
                watchers: [{
                        patterns: item.patterns,
                        tasks: item.tasks
                    }]
            };
        }
        return acc;
    }, {});
    trigger.input = merge({}, trigger.input, {
        watch: fakeWatchConfig
    });
    /**
     * Override the CLI input to include the newly split names
     * @type {*[]}
     */
    trigger.cli.input = unwrapped.map(function (x) { return x.name; });
    return trigger;
}
exports.getModifiedWatchContext = getModifiedWatchContext;
/**
 * take the following:
 *  $ crossbow watch "*.js -> (lint) (unit)"
 *
 *  and convert it into
 *  patterns: ["*.js"]
 *  tasks: ["lint", "unit"]
 */
function unwrapShorthand(incoming, i) {
    var patterns = [];
    var tasks = [];
    if (incoming.indexOf(' -> ') > -1) {
        var split = incoming.split(' -> ').map(function (x) { return x.trim(); });
        patterns = split[0].split(':');
        if (split[1]) {
            var _tasks = split[1].match(/\(.+?\)/g);
            if (_tasks) {
                tasks = _tasks.map(function (x) { return x.slice(1, -1).trim(); });
            }
            else {
                tasks = [split[1]];
            }
        }
        return { patterns: patterns, tasks: tasks, i: i, name: "_shorthand_" + i };
    }
    return { patterns: patterns, tasks: tasks, i: i, name: incoming };
}
exports.unwrapShorthand = unwrapShorthand;
//# sourceMappingURL=watch.shorthand.js.map