"use strict";
var command_run_1 = require('./command.run');
var Immutable = require('immutable');
var Rx = require('rx');
var watch_utils_1 = require("./watch.utils");
var watch_resolve_1 = require("./watch.resolve");
var watch_runner_1 = require("./watch.runner");
var reporter_resolve_1 = require("./reporter.resolve");
function execute(trigger) {
    var input = trigger.input, config = trigger.config, reporter = trigger.reporter;
    var topLevelWatchers = watch_utils_1.stripBlacklisted(Object.keys(input.watch));
    if (!topLevelWatchers.length) {
        reporter({
            type: reporter_resolve_1.ReportTypes.NoWatchersAvailable
        });
        return Rx.Observable.just({
            setup: {
                errors: [{ type: reporter_resolve_1.ReportTypes.NoWatchersAvailable }]
            }
        });
    }
    var watchTasks = watch_resolve_1.resolveWatchTasks(topLevelWatchers, trigger);
    var runners = watch_runner_1.createWatchRunners(watchTasks, trigger);
    /**
     * Never continue if any runners are invalid
     */
    if (runners.invalid.length) {
        /**
         * Now log the invalid runners
         */
        runners.invalid.forEach(function (runner) {
            reporter({ type: reporter_resolve_1.ReportTypes.WatchTaskTasksErrors, data: { tasks: runner._tasks.all, runner: runner, config: config } });
        });
        return Rx.Observable.just({
            setup: {
                watchTasks: watchTasks,
                runners: runners,
                errors: [{ type: reporter_resolve_1.ReportTypes.WatchTaskTasksErrors }]
            }
        });
    }
    reporter({ type: reporter_resolve_1.ReportTypes.WatcherNames, data: { runners: runners, trigger: trigger } });
    return Rx.Observable.just({
        setup: {
            watchTasks: watchTasks,
            runners: runners,
            errors: []
        }
    });
}
function handleIncomingWatchersCommand(cli, input, config, reporter) {
    return execute({
        shared: new Rx.BehaviorSubject(Immutable.Map({})),
        cli: cli,
        input: input,
        config: config,
        reporter: reporter,
        type: command_run_1.TriggerTypes.command
    });
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = handleIncomingWatchersCommand;
//# sourceMappingURL=command.watchers.js.map