"use strict";
/* eslint-disable class-methods-use-this */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
/**
 * Returns the opentelemetry spec compatible flattened object
 * @param obj
 * @param parentKey
 * @param res
 * @returns flattend object. {a: {b:2}, c:[1,2]} => {'a.b' : 2, c: [1,2]}
 */
function _flattenObjForOpenTelemetry(obj, parentKey, res = {}) {
    Object.getOwnPropertyNames(obj).forEach((key) => {
        if ([null, undefined, NaN].includes(obj[key])) {
            // Opentelemtry doesn't support null & undefined in their spec
            return;
        }
        const propName = parentKey ? `${parentKey}.${key}` : key;
        /* NOTE(ravindra-dyte):
            We want to flatten objects recursively, including arrays.
            Opentelemetry spec supports arrays but newrelic indexing will be impacted
            Flattening arrays, because they can also have objects in them
            Ideally we shouldn't be passing arrays in addEventInCurrentSpan method
        */
        if (typeof obj[key] === 'object') {
            _flattenObjForOpenTelemetry(obj[key], propName, res);
        }
        else {
            res[propName] = obj[key];
        }
    });
    return res;
}
class Logger {
    constructor() {
        this.level = 3;
    }
    // eslint-disable-next-line class-methods-use-this
    process(data) {
        const processed = {};
        try {
            Object.getOwnPropertyNames(data || {}).forEach((key) => {
                try {
                    processed[key] = JSON.stringify(data[key]);
                }
                catch (_a) {
                    processed[key] = 'Failed to stringify';
                }
            });
        }
        catch (e) {
            // pass
        }
        return processed;
    }
    init(level) {
        if (level === 'trace')
            this.level = 3;
        else if (level === 'log')
            this.level = 2;
        else if (level === 'error')
            this.level = 1;
    }
    trace(event, metadata) {
        Logger.addEventInCurrentSpan('warn', event, metadata, true);
    }
    warn(event, metadata) {
        Logger.addEventInCurrentSpan('warn', event, metadata, true);
    }
    error(event, metadata) {
        Logger.addEventInCurrentSpan('error', event, metadata, true);
    }
    static addEventInCurrentSpan(eventSeverity = 'info', eventName, metadata = {}, noCache) {
        try {
            const flattendMetadata = _flattenObjForOpenTelemetry(metadata, 'metadata');
            const logToProcess = Object.assign(Object.assign({ message: eventName, logSeverity: eventSeverity }, flattendMetadata), { loggedAt: new Date().getTime() });
            if (noCache) {
                Logger.sendOtelLogsToNewRelic(Logger.logsCache.splice(0, 25).concat([logToProcess]));
            }
            else {
                Logger.logsCache.push(logToProcess);
            }
        }
        catch (ex) {
            console.debug('opentelemetry::addEventInCurrentSpan_failed', ex);
        }
    }
    static sendOtelLogsToNewRelic(logs) {
        fetch('https://api-silos.dyte.io/otel/logs', {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                meetingMetadata: _flattenObjForOpenTelemetry(Logger.meetingMetadata, 'meetingMetadata'),
                serviceName: 'mobile-sdk',
                logs,
            }),
        }).catch((err) => {
            console.debug('opentelemetry::sendOtelLogToNewRelic_failed', err);
        });
    }
}
exports.Logger = Logger;
Logger.logsCache = [];
Logger.meetingMetadata = {};
exports.default = new Logger();
//# sourceMappingURL=logger.js.map