"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.globalLogger = exports.Logger = exports.BaseLogger = void 0;
const pretty_print_1 = require("./pretty-print");
const logLevel = process.env.LOG_LEVEL ?? 'info';
const isDebugEnabled = logLevel === 'debug';
const isInfoEnabled = ['info', 'debug'].includes(logLevel);
const isWarnEnabled = ['warn', 'info', 'debug', 'trace'].includes(logLevel);
class BaseLogger {
    constructor(isVerbose = false, meta = {}) {
        this.isVerbose = isVerbose;
        this.meta = meta;
    }
    child(meta = {}) {
        return new BaseLogger(this.isVerbose, { ...this.meta, ...meta });
    }
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    _log(level, msg, args) {
        const time = Date.now();
        (0, pretty_print_1.prettyPrint)({ level, time, msg, ...this.meta, ...(args ?? {}) }, !this.isVerbose);
    }
    info(message, args) {
        if (isInfoEnabled) {
            this._log('info', message, args);
        }
    }
    error(message, args) {
        this._log('error', message, args);
    }
    debug(message, args) {
        if (isDebugEnabled) {
            this._log('debug', message, args);
        }
    }
    warn(message, args) {
        if (isWarnEnabled) {
            this._log('warn', message, args);
        }
    }
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    log(args) {
        this._log('info', args.msg, args);
    }
}
exports.BaseLogger = BaseLogger;
class Logger extends BaseLogger {
    constructor(traceId, flows, step, isVerbose, socketServer) {
        super(isVerbose, { traceId, flows, step });
        this.traceId = traceId;
        this.flows = flows;
        this.step = step;
        this.socketServer = socketServer;
        this.info = (message, args) => {
            super.info(message, args);
            this.emitLog('info', message, args);
        };
        this.error = (message, args) => {
            super.error(message, args);
            this.emitLog('error', message, args);
        };
        this.debug = (message, args) => {
            if (isDebugEnabled) {
                super.debug(message, args);
                this.emitLog('debug', message, args);
            }
        };
        this.warn = (message, args) => {
            super.warn(message, args);
            this.emitLog('warn', message, args);
        };
        this.emitLog = (level, msg, args) => {
            socketServer?.emit('log', {
                step: this.step,
                ...(args ?? {}),
                level,
                time: Date.now(),
                msg,
                traceId: this.traceId,
                flows: this.flows,
            });
        };
    }
    child(meta = {}) {
        return new Logger(this.traceId, this.flows, meta.step, this.isVerbose, this.socketServer);
    }
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    log(message) {
        super.log(message);
        this.emitLog(message.level, message.msg, message);
    }
}
exports.Logger = Logger;
exports.globalLogger = new BaseLogger();
