import { n as __toESM } from "../_runtime.mjs";
import { u as require_react } from "../_libs/@floating-ui/react-dom+[...].mjs";
import { t as supabase } from "./client-_1GYITl-.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/use-auth-bumQAdtQ.js
var import_react = /* @__PURE__ */ __toESM(require_react());
function useAuth() {
	const [session, setSession] = (0, import_react.useState)(null);
	const [user, setUser] = (0, import_react.useState)(null);
	const [roles, setRoles] = (0, import_react.useState)([]);
	const [loading, setLoading] = (0, import_react.useState)(true);
	(0, import_react.useEffect)(() => {
		let unsub = null;
		const { data } = supabase.auth.onAuthStateChange((_evt, sess) => {
			setSession(sess);
			setUser(sess?.user ?? null);
			if (sess?.user) setTimeout(async () => {
				const { data: rs } = await supabase.from("user_roles").select("role").eq("user_id", sess.user.id);
				setRoles((rs ?? []).map((r) => r.role));
			}, 0);
			else setRoles([]);
		});
		unsub = data;
		supabase.auth.getSession().then(({ data: { session: s } }) => {
			setSession(s);
			setUser(s?.user ?? null);
			setLoading(false);
		});
		return () => {
			unsub?.subscription.unsubscribe();
		};
	}, []);
	return {
		session,
		user,
		roles,
		isAdmin: roles.includes("hq_admin"),
		loading
	};
}
//#endregion
export { useAuth as t };
