"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = exports.logger = exports.LogLevel = void 0;
exports.createLogger = createLogger;
const env_1 = require("./env");
var LogLevel;
(function (LogLevel) {
    LogLevel[LogLevel["ERROR"] = 0] = "ERROR";
    LogLevel[LogLevel["WARN"] = 1] = "WARN";
    LogLevel[LogLevel["INFO"] = 2] = "INFO";
    LogLevel[LogLevel["DEBUG"] = 3] = "DEBUG";
})(LogLevel || (exports.LogLevel = LogLevel = {}));
const logLevelMap = {
    error: LogLevel.ERROR,
    warn: LogLevel.WARN,
    info: LogLevel.INFO,
    debug: LogLevel.DEBUG,
};
class Logger {
    constructor(prefix = '4Runr', correlationId) {
        this.prefix = prefix;
        this.level = logLevelMap[env_1.env.LOG_LEVEL] || LogLevel.INFO;
        this.correlationId = correlationId ?? undefined;
        this.structured = env_1.env.NODE_ENV === 'production';
    }
    setCorrelationId(correlationId) {
        this.correlationId = correlationId;
    }
    shouldLog(level) {
        return level <= this.level;
    }
    formatMessage(level, message, data) {
        const timestamp = new Date().toISOString();
        const prefix = `[${timestamp}] [${level.toUpperCase()}] [${this.prefix}]`;
        if (data) {
            return `${prefix} ${message} ${JSON.stringify(data, null, 2)}`;
        }
        return `${prefix} ${message}`;
    }
    formatStructured(level, message, data) {
        const logEntry = {
            timestamp: new Date().toISOString(),
            level: level.toUpperCase(),
            component: this.prefix,
            message,
        };
        if (this.correlationId) {
            logEntry.correlationId = this.correlationId;
        }
        if (data) {
            logEntry.data = data;
        }
        return JSON.stringify(logEntry);
    }
    log(level, levelName, message, data) {
        if (!this.shouldLog(level)) {
            return;
        }
        const output = this.structured
            ? this.formatStructured(levelName, message, data)
            : this.formatMessage(levelName, message, data);
        switch (level) {
            case LogLevel.ERROR:
                console.error(output);
                break;
            case LogLevel.WARN:
                console.warn(output);
                break;
            case LogLevel.INFO:
                console.info(output);
                break;
            case LogLevel.DEBUG:
                console.debug(output);
                break;
        }
    }
    error(message, data) {
        this.log(LogLevel.ERROR, 'error', message, data);
    }
    warn(message, data) {
        this.log(LogLevel.WARN, 'warn', message, data);
    }
    info(message, data) {
        this.log(LogLevel.INFO, 'info', message, data);
    }
    debug(message, data) {
        this.log(LogLevel.DEBUG, 'debug', message, data);
    }
    sentinel(message, data) {
        this.info(`[Sentinel] ${message}`, data);
    }
    shield(message, data) {
        this.info(`[Shield] ${message}`, data);
    }
    judge(message, data) {
        this.info(`[Judge] ${message}`, data);
    }
    coach(message, data) {
        this.info(`[Coach] ${message}`, data);
    }
    guard(message, data) {
        this.info(`[Guard] ${message}`, data);
    }
    audit(message, data) {
        this.info(`[Audit] ${message}`, data);
    }
    safe(message, data) {
        if (data && typeof data === 'object') {
            const safeData = { ...data };
            const sensitiveFields = ['password', 'token', 'secret', 'key', 'authorization'];
            sensitiveFields.forEach(field => {
                if (safeData[field]) {
                    safeData[field] = '***REDACTED***';
                }
            });
            this.info(message, safeData);
        }
        else {
            this.info(message, data);
        }
    }
}
exports.Logger = Logger;
exports.logger = new Logger();
function createLogger(component, correlationId) {
    return new Logger(`4Runr:${component}`, correlationId);
}
//# sourceMappingURL=logger.js.map