// (C) 2025 GoodData Corporation
/**
 * A simple strongly-typed event emitter.
 *
 * @internal
 *
 * @example
 * ```
 * type MyEvents = {
 *   keydown: KeyboardEvent;
 * };
 * const emitter = new EventEmitter<MyEvents>();
 * emitter.on("keydown", (event) => { ... });
 * emitter.emit("keydown", event);
 * ```
 */
export class EventEmitter {
    listeners = Object.create(null);
    /**
     * Register an event handler for a specific event.
     */
    on(event, handler) {
        if (!this.listeners[event]) {
            this.listeners[event] = new Set();
        }
        this.listeners[event].add(handler);
    }
    /**
     * Remove a previously registered event handler.
     */
    off(event, handler) {
        this.listeners[event]?.delete(handler);
        if (this.listeners[event]?.size === 0) {
            delete this.listeners[event];
        }
    }
    /**
     * Emit an event, calling all registered handlers with the provided data.
     */
    emit(event, data) {
        this.listeners[event]?.forEach((handler) => handler(data));
    }
}
