"use strict";
var watch_file_watcher_1 = require("../watch.file-watcher");
var watch_resolve_1 = require("../watch.resolve");
// todo why are these imports needed here?
var command_watch_1 = require('../command.watch');
var task_utils_1 = require("../task.utils");
var merge = require('../../lodash.custom').merge;
var fncount = 0;
var inlineWatcherCount = 0;
function incomingTask(taskname, deps, fn) {
    // only 2 params given (function last);
    if (typeof deps === 'function') {
        fn = deps;
        deps = [];
    }
    var outgoing = {};
    if (task_utils_1.isPlainObject(deps) && deps.tasks) {
        if (deps.tasks) {
            outgoing[taskname] = deps;
        }
        else {
            throw new Error('Object literal must contain at least a "tasks" key');
        }
        if (fn) {
            var fnname = taskname + "_internal_fn_" + fncount++;
            outgoing[fnname] = fn;
            outgoing[taskname].tasks.push(fn);
        }
        return outgoing;
    }
    deps = [].concat(deps).filter(Boolean);
    if (deps.length) {
        if (!fn) {
            outgoing[taskname] = deps;
        }
        else {
            var fnname = taskname + "_internal_fn_" + fncount;
            outgoing[fnname] = fn;
            outgoing[taskname] = deps.concat(fnname);
        }
    }
    else {
        if (fn) {
            outgoing[taskname] = fn;
        }
    }
    return outgoing;
}
var input = {
    tasks: {},
    watch: {},
    options: {},
    env: {},
    config: {},
    cli: {},
    reporter: function () { } // to be set by lib
};
function incomingOptions(taskname, hash) {
    var outgoing = {};
    if (typeof taskname === 'string') {
        outgoing[taskname] = hash;
        return outgoing;
    }
    return taskname;
}
exports.api = {
    input: input,
    env: function (obj) {
        input.env = merge(input.env, obj);
    },
    config: function (obj) {
        input.config = merge(input.config, obj);
    },
    task: function (taskname) {
        var res = incomingTask.apply(null, arguments);
        input.tasks = merge(input.tasks, res);
        return {
            options: function (hash) {
                var res = incomingOptions(taskname, hash);
                input.options = merge(input.options, res);
            }
        };
    },
    options: function (incoming) {
        var res = incomingOptions.apply(null, arguments);
        input.options = merge(input.options, res);
    },
    watch: function (patterns, tasks, options) {
        var identifer = "_inline_watcher_" + inlineWatcherCount++;
        patterns = [].concat(patterns);
        tasks = [].concat(tasks);
        input.watch[identifer] = {
            options: options,
            watchers: [
                {
                    patterns: patterns,
                    tasks: tasks
                }
            ]
        };
        var cliInput = ['watch', identifer];
        var output = command_watch_1.default({ input: cliInput, flags: {} }, input, input.config, input.reporter);
        return output;
    },
    watcher: function (patterns, options) {
        var num = inlineWatcherCount++;
        var identifer = "_inline_watcher_" + num;
        var watcher = {
            patterns: patterns,
            tasks: [],
            name: identifer,
            options: merge({}, watch_resolve_1.defaultWatchOptions, options),
            watcherUID: identifer + "-" + num
        };
        return watch_file_watcher_1.getFileChangeStream(watcher, input.reporter);
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = input;
//# sourceMappingURL=create.js.map