import { __read, __spread, __values } from "tslib";
import { createLoggerInstance } from "./logger";
import { ELogLevel } from "./logLevel";
var middlewares = [];
export var createLogger = function () {
    var tags = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        tags[_i] = arguments[_i];
    }
    return createLoggerInstance(loggerParent, "", tags);
};
export var addReporter = function (reporter) {
    middlewares.push(reporter);
    return function () {
        var reporterIndex = middlewares.indexOf(reporter);
        if (reporterIndex > -1) {
            middlewares.splice(reporterIndex, 1);
        }
    };
};
export var addDefaultReporter = function (_a) {
    var _b = _a.level, minLevel = _b === void 0 ? ELogLevel.Error : _b, allowedTags = _a.allowedTags;
    var browserReporter = {
        level: minLevel,
        onLog: function (_a) {
            var level = _a.level, tags = _a.tags, args = _a.args;
            if (!allowedTags ||
                allowedTags.some(function (tag) { return tags.indexOf("[" + tag + "]") > -1; })) {
                var loggerMethod = void 0;
                switch (level) {
                    case ELogLevel.Info:
                        loggerMethod = console.log;
                        break;
                    case ELogLevel.Debug:
                        loggerMethod = console.log;
                        break;
                    case ELogLevel.Error:
                    case ELogLevel.Fatal:
                        loggerMethod = console.error;
                        break;
                    case ELogLevel.Warning:
                        loggerMethod = console.warn;
                        break;
                    default:
                        loggerMethod = console.log;
                        break;
                }
                loggerMethod.apply(void 0, __spread(["[" + ELogLevel[level] + "]" + (tags || "")], args));
            }
        },
    };
    middlewares.push(browserReporter);
};
export var loggerParent = {
    log: function (level, tags) {
        var e_1, _a;
        var messages = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            messages[_i - 2] = arguments[_i];
        }
        var params = {
            level: level,
            tags: tags,
            args: messages,
            timestamp: Date.now(),
        };
        try {
            for (var middlewares_1 = __values(middlewares), middlewares_1_1 = middlewares_1.next(); !middlewares_1_1.done; middlewares_1_1 = middlewares_1.next()) {
                var entry = middlewares_1_1.value;
                if (entry.level !== undefined && level < entry.level) {
                    continue;
                }
                entry.onLog(params);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (middlewares_1_1 && !middlewares_1_1.done && (_a = middlewares_1.return)) _a.call(middlewares_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    },
};
