import { EventEmitter } from "node:events";
import { log } from "./logger.js";
export function createEventBus() {
    const emitter = new EventEmitter();
    return {
        emit: (channel, data) => {
            emitter.emit(channel, data);
        },
        on: (channel, handler) => {
            const safeHandler = async (data) => {
                try {
                    await handler(data);
                }
                catch (err) {
                    log.warn(`Event handler error (${channel}): ${err instanceof Error ? err.message : String(err)}`);
                }
            };
            emitter.on(channel, safeHandler);
            return () => emitter.off(channel, safeHandler);
        },
        clear: () => {
            emitter.removeAllListeners();
        },
    };
}
//# sourceMappingURL=event-bus.js.map