"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventEmitter = void 0;
class EventEmitter {
    constructor() {
        this.listeners = {};
    }
    on(event, listener) {
        var _a;
        if (!Array.isArray(this.listeners[event])) {
            this.listeners[event] = [];
        }
        (_a = this.listeners[event]) === null || _a === void 0 ? void 0 : _a.push(listener);
        return () => {
            this.off(event, listener);
        };
    }
    off(event, listener) {
        var _a;
        const filtered = (_a = this.listeners[event]) === null || _a === void 0 ? void 0 : _a.filter(l => l !== listener);
        this.listeners[event] = filtered && filtered.length > 0 ? filtered : undefined;
    }
    emit(event, payload) {
        const listeners = this.listeners[event];
        if (!Array.isArray(listeners))
            return;
        for (const listener of listeners) {
            try {
                listener(payload);
            }
            catch (error) {
                console.error(error instanceof Error ? error.stack : error);
            }
        }
    }
}
exports.EventEmitter = EventEmitter;
