"use strict";
var file_utils_1 = require("./file.utils");
(function (ReporterErrorTypes) {
    ReporterErrorTypes[ReporterErrorTypes["ReporterFileNotFound"] = "ReporterFileNotfound"] = "ReporterFileNotFound";
    ReporterErrorTypes[ReporterErrorTypes["ReporterTypeNotSupported"] = "ReporterTypeNotSupported"] = "ReporterTypeNotSupported";
})(exports.ReporterErrorTypes || (exports.ReporterErrorTypes = {}));
var ReporterErrorTypes = exports.ReporterErrorTypes;
(function (ReporterTypes) {
    ReporterTypes[ReporterTypes["InlineFunction"] = "InlineFunction"] = "InlineFunction";
    ReporterTypes[ReporterTypes["ExternalFile"] = "ExternalFile"] = "ExternalFile";
    ReporterTypes[ReporterTypes["UnsupportedValue"] = "UnsupportedValue"] = "UnsupportedValue";
    ReporterTypes[ReporterTypes["Muted"] = "Muted"] = "Muted";
})(exports.ReporterTypes || (exports.ReporterTypes = {}));
var ReporterTypes = exports.ReporterTypes;
(function (ReportNames) {
    ReportNames[ReportNames["DuplicateConfigFile"] = "DuplicateConfigFile"] = "DuplicateConfigFile";
    ReportNames[ReportNames["ConfigFileCreated"] = "ConfigFileCreated"] = "ConfigFileCreated";
    ReportNames[ReportNames["InitConfigTypeNotSupported"] = "InitConfigTypeNotSupported"] = "InitConfigTypeNotSupported";
    ReportNames[ReportNames["InputError"] = "InputFileNotFound"] = "InputError";
    ReportNames[ReportNames["InputFileNotFound"] = "InputFileNotFound"] = "InputFileNotFound";
    ReportNames[ReportNames["InvalidReporter"] = "InvalidReporter"] = "InvalidReporter";
    ReportNames[ReportNames["UsingConfigFile"] = "UsingConfigFile"] = "UsingConfigFile";
    ReportNames[ReportNames["TaskList"] = "TaskList"] = "TaskList";
    ReportNames[ReportNames["TaskTree"] = "TaskTree"] = "TaskTree";
    ReportNames[ReportNames["TaskErrors"] = "TaskErrors"] = "TaskErrors";
    ReportNames[ReportNames["TaskReport"] = "TaskReport"] = "TaskReport";
    ReportNames[ReportNames["InvalidTasksSimple"] = "InvalidTasksSimple"] = "InvalidTasksSimple";
    ReportNames[ReportNames["NoTasksAvailable"] = "NoTasksAvailable"] = "NoTasksAvailable";
    ReportNames[ReportNames["NoTasksProvided"] = "NoTasksProvided"] = "NoTasksProvided";
    ReportNames[ReportNames["SimpleTaskList"] = "SimpleTaskList"] = "SimpleTaskList";
    ReportNames[ReportNames["BeforeWatchTaskErrors"] = "BeforeWatchTaskErrors"] = "BeforeWatchTaskErrors";
    ReportNames[ReportNames["BeforeTaskList"] = "BeforeTaskList"] = "BeforeTaskList";
    ReportNames[ReportNames["BeforeTasksDidNotComplete"] = "BeforeTasksDidNotComplete"] = "BeforeTasksDidNotComplete";
    ReportNames[ReportNames["WatchTaskTasksErrors"] = "WatchTaskTasksErrors"] = "WatchTaskTasksErrors";
    ReportNames[ReportNames["WatchTaskErrors"] = "WatchTaskErrors"] = "WatchTaskErrors";
    ReportNames[ReportNames["WatchTaskReport"] = "WatchTaskReport"] = "WatchTaskReport";
    ReportNames[ReportNames["NoFilesMatched"] = "NoFilesMatched"] = "NoFilesMatched";
    ReportNames[ReportNames["NoWatchersAvailable"] = "NoWatchersAvailable"] = "NoWatchersAvailable";
    ReportNames[ReportNames["NoWatchTasksProvided"] = "NoWatchTasksProvided"] = "NoWatchTasksProvided";
    ReportNames[ReportNames["Watchers"] = "Watchers"] = "Watchers";
    ReportNames[ReportNames["WatcherNames"] = "WatcherNames"] = "WatcherNames";
    ReportNames[ReportNames["WatcherTriggeredTasksCompleted"] = "WatcherTriggeredTasksCompleted"] = "WatcherTriggeredTasksCompleted";
    ReportNames[ReportNames["WatcherTriggeredTasks"] = "WatcherTriggeredTasks"] = "WatcherTriggeredTasks";
    ReportNames[ReportNames["DocsAddedToFile"] = "DocsAddedToFile"] = "DocsAddedToFile";
    ReportNames[ReportNames["DocsGenerated"] = "DocsMarkdownGenerated"] = "DocsGenerated";
    ReportNames[ReportNames["DocsInputFileNotFound"] = "DocsInputFileNotFound"] = "DocsInputFileNotFound";
    ReportNames[ReportNames["DocsOutputFileExists"] = "DocsOutputFileExists"] = "DocsOutputFileExists";
    ReportNames[ReportNames["Summary"] = "Summary"] = "Summary";
    ReportNames[ReportNames["HashDirError"] = "HashDirError"] = "HashDirError";
})(exports.ReportNames || (exports.ReportNames = {}));
var ReportNames = exports.ReportNames;
function getReporters(config, input) {
    var reporters = (function () {
        /**
         * If --outputOnly (-q) was given, the user wants NO
         * output, other than output from child processes - this
         * is used for shell expansion within other tasks where
         * the 'using crossbow.yaml' or completions summaries are
         * unwanted.
         */
        if (config.outputOnly) {
            return [{
                    type: ReporterTypes.Muted,
                    callable: function () { },
                    errors: [],
                    sources: []
                }];
        }
        /**
         * At this point, a user may of provided a string (as a path to lookup)
         * or a function directly, so we use those to resolve the reporters.
         */
        return [].concat(config.reporters).map(getOneReporter);
    })();
    return {
        all: reporters,
        valid: reporters.filter(function (x) { return x.errors.length === 0; }),
        invalid: reporters.filter(function (x) { return x.errors.length > 0; })
    };
    function getOneReporter(reporter) {
        /**
         * If a function was given as a reported (eg: inline)
         * then it's ALWAYS a valid reporter
         */
        if (typeof reporter === 'function') {
            return {
                type: ReporterTypes.InlineFunction,
                callable: reporter,
                errors: [],
                sources: []
            };
        }
        /**
         * If the reporter was not a string or function
         * it's definitely an unsupported type
         */
        if (typeof reporter !== 'string') {
            return {
                type: ReporterTypes.UnsupportedValue,
                errors: [{ type: ReporterErrorTypes.ReporterTypeNotSupported }],
                sources: [reporter]
            };
        }
        var files = file_utils_1.readFilesFromDisk([reporter], config.cwd);
        var errors = files
            .reduce(function (acc, item) {
            // Convert errors from reading files
            // into errors about reporters
            // This is for correct context in logging
            if (item.errors.length) {
                return acc.concat({
                    type: ReporterErrorTypes.ReporterFileNotFound,
                    file: item
                });
            }
            return acc;
        }, []);
        /**
         * If any errors occurred, return them
         */
        if (errors.length) {
            return {
                type: ReporterTypes.ExternalFile,
                errors: errors,
                sources: files
            };
        }
        /**
         * Now try to 'require' the module. If it
         * does not contain a default export, create an error
         */
        var callable = require(files[0].resolved);
        if (typeof callable !== "function") {
            return {
                type: ReporterTypes.UnsupportedValue,
                errors: [{ type: ReporterErrorTypes.ReporterTypeNotSupported }],
                sources: [files[0]]
            };
        }
        /**
         * Here we have a valid external file to return
         * as a single reporter
         */
        return {
            type: ReporterTypes.ExternalFile,
            callable: callable,
            errors: [],
            sources: files
        };
    }
}
exports.getReporters = getReporters;
function getDefaultReporter() {
    return require('./reporters/defaultReporter').default;
}
exports.getDefaultReporter = getDefaultReporter;
//# sourceMappingURL=reporter.resolve.js.map