import { ApiClientEvents as e } from "../../events/api-client-events.js";
import { eventBus as t } from "../../services/eventEmitter.js";
import "../../services/index.js";
import { useStudio as n } from "../../providers/StudioProvider.js";
import { useGetHealthInfo as r, useWorkerAuth as i, useWorkerAuthTokenRefresh as a } from "../../hooks/useAuth.js";
import { useCallback as o, useEffect as s, useRef as c, useState as l } from "react";
import { jsx as u, jsxs as d } from "react/jsx-runtime";
import { useQueryClient as f } from "@tanstack/react-query";
import { RefreshCw as p, X as m } from "lucide-react";
//#region src/features/health/LocalHealthCheck.tsx
var h = {
	None: 0,
	Refresh: 1,
	Login: 2,
	Debug: 3,
	Connection: 4
}, g = () => {
	let { environment: g } = n(), [_, v] = l(h.None), y = i(), b = a(), x = r(!1), S = f(), C = c(b);
	C.current = b;
	let w = c(y);
	w.current = y;
	let T = c(x);
	T.current = x;
	let E = o(() => {
		T.current.refetch();
	}, []), D = o(() => {
		C.current.mutate();
	}, []), O = o(async () => {
		try {
			let e = "local";
			g.getIdToken && (e = await g.getIdToken()), w.current.mutate({ hubLoginRequestDto: { idToken: e } });
		} catch {
			v(h.Debug);
		}
	}, [g]);
	s(() => {
		if (_ !== h.Connection) return;
		let e = setInterval(E, 5e3);
		return () => clearInterval(e);
	}, [_, E]), s(() => {
		x.data && (v(h.None), S.invalidateQueries());
	}, [x.data, S]), s(() => {
		b.error && v(h.Login);
	}, [b.error]), s(() => {
		b.data?.status === 200 && v(h.None);
	}, [b.data]), s(() => {
		y.error && v(h.Debug);
	}, [y.error]), s(() => {
		y.data?.status === 200 && v(h.None);
	}, [y.data]), s(() => {
		_ === h.Refresh ? D() : _ === h.Login && O();
	}, [
		_,
		D,
		O
	]), s(() => {
		let n = t.on(e.UNAUTHORIZED, () => {
			v(h.Refresh);
		}), r = t.on(e.ERR_NETWORK, () => {
			v(h.Connection);
		});
		return () => {
			n(), r();
		};
	}, []);
	let [k, A] = l(!1), j = _ >= h.Debug && !k;
	return s(() => {
		_ < h.Debug && A(!1);
	}, [_]), j ? /* @__PURE__ */ u("div", {
		className: "bg-destructive/10 border-destructive/30 fixed inset-x-0 bottom-0 z-50 border-t px-4 py-3",
		children: /* @__PURE__ */ d("div", {
			className: "flex items-center justify-between",
			children: [/* @__PURE__ */ d("div", {
				className: "flex items-center gap-3",
				children: [/* @__PURE__ */ u("div", { className: "bg-destructive h-2 w-2 rounded-full" }), /* @__PURE__ */ d("div", { children: [/* @__PURE__ */ u("p", {
					className: "text-foreground text-sm font-semibold",
					children: "Connection issues detected"
				}), /* @__PURE__ */ d("p", {
					className: "text-muted-foreground text-xs",
					children: [
						"Please make sure the environment",
						" ",
						/* @__PURE__ */ d("strong", { children: [
							g.name,
							" (",
							g.id,
							")"
						] }),
						" ",
						"is properly configured and running."
					]
				})] })]
			}), /* @__PURE__ */ d("div", {
				className: "flex items-center gap-2",
				children: [_ === h.Connection && /* @__PURE__ */ d("button", {
					onClick: E,
					className: "bg-primary flex items-center gap-2 rounded-md px-3 py-1.5 text-sm text-white transition-colors disabled:cursor-not-allowed disabled:opacity-50",
					children: [/* @__PURE__ */ u(p, { className: `h-3 w-3 ${x.isLoading ? "animate-spin" : ""}` }), "Retry"]
				}), /* @__PURE__ */ u("button", {
					onClick: () => A(!0),
					className: "text-muted-foreground hover:text-foreground rounded-xs p-1 transition-colors",
					children: /* @__PURE__ */ u(m, { className: "h-4 w-4" })
				})]
			})]
		})
	}) : null;
};
//#endregion
export { g as default };
