import { jsx, jsxs } from "react/jsx-runtime";
import { useRouterState, useNavigate, Link } from "@tanstack/react-router";
import { u as useAuth } from "./use-auth-DoD1gvcu.js";
import { s as supabase } from "./client-CJCZGN4z.js";
import { LayoutDashboard, FileText, Inbox, Radio, MessageSquare, Megaphone, Users, Home, LogOut, BarChart3 } from "lucide-react";
import { B as Button } from "./button-BXrfXN_b.js";
const NAV = [
  { to: "/admin", label: "Dashboard", icon: LayoutDashboard, exact: true },
  { to: "/admin/articles", label: "Articles", icon: FileText },
  { to: "/admin/submissions", label: "Citizen Submissions", icon: Inbox },
  { to: "/admin/breaking", label: "Breaking News", icon: Radio },
  { to: "/admin/polls", label: "Polls", icon: MessageSquare },
  { to: "/admin/ads", label: "Ad Slots", icon: Megaphone },
  { to: "/admin/users", label: "Users & Roles", icon: Users, adminOnly: true }
];
function AdminShell({ children, title }) {
  const { user, roles, loading, isStaff, hasRole } = useAuth();
  const path = useRouterState({ select: (s) => s.location.pathname });
  const navigate = useNavigate();
  if (loading) return /* @__PURE__ */ jsx("div", { className: "p-8 text-sm text-muted-foreground", children: "Loading…" });
  if (!isStaff) {
    return /* @__PURE__ */ jsx("div", { className: "min-h-screen grid place-items-center px-4", children: /* @__PURE__ */ jsxs("div", { className: "max-w-md text-center", children: [
      /* @__PURE__ */ jsx("h1", { className: "font-serif text-2xl font-black", children: "Access restricted" }),
      /* @__PURE__ */ jsx("p", { className: "text-muted-foreground mt-2", children: "Your account doesn't have newsroom access. Ask an admin to grant you a role." }),
      /* @__PURE__ */ jsxs("div", { className: "mt-4 flex gap-2 justify-center", children: [
        /* @__PURE__ */ jsx(Button, { asChild: true, variant: "outline", children: /* @__PURE__ */ jsx(Link, { to: "/", children: "Back to site" }) }),
        /* @__PURE__ */ jsx(Button, { variant: "ghost", onClick: async () => {
          await supabase.auth.signOut();
          navigate({ to: "/auth" });
        }, children: "Sign out" })
      ] })
    ] }) });
  }
  return /* @__PURE__ */ jsxs("div", { className: "min-h-screen flex bg-background text-foreground", children: [
    /* @__PURE__ */ jsxs("aside", { className: "w-60 border-r border-border bg-surface flex flex-col", children: [
      /* @__PURE__ */ jsxs("div", { className: "p-4 border-b border-border", children: [
        /* @__PURE__ */ jsxs(Link, { to: "/", className: "font-serif text-lg font-black italic text-brand", children: [
          "MALAWI",
          /* @__PURE__ */ jsx("span", { className: "text-brand-accent", children: "TODAY" })
        ] }),
        /* @__PURE__ */ jsx("p", { className: "text-[10px] uppercase tracking-widest text-muted-foreground mt-1", children: "Newsroom" })
      ] }),
      /* @__PURE__ */ jsx("nav", { className: "flex-1 p-2 space-y-1 text-sm", children: NAV.filter((n) => !("adminOnly" in n && n.adminOnly) || hasRole("admin")).map((n) => {
        const Icon = n.icon;
        const active = "exact" in n && n.exact ? path === n.to : path.startsWith(n.to);
        return /* @__PURE__ */ jsxs(
          Link,
          {
            to: n.to,
            className: `flex items-center gap-2 px-3 py-2 rounded-sm transition-colors ${active ? "bg-brand text-primary-foreground" : "hover:bg-muted"}`,
            children: [
              /* @__PURE__ */ jsx(Icon, { className: "w-4 h-4" }),
              " ",
              n.label
            ]
          },
          n.to
        );
      }) }),
      /* @__PURE__ */ jsxs("div", { className: "border-t border-border p-3 text-xs", children: [
        /* @__PURE__ */ jsx("div", { className: "truncate font-bold", children: user?.email }),
        /* @__PURE__ */ jsx("div", { className: "text-muted-foreground uppercase tracking-widest text-[10px] mt-1", children: roles.join(" · ") || "no role" }),
        /* @__PURE__ */ jsxs("div", { className: "flex gap-1 mt-3", children: [
          /* @__PURE__ */ jsx(Button, { asChild: true, variant: "outline", size: "sm", className: "flex-1", children: /* @__PURE__ */ jsxs(Link, { to: "/", children: [
            /* @__PURE__ */ jsx(Home, { className: "w-3 h-3 mr-1" }),
            " Site"
          ] }) }),
          /* @__PURE__ */ jsx(Button, { variant: "ghost", size: "sm", onClick: async () => {
            await supabase.auth.signOut();
            navigate({ to: "/auth" });
          }, children: /* @__PURE__ */ jsx(LogOut, { className: "w-3 h-3" }) })
        ] })
      ] })
    ] }),
    /* @__PURE__ */ jsxs("main", { className: "flex-1 overflow-auto", children: [
      /* @__PURE__ */ jsx("header", { className: "border-b border-border bg-surface px-6 py-4 sticky top-0 z-10", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
        /* @__PURE__ */ jsx(BarChart3, { className: "w-4 h-4 text-brand-accent" }),
        /* @__PURE__ */ jsx("h1", { className: "font-serif text-xl font-black", children: title })
      ] }) }),
      /* @__PURE__ */ jsx("div", { className: "p-6", children })
    ] })
  ] });
}
export {
  AdminShell as A
};
