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