"use strict";
/**
 * Game Iframe SDK - Event Emitter
 * Simple typed event emitter for SDK
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventEmitter = void 0;
class EventEmitter {
    constructor() {
        this.handlers = new Map();
    }
    /**
     * Subscribe to an event
     */
    on(event, handler) {
        if (!this.handlers.has(event)) {
            this.handlers.set(event, new Set());
        }
        this.handlers.get(event).add(handler);
        // Return unsubscribe function
        return () => this.off(event, handler);
    }
    /**
     * Subscribe to an event (once)
     */
    once(event, handler) {
        const wrappedHandler = (data) => {
            this.off(event, wrappedHandler);
            handler(data);
        };
        return this.on(event, wrappedHandler);
    }
    /**
     * Unsubscribe from an event
     */
    off(event, handler) {
        this.handlers.get(event)?.delete(handler);
    }
    /**
     * Emit an event
     */
    emit(event, data) {
        this.handlers.get(event)?.forEach(handler => {
            try {
                handler(data);
            }
            catch (err) {
                console.error(`[EventEmitter] Error in handler for "${String(event)}":`, err);
            }
        });
    }
    /**
     * Remove all handlers for an event (or all events)
     */
    removeAllListeners(event) {
        if (event) {
            this.handlers.delete(event);
        }
        else {
            this.handlers.clear();
        }
    }
}
exports.EventEmitter = EventEmitter;
//# sourceMappingURL=EventEmitter.js.map