"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get createStaticLogger () {
        return createStaticLogger;
    },
    get getStaticLogger () {
        return getStaticLogger;
    },
    get logContextClassProperty () {
        return logContextClassProperty;
    }
});
const _winston = /*#__PURE__*/ _interop_require_default(require("winston"));
const _nodeprocess = /*#__PURE__*/ _interop_require_default(require("node:process"));
const _serverconstants = require("../../server.constants");
const _lokiloggingtransport = require("./loki-logging.transport");
const _fileloggingtransport = require("./file-logging.transport");
const _envutils = require("../../utils/env.utils");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
let staticLogger = null;
const levelMap = {
    error: "ERR",
    warn: "WRN",
    info: "INF",
    debug: "DBG",
    http: "HTT",
    verbose: "VRB",
    silly: "SLY"
};
const logContextClassProperty = "class";
function getStaticLogger() {
    if (!staticLogger) {
        throw new Error("Logger not yet initialized.");
    }
    return staticLogger;
}
function createStaticLogger(config) {
    if (staticLogger) {
        return;
    }
    const isProd = _nodeprocess.default.env[_serverconstants.AppConstants.NODE_ENV_KEY] === _serverconstants.AppConstants.defaultProductionEnv;
    const isTest = _nodeprocess.default.env[_serverconstants.AppConstants.NODE_ENV_KEY] === _serverconstants.AppConstants.defaultTestEnv;
    const effectiveLogLevel = isProd || isTest ? "warn" : "debug";
    const lokiTransport = (0, _lokiloggingtransport.createLokiLoggingTransport)({
        logLevel: effectiveLogLevel
    });
    const extraWinstonTransports = [];
    if (lokiTransport) {
        extraWinstonTransports.push(lokiTransport);
    }
    const fileLoggerTransport = (0, _fileloggingtransport.createFileLoggingTransport)({
        enabled: config.enableFileLogs,
        isTest
    });
    if (fileLoggerTransport) {
        extraWinstonTransports.push(fileLoggerTransport);
    }
    staticLogger = _winston.default.createLogger({
        transports: [
            ...extraWinstonTransports,
            new _winston.default.transports.Console({
                level: effectiveLogLevel,
                format: _winston.default.format.combine(...(0, _envutils.isDevelopmentEnvironment)() && _nodeprocess.default.env[_serverconstants.AppConstants.ENABLE_COLORED_LOGS_KEY] == "true" ? [
                    _winston.default.format((info)=>{
                        info.rawLevel = info.level;
                        return info;
                    })(),
                    _winston.default.format.colorize({
                        colors: {
                            error: "red",
                            warn: "yellow",
                            info: "white",
                            debug: "gray",
                            http: "magenta",
                            verbose: "cyan",
                            silly: "gray"
                        },
                        level: true,
                        message: true,
                        all: false
                    }),
                    _winston.default.format.printf((info)=>{
                        const now = new Date();
                        const timestamp = `${now.toISOString().split("T")[0]} ${now.toTimeString().split(" ")[0]}.${now.getMilliseconds().toString().padStart(3, "0")}`;
                        const levelAbbr = levelMap[info.rawLevel] ?? info.rawLevel.substring(0, 3).toUpperCase();
                        const gray = "\x1b[90m";
                        const reset = info.message.substring(0, 5) ?? "\x1b[0m";
                        const numberRegex = /\b\d+\b/g;
                        const coloredMessage = info.message.replace(numberRegex, (match)=>`\x1b[35m${match}${reset}`);
                        const serviceName = info[logContextClassProperty] ?? "unknown";
                        let logEntry = `${gray}[${timestamp} ${reset}${levelAbbr}${reset}${gray}]${reset} ${gray}[${reset}${serviceName}${gray}]${reset} ${coloredMessage}`;
                        if (info.meta) {
                            const metaString = JSON.stringify(info.meta);
                            const coloredMeta = metaString.replace(numberRegex, (match)=>`\x1b[35m${match}${reset}`);
                            logEntry += ` ${coloredMeta}`;
                        }
                        return logEntry;
                    })
                ] : [])
            })
        ],
        format: _winston.default.format.printf((info)=>{
            const now = new Date();
            const timestamp = `${now.toISOString().split("T")[0]} ${now.toTimeString().split(" ")[0]}.${now.getMilliseconds().toString().padStart(3, "0")}`;
            const levelAbbr = levelMap[info.level] || `[${info.level.substring(0, 3).toUpperCase()}]`;
            const serviceName = info[logContextClassProperty] ?? "unknown";
            let message = `[${timestamp} ${levelAbbr}] [${serviceName}] ${info.message}`;
            if (info.meta) {
                const pascalCaseMeta = Object.entries(info.meta).reduce((acc, [key, value])=>{
                    const pascalKey = key.charAt(0).toUpperCase() + key.slice(1);
                    acc[pascalKey] = value;
                    return acc;
                }, {});
                message += ` ${JSON.stringify(pascalCaseMeta)}`;
            }
            return message;
        })
    });
}

//# sourceMappingURL=static.logger.js.map