"use strict";
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
class EventCenter {
  constructor() {
    // 存储所有事件监听器
    __publicField(this, "listeners", /* @__PURE__ */ new Map());
    // 用于生成唯一的监听器ID
    __publicField(this, "listenerIdCounter", 0);
  }
  /**
   * 订阅事件
   * @param event 事件名称
   * @param callback 回调函数
   * @returns 返回取消订阅的函数
   */
  on(event, callback) {
    if (!this.isValidEvent(event) || !this.isValidCallback(callback)) {
      throw new Error("Invalid event name or callback function");
    }
    const listenerId = this.generateListenerId();
    const listener = {
      callback,
      once: false,
      id: listenerId
    };
    if (!this.listeners.has(event)) {
      this.listeners.set(event, []);
    }
    this.listeners.get(event).push(listener);
    return () => this.off(event, listenerId);
  }
  /**
   * 订阅事件（只触发一次）
   * @param event 事件名称
   * @param callback 回调函数
   * @returns 返回取消订阅的函数
   */
  once(event, callback) {
    if (!this.isValidEvent(event) || !this.isValidCallback(callback)) {
      throw new Error("Invalid event name or callback function");
    }
    const listenerId = this.generateListenerId();
    const listener = {
      callback,
      once: true,
      id: listenerId
    };
    if (!this.listeners.has(event)) {
      this.listeners.set(event, []);
    }
    this.listeners.get(event).push(listener);
    return () => this.off(event, listenerId);
  }
  /**
   * 取消订阅事件
   * @param event 事件名称
   * @param callbackOrId 回调函数或监听器ID
   */
  off(event, callbackOrId) {
    if (!this.isValidEvent(event)) {
      return;
    }
    const listeners = this.listeners.get(event);
    if (!listeners) {
      return;
    }
    if (callbackOrId === void 0) {
      this.listeners.delete(event);
      return;
    }
    const isId = typeof callbackOrId === "string";
    const filteredListeners = listeners.filter((listener) => {
      if (isId) {
        return listener.id !== callbackOrId;
      } else {
        return listener.callback !== callbackOrId;
      }
    });
    if (filteredListeners.length === 0) {
      this.listeners.delete(event);
    } else {
      this.listeners.set(event, filteredListeners);
    }
  }
  /**
   * 触发事件
   * @param event 事件名称
   * @param data 传递给回调函数的数据
   */
  emit(event, data) {
    if (!this.isValidEvent(event)) {
      return;
    }
    const listeners = this.listeners.get(event);
    if (!listeners || listeners.length === 0) {
      return;
    }
    const listenersToExecute = [...listeners];
    const onceListeners = [];
    listenersToExecute.forEach((listener) => {
      try {
        listener.callback(data);
        if (listener.once) {
          onceListeners.push(listener.id);
        }
      } catch (error) {
        console.error(`Error executing event listener for "${event}":`, error);
      }
    });
    if (onceListeners.length > 0) {
      onceListeners.forEach((id) => this.off(event, id));
    }
  }
  /**
   * 获取指定事件的监听器数量
   * @param event 事件名称
   * @returns 监听器数量
   */
  getListenerCount(event) {
    if (event) {
      const listeners = this.listeners.get(event);
      return listeners ? listeners.length : 0;
    }
    let totalCount = 0;
    for (const listeners of this.listeners.values()) {
      totalCount += listeners.length;
    }
    return totalCount;
  }
  /**
   * 获取所有已注册的事件名称
   * @returns 事件名称数组
   */
  getEventNames() {
    return Array.from(this.listeners.keys());
  }
  /**
   * 检查是否有指定事件的监听器
   * @param event 事件名称
   * @returns 是否有监听器
   */
  hasListeners(event) {
    return this.getListenerCount(event) > 0;
  }
  /**
   * 清除所有事件监听器
   */
  clear() {
    this.listeners.clear();
  }
  /**
   * 清除指定事件的所有监听器
   * @param event 事件名称
   */
  clearEvent(event) {
    if (this.isValidEvent(event)) {
      this.listeners.delete(event);
    }
  }
  /**
   * 生成唯一的监听器ID
   * @returns 监听器ID
   */
  generateListenerId() {
    return `listener_${++this.listenerIdCounter}_${Date.now()}`;
  }
  /**
   * 验证事件名称是否有效
   * @param event 事件名称
   * @returns 是否有效
   */
  isValidEvent(event) {
    return typeof event === "string" && event.trim().length > 0;
  }
  /**
   * 验证回调函数是否有效
   * @param callback 回调函数
   * @returns 是否有效
   */
  isValidCallback(callback) {
    return typeof callback === "function";
  }
}
const eventCenter = new EventCenter();
eventCenter.on.bind(eventCenter);
eventCenter.once.bind(eventCenter);
eventCenter.off.bind(eventCenter);
eventCenter.emit.bind(eventCenter);
eventCenter.clear.bind(eventCenter);
eventCenter.getListenerCount.bind(eventCenter);
eventCenter.getEventNames.bind(eventCenter);
eventCenter.hasListeners.bind(eventCenter);
exports.EventCenter = EventCenter;
exports.default = eventCenter;
exports.eventCenter = eventCenter;
