import EventEmitter from 'events';
import { addTracingToMethods, traceFunction } from '../tracing/methodTracing.js';
export class TracedEventEmitter extends EventEmitter {
    tracer;
    constructor(tracer, options) {
        super(options);
        this.tracer = tracer;
        const methods = ['emit'];
        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) => {
            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;
    }
}
//# sourceMappingURL=tracedEventEmitter.js.map