"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TracedEventEmitter = void 0;
const events_1 = __importDefault(require("events"));
const methodTracing_js_1 = require("../tracing/methodTracing.js");
class TracedEventEmitter extends events_1.default {
    tracer;
    constructor(tracer, options) {
        super(options);
        this.tracer = tracer;
        const methods = ['emit'];
        (0, methodTracing_js_1.addTracingToMethods)(this, methods, this.tracer);
    }
    on(eventName, listener1) {
        const tracer = this.tracer;
        const klassName = this.constructor.name;
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const name = listener1.name;
        const listenerStr = name ? name : 'anonymous';
        const outputStr = `${klassName}.emit listener on ${String(eventName)}: ${listenerStr}`;
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const wrappedListener = ((...args) => {
            (0, methodTracing_js_1.traceFunction)({
                name: outputStr,
                tracer: tracer,
                fn: async () => {
                    // await the listener, but since we're not awaiting the wrapper, we've decoupled .emit()
                    // returning from the amount of time that the listener takes, but this will let us get a
                    // more accurate idea of how long callers take
                    // eslint-disable-next-line @typescript-eslint/await-thenable
                    await listener1(...args);
                }
            }).catch((err) => {
                throw err;
            });
        });
        super.on(eventName, wrappedListener);
        return this;
    }
}
exports.TracedEventEmitter = TracedEventEmitter;
//# sourceMappingURL=tracedEventEmitter.js.map