/* ────────────────────────────────────────────────────────────────
   EventBus — Typed publish/subscribe event system
──────────────────────────────────────────────────────────────── */
class EventBus {
    constructor() {
        this.handlers = new Map();
    }
    /** Subscribe to an event. Returns unsubscribe function. */
    on(event, handler) {
        let set = this.handlers.get(event);
        if (!set) {
            set = new Set();
            this.handlers.set(event, set);
        }
        set.add(handler);
        return () => this.off(event, handler);
    }
    /** Subscribe once — auto-removes after first call. */
    once(event, handler) {
        const wrapper = (data) => {
            this.off(event, wrapper);
            handler(data);
        };
        return this.on(event, wrapper);
    }
    /** Unsubscribe from an event. */
    off(event, handler) {
        const set = this.handlers.get(event);
        if (set) {
            set.delete(handler);
            if (set.size === 0)
                this.handlers.delete(event);
        }
    }
    /** Emit an event to all subscribers. */
    emit(event, data) {
        const set = this.handlers.get(event);
        if (set) {
            for (const handler of set) {
                handler(data);
            }
        }
    }
    /** Remove all handlers for an event, or all events if no arg. */
    clear(event) {
        if (event) {
            this.handlers.delete(event);
        }
        else {
            this.handlers.clear();
        }
    }
    /** Check if an event has any listeners. */
    has(event) {
        const set = this.handlers.get(event);
        return set != null && set.size > 0;
    }
}

export { EventBus };
//# sourceMappingURL=EventBus.js.map
