"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = exports.logger = void 0;
let silenced = false;
const _originalConsole = {
    log: console.log.bind(console),
    warn: console.warn.bind(console),
    error: console.error.bind(console),
    info: console.info.bind(console),
};
let consoleOverloadInstalled = false;
function getEvents() {
    try {
        const { services } = require("./services");
        return services().Events;
    }
    catch {
        return null;
    }
}
function emit(source, message, level) {
    if (silenced)
        return;
    try {
        const events = getEvents();
        if (events) {
            events.log(source, message, level);
            return;
        }
    }
    catch {
    }
    const prefix = source ? `[${source}] ` : "";
    if (level === "warn")
        _originalConsole.warn(`${prefix}${message}`);
    else if (level === "error")
        _originalConsole.error(`${prefix}${message}`);
    else
        _originalConsole.log(`${prefix}${message}`);
}
function makeBoundLogger(source) {
    return {
        log(message, level = "info") {
            emit(source, message, level);
        },
        info(message) {
            emit(source, message, "info");
        },
        warn(message) {
            emit(source, message, "warn");
        },
        error(message) {
            emit(source, message, "error");
        },
    };
}
exports.logger = {
    log(source, message, level = "info") {
        emit(source, message, level);
    },
    info(source, message) {
        emit(source, message, "info");
    },
    warn(source, message) {
        emit(source, message, "warn");
    },
    error(source, message) {
        emit(source, message, "error");
    },
    silence() {
        silenced = true;
    },
    unsilence() {
        silenced = false;
    },
    isSilenced() {
        return silenced;
    },
    installConsoleOverload() {
        if (consoleOverloadInstalled)
            return;
        consoleOverloadInstalled = true;
        const route = (originalFn, args) => {
            if (silenced)
                return;
            originalFn(...args);
        };
        console.log = (...args) => route(_originalConsole.log, args);
        console.info = (...args) => route(_originalConsole.info, args);
        console.warn = (...args) => route(_originalConsole.warn, args);
    },
    uninstallConsoleOverload() {
        if (!consoleOverloadInstalled)
            return;
        console.log = _originalConsole.log;
        console.info = _originalConsole.info;
        console.warn = _originalConsole.warn;
        consoleOverloadInstalled = false;
    },
};
exports.Logger = {
    of(source) {
        return makeBoundLogger(source);
    },
};
//# sourceMappingURL=logger.js.map