import { addMonitoringLog } from '../routes/monitoring.js';
const MONITORING_WRAPPED = '__monitoringWrapped';
export function wrapLoggerForMonitoring(logger) {
    if (!logger || typeof logger.info !== 'function') {
        return logger;
    }
    if (logger[MONITORING_WRAPPED]) {
        return logger;
    }
    if (typeof logger.getLogs === 'function') {
        return logger;
    }
    const originalInfo = logger.info.bind(logger);
    const originalWarn = logger.warn.bind(logger);
    const originalError = logger.error.bind(logger);
    const originalDebug = logger.debug?.bind(logger);
    const resolveCorrelationId = (data) => {
        if (data && typeof data['correlationId'] === 'string') {
            return data['correlationId'];
        }
        if (typeof logger.getCorrelationId === 'function') {
            return logger.getCorrelationId();
        }
        return undefined;
    };
    const logContext = typeof logger.getComponent === 'function'
        ? logger.getComponent()
        : typeof logger.prefix === 'string'
            ? logger.prefix
            : 'Gateway';
    const capture = (level, message, data) => {
        const correlationId = resolveCorrelationId(data);
        addMonitoringLog({
            timestamp: new Date().toISOString(),
            level,
            message,
            context: logContext,
            ...(correlationId && { correlationId }),
            ...(data && { data }),
        });
    };
    logger.info = (message, data) => {
        capture('info', message, data);
        return originalInfo(message, data);
    };
    logger.warn = (message, data) => {
        capture('warn', message, data);
        return originalWarn(message, data);
    };
    logger.error = (message, data) => {
        capture('error', message, data);
        return originalError(message, data);
    };
    if (originalDebug) {
        logger.debug = (message, data) => {
            capture('debug', message, data);
            return originalDebug(message, data);
        };
    }
    logger[MONITORING_WRAPPED] = true;
    return logger;
}
//# sourceMappingURL=log-capture.js.map