"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.batchLogger = exports.logger = void 0;
const tslib_1 = require("tslib");
const pino_1 = tslib_1.__importDefault(require("pino"));
require("dotenv/config");
const isPrettyPrintEnabled = process.env.LOG_PRETTY === 'true';
const isTestEnvironment = process.env.ENVIRONMENT === 'tests';
const defaultLabels = {
    name: process.env.INSTANCE,
    app: process.env.APP_NAME,
    pod: process.env.POD_NAME,
    environment: process.env.ENVIRONMENT,
};
const loggerConfig = {
    enabled: !isTestEnvironment,
    level: process.env.LOG_LEVEL || 'info',
    formatters: {
        level: (label) => {
            return { level: label };
        },
    },
    base: defaultLabels,
    stream: process.stdout,
    transport: isPrettyPrintEnabled
        ? {
            target: 'pino-pretty',
        }
        : undefined,
};
exports.logger = (0, pino_1.default)(loggerConfig);
const logBatch = {};
// Utility to aggregate fields into arrays
const aggregateFields = (existing, value) => {
    if (value !== undefined && !existing.includes(value)) {
        return [...existing, value];
    }
    return existing;
};
// Function to flush logs for a given message
const flushLogForMessage = (messageKey, showAggregation) => {
    const log = logBatch[messageKey];
    // Exclude fields from log.aggregated if they are empty list []
    const aggregation = Object.fromEntries(Object.entries(log.aggregated).filter(([_, value]) => value.length > 0));
    if (log) {
        exports.logger.info({
            count: log.count,
            aggregated: showAggregation ? JSON.stringify(aggregation) : undefined,
        }, log.message);
        delete logBatch[messageKey]; // Clear the batch after flushing
    }
};
// Main batch logger function with optional custom aggregation time
const batchLogger = (aggregationFields = {}, message, customAggregationTimeSeconds = 5, showAggregation = true) => {
    const messageKey = message;
    if (logBatch[messageKey]) {
        // If the message already exists in the batch, aggregate fields
        logBatch[messageKey].count += 1;
        logBatch[messageKey].aggregated.id = aggregateFields(logBatch[messageKey].aggregated.id, aggregationFields.id);
        logBatch[messageKey].aggregated.executor = aggregateFields(logBatch[messageKey].aggregated.executor, aggregationFields.executor);
        logBatch[messageKey].aggregated.status = aggregateFields(logBatch[messageKey].aggregated.status, aggregationFields.status);
        logBatch[messageKey].aggregated.amount = aggregateFields(logBatch[messageKey].aggregated.amount, aggregationFields.amount);
        logBatch[messageKey].aggregated.txHash = aggregateFields(logBatch[messageKey].aggregated.txHash, aggregationFields.txHash);
        logBatch[messageKey].aggregated.blockNumber = aggregateFields(logBatch[messageKey].aggregated.blockNumber, aggregationFields.blockNumber);
        logBatch[messageKey].aggregated.message = aggregateFields(logBatch[messageKey].aggregated.message, aggregationFields.message);
        logBatch[messageKey].aggregated.source = aggregateFields(logBatch[messageKey].aggregated.source, aggregationFields.source);
        logBatch[messageKey].aggregated.destination = aggregateFields(logBatch[messageKey].aggregated.destination, aggregationFields.destination);
        logBatch[messageKey].aggregated.ageInMinutes = aggregateFields(logBatch[messageKey].aggregated.ageInMinutes, aggregationFields.ageInMinutes);
    }
    else {
        // Create a new log entry
        logBatch[messageKey] = {
            count: 1,
            aggregated: {
                id: aggregationFields.id ? [aggregationFields.id] : [],
                executor: aggregationFields.executor ? [aggregationFields.executor] : [],
                txHash: aggregationFields.txHash ? [aggregationFields.txHash] : [],
                blockNumber: aggregationFields.blockNumber ? [aggregationFields.blockNumber] : [],
                source: aggregationFields.source ? [aggregationFields.source] : [],
                destination: aggregationFields.destination ? [aggregationFields.destination] : [],
                status: aggregationFields.status ? [aggregationFields.status] : [],
                ageInMinutes: aggregationFields.ageInMinutes ? [aggregationFields.ageInMinutes] : [],
                amount: aggregationFields.amount ? [aggregationFields.amount] : [],
                message: aggregationFields.message ? [aggregationFields.message] : [],
                context: aggregationFields.context ? [aggregationFields.context] : [],
            },
            message,
        };
        // Set a custom timer for flushing this specific message
        logBatch[messageKey].timerId = setTimeout(() => {
            flushLogForMessage(messageKey, showAggregation);
        }, customAggregationTimeSeconds * 1000);
    }
};
exports.batchLogger = batchLogger;
//# sourceMappingURL=logger.js.map