var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/effects/index.ts
var effects_exports = {};
__export(effects_exports, {
  EffectsManager: () => EffectsManager
});
module.exports = __toCommonJS(effects_exports);
var EffectsManager = class {
  constructor() {
    this.listeners = /* @__PURE__ */ new Map();
  }
  on(event, callback) {
    var _a;
    if (!this.listeners.has(event))
      this.listeners.set(event, /* @__PURE__ */ new Set());
    (_a = this.listeners.get(event)) == null ? void 0 : _a.add(callback);
    return () => {
      this.off(event, callback);
    };
  }
  // 设置单例监听器，只能存在一个，新的会替换旧的
  only(event, callback) {
    var _a, _b;
    if (this.listeners.has(event)) {
      (_a = this.listeners.get(event)) == null ? void 0 : _a.clear();
    } else {
      this.listeners.set(event, /* @__PURE__ */ new Set());
    }
    (_b = this.listeners.get(event)) == null ? void 0 : _b.add(callback);
    return () => {
      this.off(event, callback);
    };
  }
  off(event, callback) {
    var _a;
    (_a = this.listeners.get(event)) == null ? void 0 : _a.delete(callback);
  }
  // 模块销毁时，删除所有以模块名开头的监听
  offByModuleDestroy(module2) {
    Array.from(this.listeners.keys()).forEach((item) => {
      if (item.startsWith(module2)) {
        this.listeners.delete(item);
      }
    });
  }
  once(event, callback) {
    const wrapper = async (payload) => {
      await callback(payload);
      this.off(event, wrapper);
    };
    return this.on(event, wrapper);
  }
  async emit(event, payload) {
    const callbacks = this.listeners.get(event);
    if (!callbacks)
      return { status: true, data: [] };
    const res = [];
    await Promise.all(
      Array.from(callbacks).reverse().map(async (cb) => {
        const result = await cb(payload);
        if (result !== void 0 && typeof result === "object" && "status" in result) {
          res.push(result);
        }
      })
    );
    const isAllPass = res.every((r) => (r == null ? void 0 : r.status) !== false);
    return { status: isAllPass, data: res };
  }
  clear() {
    this.listeners.clear();
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  EffectsManager
});
