import { r as reactExports, U as jsxRuntimeExports, _ as Outlet } from "./worker-entry-ScJyyKL-.js";
import { u as useNavigate, L as Link } from "./router-DeIlR9yC.js";
import { s as supabase } from "./client-Dez4i5AL.js";
import { B as Button } from "./button-BfwfuXeE.js";
import { c as createLucideIcon } from "./createLucideIcon-CzafGoM5.js";
import "node:events";
import "node:async_hooks";
import "node:stream/web";
import "node:stream";
const __iconNode$1 = [
  ["path", { d: "M15 3h6v6", key: "1q9fwt" }],
  ["path", { d: "M10 14 21 3", key: "gplh6r" }],
  ["path", { d: "M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6", key: "a6xqqp" }]
];
const ExternalLink = createLucideIcon("external-link", __iconNode$1);
const __iconNode = [
  ["path", { d: "m16 17 5-5-5-5", key: "1bji2h" }],
  ["path", { d: "M21 12H9", key: "dn1m92" }],
  ["path", { d: "M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4", key: "1uf3rs" }]
];
const LogOut = createLucideIcon("log-out", __iconNode);
function useAuth() {
  const [session, setSession] = reactExports.useState(null);
  const [role, setRole] = reactExports.useState(null);
  const [loading, setLoading] = reactExports.useState(true);
  reactExports.useEffect(() => {
    const { data: sub } = supabase.auth.onAuthStateChange((_e, s) => {
      setSession(s);
      if (s?.user) {
        setTimeout(() => fetchRole(s.user.id), 0);
      } else {
        setRole(null);
      }
    });
    supabase.auth.getSession().then(({ data }) => {
      setSession(data.session);
      if (data.session?.user) fetchRole(data.session.user.id);
      setLoading(false);
    });
    async function fetchRole(uid) {
      const { data } = await supabase.from("user_roles").select("role").eq("user_id", uid);
      const roles = (data ?? []).map((r) => r.role);
      if (roles.includes("admin")) setRole("admin");
      else if (roles.includes("editor")) setRole("editor");
      else setRole(null);
    }
    return () => sub.subscription.unsubscribe();
  }, []);
  return { session, user: session?.user ?? null, role, loading };
}
function AdminLayout() {
  const {
    session,
    role,
    loading,
    user
  } = useAuth();
  const navigate = useNavigate();
  reactExports.useEffect(() => {
    if (!loading && !session) navigate({
      to: "/auth"
    });
  }, [loading, session, navigate]);
  if (loading || !session) {
    return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "min-h-screen flex items-center justify-center text-muted-foreground", children: "Loading..." });
  }
  if (!role) {
    return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "min-h-screen flex items-center justify-center bg-background bg-spotlight px-5", children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "max-w-md text-center bg-onyx border border-border p-10", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("h1", { className: "font-display text-3xl", children: "Awaiting Access" }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("p", { className: "text-muted-foreground mt-4", children: [
        "Your account is signed in but does not have a staff role assigned yet. An existing admin must grant you the ",
        /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "text-gold", children: "admin" }),
        " or",
        " ",
        /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "text-gold", children: "editor" }),
        " role from the backend."
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("p", { className: "text-xs text-muted-foreground mt-4", children: [
        "Signed in as ",
        /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "text-foreground", children: user?.email })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { variant: "goldOutline", className: "mt-6", onClick: async () => {
        await supabase.auth.signOut();
        navigate({
          to: "/auth"
        });
      }, children: "Sign Out" })
    ] }) });
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "min-h-screen bg-background", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx("header", { className: "border-b border-border bg-onyx sticky top-0 z-40 backdrop-blur", children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "container mx-auto px-5 md:px-8 h-16 flex items-center justify-between", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Link, { to: "/admin", className: "flex items-center gap-2", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "font-display text-xl", children: "Luxxen" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "font-display text-xl text-gradient-gold", children: "Admin" })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex items-center gap-3", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "hidden md:inline text-xs uppercase tracking-widest text-gold", children: role }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { asChild: true, variant: "ghost", size: "sm", children: /* @__PURE__ */ jsxRuntimeExports.jsxs(Link, { to: "/", children: [
          "View Site ",
          /* @__PURE__ */ jsxRuntimeExports.jsx(ExternalLink, { className: "ml-1", size: 14 })
        ] }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs(Button, { variant: "outline", size: "sm", onClick: async () => {
          await supabase.auth.signOut();
          navigate({
            to: "/auth"
          });
        }, children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(LogOut, { size: 14 }),
          " Sign Out"
        ] })
      ] })
    ] }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(Outlet, {})
  ] });
}
export {
  AdminLayout as component
};
