import { __exports as l } from "../../../../../../../_virtual/events2.js";
import { __require as p } from "./ral.js";
var o;
function b() {
  if (o) return l;
  o = 1, Object.defineProperty(l, "__esModule", { value: !0 }), l.Emitter = l.Event = void 0;
  const h = p();
  var c;
  (function(r) {
    const s = { dispose() {
    } };
    r.None = function() {
      return s;
    };
  })(c || (l.Event = c = {}));
  class _ {
    add(s, i = null, e) {
      this._callbacks || (this._callbacks = [], this._contexts = []), this._callbacks.push(s), this._contexts.push(i), Array.isArray(e) && e.push({ dispose: () => this.remove(s, i) });
    }
    remove(s, i = null) {
      if (!this._callbacks)
        return;
      let e = !1;
      for (let t = 0, n = this._callbacks.length; t < n; t++)
        if (this._callbacks[t] === s)
          if (this._contexts[t] === i) {
            this._callbacks.splice(t, 1), this._contexts.splice(t, 1);
            return;
          } else
            e = !0;
      if (e)
        throw new Error("When adding a listener with a context, you should remove it with the same context");
    }
    invoke(...s) {
      if (!this._callbacks)
        return [];
      const i = [], e = this._callbacks.slice(0), t = this._contexts.slice(0);
      for (let n = 0, u = e.length; n < u; n++)
        try {
          i.push(e[n].apply(t[n], s));
        } catch (f) {
          (0, h.default)().console.error(f);
        }
      return i;
    }
    isEmpty() {
      return !this._callbacks || this._callbacks.length === 0;
    }
    dispose() {
      this._callbacks = void 0, this._contexts = void 0;
    }
  }
  class a {
    constructor(s) {
      this._options = s;
    }
    /**
     * For the public to allow to subscribe
     * to events from this Emitter
     */
    get event() {
      return this._event || (this._event = (s, i, e) => {
        this._callbacks || (this._callbacks = new _()), this._options && this._options.onFirstListenerAdd && this._callbacks.isEmpty() && this._options.onFirstListenerAdd(this), this._callbacks.add(s, i);
        const t = {
          dispose: () => {
            this._callbacks && (this._callbacks.remove(s, i), t.dispose = a._noop, this._options && this._options.onLastListenerRemove && this._callbacks.isEmpty() && this._options.onLastListenerRemove(this));
          }
        };
        return Array.isArray(e) && e.push(t), t;
      }), this._event;
    }
    /**
     * To be kept private to fire an event to
     * subscribers
     */
    fire(s) {
      this._callbacks && this._callbacks.invoke.call(this._callbacks, s);
    }
    dispose() {
      this._callbacks && (this._callbacks.dispose(), this._callbacks = void 0);
    }
  }
  return l.Emitter = a, a._noop = function() {
  }, l;
}
export {
  b as __require
};
