var c = Object.defineProperty;
var t = (i, e) => c(i, "name", { value: e, configurable: !0 });
import { create as o, setIfUndefined as b } from "./map.js";
import { create as d } from "./set.js";
import { from as h } from "./array.js";
class m {
  static {
    t(this, "Observable");
  }
  constructor() {
    this._observers = o();
  }
  /**
   * @param {N} name
   * @param {function} f
   */
  on(e, r) {
    b(this._observers, e, d).add(r);
  }
  /**
   * @param {N} name
   * @param {function} f
   */
  once(e, r) {
    const s = /* @__PURE__ */ t((...f) => {
      this.off(e, s), r(...f);
    }, "_f");
    this.on(e, s);
  }
  /**
   * @param {N} name
   * @param {function} f
   */
  off(e, r) {
    const s = this._observers.get(e);
    s !== void 0 && (s.delete(r), s.size === 0 && this._observers.delete(e));
  }
  /**
   * Emit a named event. All registered event listeners that listen to the
   * specified name will receive the event.
   *
   * @todo This should catch exceptions
   *
   * @param {N} name The event name.
   * @param {Array<any>} args The arguments that are applied to the event listener.
   */
  emit(e, r) {
    return h((this._observers.get(e) || o()).values()).forEach((s) => s(...r));
  }
  destroy() {
    this._observers = o();
  }
}
export {
  m as Observable
};
