class EventBus {
    listeners = new Map();
    emit(eventName, payload) {
        const listeners = this.listeners.get(eventName);
        if (listeners) {
            const enrichedPayload = {
                ...payload,
                timestamp: Date.now()
            };
            listeners.forEach((callback)=>callback(enrichedPayload));
        }
    }
    on(eventName, callback) {
        if (!this.listeners.has(eventName)) this.listeners.set(eventName, new Set());
        this.listeners.get(eventName).add(callback);
        return ()=>this.off(eventName, callback);
    }
    off(eventName, callback) {
        this.listeners.get(eventName)?.delete(callback);
        if (this.listeners.get(eventName)?.size === 0) this.listeners.delete(eventName);
    }
    once(eventName, callback) {
        const wrappedCallback = (payload)=>{
            callback(payload);
            this.off(eventName, wrappedCallback);
        };
        return this.on(eventName, wrappedCallback);
    }
    clear() {
        this.listeners.clear();
    }
}
const canvasEventBus = new EventBus();
export { canvasEventBus };
