class EventEmitter {
  listeners = /* @__PURE__ */ new Map();
  on(eventType, handler) {
    if (!this.listeners.has(eventType)) {
      this.listeners.set(eventType, []);
    }
    this.listeners.get(eventType).push(handler);
  }
  off(eventType, handler) {
    const handlers = this.listeners.get(eventType);
    if (handlers) {
      const index = handlers.indexOf(handler);
      if (index !== -1) {
        handlers.splice(index, 1);
      }
    }
  }
  async emit(eventType, data) {
    const handlers = this.listeners.get(eventType);
    if (handlers) {
      const promises = handlers.map((handler) => {
        try {
          const result = handler(data);
          return Promise.resolve(result);
        } catch (error) {
          console.error(`Error in event handler for ${eventType}:`, error);
          return Promise.resolve();
        }
      });
      await Promise.allSettled(promises);
    }
  }
  removeAllListeners(eventType) {
    if (eventType) {
      this.listeners.delete(eventType);
    } else {
      this.listeners.clear();
    }
  }
  listenerCount(eventType) {
    return this.listeners.get(eventType)?.length || 0;
  }
  eventNames() {
    return Array.from(this.listeners.keys());
  }
}
export {
  EventEmitter
};
