import { __commonJSMin } from "../../../../_virtual/rolldown_runtime.js";
import { require_ral } from "./ral.js";
var require_events = /* @__PURE__ */ __commonJSMin(((e) => {
	Object.defineProperty(e, "__esModule", { value: !0 }), e.Emitter = e.Event = void 0;
	var n = require_ral(), r;
	(function(e) {
		let t = { dispose() {} };
		e.None = function() {
			return t;
		};
	})(r || (e.Event = r = {}));
	var i = class {
		add(e, t = null, n) {
			this._callbacks || (this._callbacks = [], this._contexts = []), this._callbacks.push(e), this._contexts.push(t), Array.isArray(n) && n.push({ dispose: () => this.remove(e, t) });
		}
		remove(e, t = null) {
			if (!this._callbacks) return;
			let n = !1;
			for (let r = 0, i = this._callbacks.length; r < i; r++) if (this._callbacks[r] === e) if (this._contexts[r] === t) {
				this._callbacks.splice(r, 1), this._contexts.splice(r, 1);
				return;
			} else n = !0;
			if (n) throw Error("When adding a listener with a context, you should remove it with the same context");
		}
		invoke(...e) {
			if (!this._callbacks) return [];
			let t = [], r = this._callbacks.slice(0), i = this._contexts.slice(0);
			for (let a = 0, o = r.length; a < o; a++) try {
				t.push(r[a].apply(i[a], e));
			} catch (e) {
				(0, n.default)().console.error(e);
			}
			return t;
		}
		isEmpty() {
			return !this._callbacks || this._callbacks.length === 0;
		}
		dispose() {
			this._callbacks = void 0, this._contexts = void 0;
		}
	}, a = class e {
		constructor(e) {
			this._options = e;
		}
		get event() {
			return this._event ||= (t, n, r) => {
				this._callbacks ||= new i(), this._options && this._options.onFirstListenerAdd && this._callbacks.isEmpty() && this._options.onFirstListenerAdd(this), this._callbacks.add(t, n);
				let a = { dispose: () => {
					this._callbacks && (this._callbacks.remove(t, n), a.dispose = e._noop, this._options && this._options.onLastListenerRemove && this._callbacks.isEmpty() && this._options.onLastListenerRemove(this));
				} };
				return Array.isArray(r) && r.push(a), a;
			}, this._event;
		}
		fire(e) {
			this._callbacks && this._callbacks.invoke.call(this._callbacks, e);
		}
		dispose() {
			this._callbacks &&= (this._callbacks.dispose(), void 0);
		}
	};
	e.Emitter = a, a._noop = function() {};
}));
export default require_events();
export { require_events };
