import { useEffect as e, useRef as t, useState as n } from "react";
//#region src/services/sse-react.ts
function r(r) {
	let { url: a, withCredentials: o } = r, s = t(r.events ?? {}), [c, l] = n(!1), [u, d] = n(null), f = t(null), p = t(/* @__PURE__ */ new Set()), m = t([]), h = (e) => {
		let t = f.current;
		!t || p.current.has(e) || (m.current.push(t.on(e, (t) => {
			let n = s.current[e];
			if (n) try {
				n(t);
			} catch (e) {
				d(e instanceof Error ? e : Error(String(e)));
			}
		})), p.current.add(e));
	};
	return e(() => {
		s.current = r.events ?? {};
		for (let e of Object.keys(s.current)) h(e);
	}, [r.events]), e(() => {
		let e = !1, t = null, n = new i({
			url: a,
			withCredentials: o === !0
		});
		f.current = n, p.current = /* @__PURE__ */ new Set(), m.current = [];
		for (let e of Object.keys(s.current)) h(e);
		n.connect();
		let r = () => {
			if (e) return;
			let t = n.isConnected();
			l((e) => e === t ? e : t);
		};
		return r(), t = setInterval(r, 750), () => {
			e = !0, t !== null && clearInterval(t);
			for (let e of m.current) e();
			m.current = [], p.current = /* @__PURE__ */ new Set(), f.current = null, n.disconnect(), l(!1);
		};
	}, [a, o]), {
		connected: c,
		lastError: u
	};
}
//#endregion
//#region src/services/sse.ts
var i = class {
	config;
	handlers = /* @__PURE__ */ new Map();
	eventSource = null;
	attachedTypes = /* @__PURE__ */ new Set();
	retryCount = 0;
	reconnectTimer = null;
	constructor(e) {
		this.config = e;
	}
	on(e, t) {
		let n = this.handlers.get(e);
		return n || (n = /* @__PURE__ */ new Set(), this.handlers.set(e, n)), n.add(t), this.eventSource && this.attachEventListener(e), () => {
			this.off(e, t);
		};
	}
	off(e, t) {
		let n = this.handlers.get(e);
		n && (n.delete(t), n.size === 0 && this.handlers.delete(e));
	}
	connect() {
		this.eventSource && this.eventSource.close(), this.attachedTypes = /* @__PURE__ */ new Set();
		let e = this.buildUrl();
		this.eventSource = new EventSource(e, { withCredentials: this.config.withCredentials === !0 }), this.eventSource.onopen = () => {
			this.retryCount = 0;
		}, this.eventSource.onerror = () => {
			this.eventSource &&= (this.eventSource.close(), null);
			let e = this.config.reconnect !== !1, t = this.config.maxRetries ?? 10;
			if (e && this.retryCount < t) {
				this.retryCount += 1;
				let e = (this.config.reconnectInterval ?? 1e3) * Math.min(this.retryCount, 10);
				this.reconnectTimer = setTimeout(() => {
					this.reconnectTimer = null, this.connect();
				}, e);
			}
		};
		for (let e of this.handlers.keys()) this.attachEventListener(e);
	}
	disconnect() {
		this.reconnectTimer !== null && (clearTimeout(this.reconnectTimer), this.reconnectTimer = null), this.eventSource &&= (this.eventSource.close(), null), this.retryCount = 0;
	}
	isConnected() {
		return this.eventSource !== null && this.eventSource.readyState === EventSource.OPEN;
	}
	isFailed() {
		if (this.eventSource !== null || this.reconnectTimer !== null) return !1;
		let e = this.config.maxRetries ?? 10;
		return this.retryCount >= e;
	}
	buildUrl() {
		let e = this.config.url;
		if (this.config.allowQueryTokenAuth !== !0 || !this.config.getAuthToken) return e;
		let t = this.config.getAuthToken();
		return t ? `${e}${e.includes("?") ? "&" : "?"}token=${encodeURIComponent(t)}` : e;
	}
	attachEventListener(e) {
		this.eventSource && (this.attachedTypes.has(e) || (this.attachedTypes.add(e), this.eventSource.addEventListener(e, (t) => {
			let n = this.handlers.get(e);
			if (!n) return;
			let r;
			try {
				r = JSON.parse(t.data);
			} catch {
				r = t.data;
			}
			for (let e of n) e(r);
		})));
	}
};
//#endregion
export { r as n, i as t };

//# sourceMappingURL=sse-D0HLjufB.js.map