"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var logger_1 = require("./logger");
var logLevel_1 = require("./logLevel");
var middlewares = [];
exports.createLogger = function () {
    var tags = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        tags[_i] = arguments[_i];
    }
    return logger_1.createLoggerInstance(exports.loggerParent, "", tags);
};
exports.addReporter = function (reporter) {
    middlewares.push(reporter);
    return function () {
        var reporterIndex = middlewares.indexOf(reporter);
        if (reporterIndex > -1) {
            middlewares.splice(reporterIndex, 1);
        }
    };
};
exports.addDefaultReporter = function (_a) {
    var _b = _a.level, minLevel = _b === void 0 ? logLevel_1.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 logLevel_1.ELogLevel.Info:
                        loggerMethod = console.log;
                        break;
                    case logLevel_1.ELogLevel.Debug:
                        loggerMethod = console.log;
                        break;
                    case logLevel_1.ELogLevel.Error:
                    case logLevel_1.ELogLevel.Fatal:
                        loggerMethod = console.error;
                        break;
                    case logLevel_1.ELogLevel.Warning:
                        loggerMethod = console.warn;
                        break;
                    default:
                        loggerMethod = console.log;
                        break;
                }
                loggerMethod.apply(void 0, tslib_1.__spread(["[" + logLevel_1.ELogLevel[level] + "]" + (tags || "")], args));
            }
        },
    };
    middlewares.push(browserReporter);
};
exports.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 = tslib_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; }
        }
    },
};
