"use strict";
var command_run_1 = require("./command.run");
var reporter_resolve_1 = require("./reporter.resolve");
var task_resolve_1 = require("./task.resolve");
var Rx = require('rx');
var command_run_context_1 = require("./command.run.context");
var debug = require('debug')('cb:command.run.execute');
(function (RunCommandReportTypes) {
    RunCommandReportTypes[RunCommandReportTypes["NoTasks"] = "NoTasks"] = "NoTasks";
    RunCommandReportTypes[RunCommandReportTypes["Setup"] = "Setup"] = "Setup";
    RunCommandReportTypes[RunCommandReportTypes["Complete"] = "Complete"] = "Complete";
    RunCommandReportTypes[RunCommandReportTypes["TaskReport"] = "TaskReport"] = "TaskReport";
})(exports.RunCommandReportTypes || (exports.RunCommandReportTypes = {}));
var RunCommandReportTypes = exports.RunCommandReportTypes;
function executeRunCommand(trigger) {
    var cli = trigger.cli, input = trigger.input, config = trigger.config, reporter = trigger.reporter;
    var _a = command_run_1.getRunCommandSetup(trigger), tasks = _a.tasks, sequence = _a.sequence, runner = _a.runner;
    /**
     * Never continue if any tasks were flagged as invalid and we've not handed
     * off
     */
    if (tasks.invalid.length) {
        reporter({
            type: reporter_resolve_1.ReportTypes.TaskErrors,
            data: {
                tasks: tasks.all,
                taskCollection: cli.input.slice(1),
                input: input,
                config: config
            }
        });
        return {
            setup: {
                sequence: sequence,
                tasks: tasks,
                errors: []
            },
            update$: Rx.Observable.empty()
        };
    }
    debug("~ run mode from config in mode: '" + config.runMode + "'");
    /**
     * Report task list that's about to run
     */
    reporter({ type: reporter_resolve_1.ReportTypes.TaskList, data: { sequence: sequence, cli: cli, titlePrefix: '', config: config } });
    /**
     * Get a run context for this execution.
     * note: This could take some time as it may need
     * to hash directories etc. A run context is just a key=>value
     * map of read-only values.
     */
    return {
        setup: {
            sequence: sequence,
            tasks: tasks,
            errors: []
        },
        update$: command_run_context_1.default(tasks.all, trigger)
            .flatMap(function (runContext) {
            if (trigger.config.runMode === task_resolve_1.TaskRunModes.parallel) {
                return runner.parallel(runContext);
            }
            return runner.series(runContext);
        }).share()
    };
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = executeRunCommand;
//# sourceMappingURL=command.run.execute.js.map