"use strict";
class Bus {
  constructor() {
    this.listeners = /* @__PURE__ */ new Map();
    this.emitted = /* @__PURE__ */ new Map();
    this.onceListeners = /* @__PURE__ */ new Map();
  }
  on(evtName, listener) {
    const target = this.listeners.get(evtName) || [];
    target.push(listener);
    this.listeners.set(evtName, target);
    if (this.emitted.has(evtName)) {
      listener(...this.emitted.get(evtName));
    }
  }
  once(evtName, listener) {
    if (this.emitted.has(evtName)) {
      listener(...this.emitted.get(evtName));
      return;
    }
    const onceWrapper = (...args) => {
      listener(...args);
      this.off(evtName, onceWrapper);
    };
    const onceMap = this.onceListeners.get(evtName) || /* @__PURE__ */ new Map();
    onceMap.set(listener, onceWrapper);
    this.onceListeners.set(evtName, onceMap);
    this.on(evtName, onceWrapper);
  }
  emit(evtName, ...args) {
    this.emitted.set(evtName, args);
    const listeners = this.listeners.get(evtName) || [];
    [...listeners].forEach((func) => func(...args));
  }
  off(evtName, listener) {
    if (listener) {
      const onceMap = this.onceListeners.get(evtName);
      if (onceMap && onceMap.has(listener)) {
        const wrapper = onceMap.get(listener);
        this._removeListener(evtName, wrapper);
        onceMap.delete(listener);
        return;
      }
      this._removeListener(evtName, listener);
    } else {
      this.listeners.delete(evtName);
      this.onceListeners.delete(evtName);
      this.emitted.delete(evtName);
    }
  }
  // 私有方法：从指定事件中移除单个监听器
  _removeListener(evtName, listener) {
    const listeners = this.listeners.get(evtName);
    if (!listeners)
      return;
    const index = listeners.indexOf(listener);
    if (index > -1) {
      listeners.splice(index, 1);
      if (listeners.length === 0) {
        this.listeners.delete(evtName);
      }
    }
  }
}
exports.Bus = Bus;
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/uni_modules/tdesign-uniapp/components/common/bus.js.map
