import { useState, useCallback, useEffect } from "react";
import { s as supabase } from "./client-CJCZGN4z.js";
function useAuth() {
  const [session, setSession] = useState(null);
  const [user, setUser] = useState(null);
  const [roles, setRoles] = useState([]);
  const [loading, setLoading] = useState(true);
  const loadRoles = useCallback(async (uid) => {
    if (!uid) return setRoles([]);
    const { data } = await supabase.from("user_roles").select("role").eq("user_id", uid);
    setRoles((data ?? []).map((r) => r.role));
  }, []);
  useEffect(() => {
    const { data: { subscription } } = supabase.auth.onAuthStateChange((_evt, s) => {
      setSession(s);
      setUser(s?.user ?? null);
      setTimeout(() => loadRoles(s?.user?.id), 0);
    });
    supabase.auth.getSession().then(({ data }) => {
      setSession(data.session);
      setUser(data.session?.user ?? null);
      loadRoles(data.session?.user?.id).finally(() => setLoading(false));
    });
    return () => subscription.unsubscribe();
  }, [loadRoles]);
  const hasRole = (r) => roles.includes(r);
  const isStaff = roles.some((r) => r === "admin" || r === "editor" || r === "writer");
  return { session, user, roles, loading, hasRole, isStaff };
}
export {
  useAuth as u
};
