"use strict";
var task_utils_1 = require("../task.utils");
var task_resolve_1 = require("../task.resolve");
var task_resolve_2 = require("../task.resolve");
var task_runner_1 = require("../task.runner");
var defaultReporter_1 = require("./defaultReporter");
function taskPreviews(item) {
    if (!item.tasks.length) {
        if (item.type === task_resolve_2.TaskTypes.InlineFunction) {
            if (item.inlineFunctions[0].name) {
                return "[ Function: " + item.inlineFunctions[0].name + " ]";
            }
            return '[ Function ]';
        }
    }
    var names = item.tasks.map(function (x) {
        return task_utils_1.escapeNewLines(x.baseTaskName);
    });
    return "[ " + names.join(', ') + " ]";
}
function limit(string, linelength) {
    var rem = string.length - linelength;
    if (rem > 0) {
        return string.slice(0, linelength - 3) + '...';
    }
    return string;
}
function getSimpleTaskList(tasks) {
    var filtered = tasks
        .filter(function (x) { return !task_utils_1.isInternal(x.taskName); })
        .filter(function (x) { return x.baseTaskName[0] !== '_'; });
    return twoCol(filtered).map(function (x) { return (x[0] + "  " + x[1]); });
}
exports.getSimpleTaskList = getSimpleTaskList;
function twoCol(tasks) {
    var longest = task_utils_1.longestString(tasks.map(function (x) {
        if (x.runMode === task_resolve_1.TaskRunModes.parallel) {
            return x.baseTaskName + ' <p>';
        }
        return x.baseTaskName;
    }));
    var cols = process.stdout.columns;
    return tasks.map(function (item) {
        var outgoingName = (function () {
            if (item.runMode === task_resolve_1.TaskRunModes.parallel) {
                return item.baseTaskName + ' <p>';
            }
            return item.baseTaskName;
        })();
        var name = task_utils_1.padLine(outgoingName, longest + 1);
        var desclength = (cols - 6) - longest;
        var desc = (function () {
            if (item.description) {
                return limit(item.description, desclength);
            }
            /**
             * .js files on disk
             */
            if (item.type === task_resolve_2.TaskTypes.ExternalTask) {
                return limit("Run via: " + item.externalTasks[0].parsed.name, desclength);
            }
            /**
             * .sh files on disk
             */
            if (item.origin === task_resolve_1.TaskOriginTypes.FileSystem) {
                return limit("Run via: " + item.externalTasks[0].parsed.name, desclength);
            }
            return limit(taskPreviews(item), desclength);
        })();
        return [name, desc];
    });
}
exports.twoCol = twoCol;
function twoColWatchers(runners) {
    var longest = task_utils_1.longestString(runners.valid.map(function (x) { return x.parent; }));
    var cols = process.stdout.columns;
    return runners.valid.map(function (runner) {
        var name = task_utils_1.padLine(runner.parent, longest + 1);
        var desclength = (cols - 6) - longest;
        var desc = limit(runner.patterns.length + " pattern(s), " + runner.tasks.length + " task(s)", desclength);
        return [name, desc];
    });
}
exports.twoColWatchers = twoColWatchers;
function _taskReport(report) {
    var skipped = report.item.task.skipped || report.stats.skipped;
    var item = report.item;
    var task = item.task;
    var labelPrefix = defaultReporter_1.getLabel(task);
    var label = task_utils_1.escapeNewLines((function () {
        if (item.subTaskName) {
            return item.task.taskName + ":{bold:" + item.subTaskName + "}";
        }
        if (item.viaName) {
            if (item.viaName.indexOf(':') > -1) {
                var split = item.viaName.split(':');
                return split[0] + ":{bold:" + split[1] + "}";
            }
            return item.viaName;
        }
        return labelPrefix;
    })());
    var withFlags = (function (label) {
        if (Object.keys(task.flags).length) {
            return "" + label + task.rawInput.replace(label, '');
        }
        return label;
    })(label);
    return (function (label) {
        if (report.type === task_runner_1.TaskReportType.start) {
            if (skipped) {
                return "{yellow:-} " + label + " {yellow:(skipped)}";
            }
            return "{yellow:>} " + label;
        }
        if (report.type === task_runner_1.TaskReportType.end) {
            if (skipped) {
                return '';
            }
            return "{green:\u2714} " + label + " {yellow:(" + duration(report.stats.duration) + ")}";
        }
        if (report.type === task_runner_1.TaskReportType.error) {
            return "{red:x} " + label;
        }
    })(withFlags);
}
exports._taskReport = _taskReport;
function duration(ms) {
    return String((Number(ms) / 1000).toFixed(2)) + 's';
}
exports.duration = duration;
//# sourceMappingURL=task.list.js.map