import { jsx, jsxs } from "react/jsx-runtime";
import { useNavigate, useLocation, Outlet, Link } from "@tanstack/react-router";
import { ArrowUpRight, LogOut, X, Menu, Package, FileSearch, Users, Settings } from "lucide-react";
import { useState, useEffect } from "react";
import { l as logo } from "./logo-DIdLvJAL.js";
import { s as supabase } from "./client-Dz5aErML.js";
import { T as ThemeToggle } from "./ThemeToggle-FQS9vN6L.js";
import "@supabase/supabase-js";
function useAdminAuth() {
  const [session, setSession] = useState(null);
  const [user, setUser] = useState(null);
  const [isAdmin, setIsAdmin] = useState(null);
  const [loading, setLoading] = useState(true);
  useEffect(() => {
    let mounted = true;
    let authCheckId = 0;
    const checkAdminSession = async (sess) => {
      const currentCheck = ++authCheckId;
      if (!mounted) return;
      setLoading(true);
      setSession(sess);
      setUser(sess?.user ?? null);
      setIsAdmin(null);
      if (!sess?.user) {
        if (!mounted || currentCheck !== authCheckId) return;
        setIsAdmin(false);
        setLoading(false);
        return;
      }
      const { data } = await supabase.from("user_roles").select("role").eq("user_id", sess.user.id).eq("role", "admin").maybeSingle();
      if (!mounted || currentCheck !== authCheckId) return;
      setIsAdmin(!!data);
      setLoading(false);
    };
    const { data: sub } = supabase.auth.onAuthStateChange((_event, sess) => {
      setTimeout(() => {
        void checkAdminSession(sess);
      }, 0);
    });
    supabase.auth.getSession().then(({ data: { session: s } }) => {
      void checkAdminSession(s);
    });
    return () => {
      mounted = false;
      sub.subscription.unsubscribe();
    };
  }, []);
  return { session, user, isAdmin, loading };
}
const ALLOWED_ADMIN_EMAIL = "ozinpergam29@gmail.com";
const adminNavItems = [{
  to: "/adminpergam",
  label: "Produk",
  icon: Package,
  match: (path) => path === "/adminpergam" || path === "/adminpergam/" || path.startsWith("/adminpergam/products/") && path !== "/adminpergam/products/new"
}, {
  to: "/adminpergam/laporan",
  label: "Laporan",
  icon: FileSearch,
  match: (path) => path.startsWith("/adminpergam/laporan")
}, {
  to: "/adminpergam/users",
  label: "Akun Admin",
  icon: Users,
  match: (path) => path.startsWith("/adminpergam/users")
}, {
  to: "/adminpergam/settings",
  label: "Kontak",
  icon: Settings,
  match: (path) => path.startsWith("/adminpergam/settings")
}];
function AdminLayout() {
  const {
    user,
    isAdmin,
    loading
  } = useAdminAuth();
  const navigate = useNavigate();
  const location = useLocation();
  const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
  const isLogin = location.pathname === "/adminpergam/login";
  const isSetup = location.pathname === "/adminpergam/setup";
  useEffect(() => {
    if (loading || isLogin || isSetup) return;
    if (!user) {
      navigate({
        to: "/adminpergam/login"
      });
    }
  }, [user, loading, navigate, isLogin, isSetup]);
  useEffect(() => {
    setMobileMenuOpen(false);
  }, [location.pathname]);
  if (isLogin || isSetup) return /* @__PURE__ */ jsx(Outlet, {});
  if (loading || user && isAdmin !== true && isAdmin !== false) {
    return /* @__PURE__ */ jsx("div", { className: "min-h-screen grid place-items-center text-muted-foreground text-sm", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-3", children: [
      /* @__PURE__ */ jsx("div", { className: "h-8 w-8 animate-spin rounded-full border-2 border-primary border-t-transparent" }),
      /* @__PURE__ */ jsx("span", { children: "Memverifikasi Akses..." })
    ] }) });
  }
  if (!user) return null;
  const emailAllowed = (user.email ?? "").toLowerCase() === ALLOWED_ADMIN_EMAIL.toLowerCase();
  const handleLogout = async () => {
    setMobileMenuOpen(false);
    await supabase.auth.signOut();
    navigate({
      to: "/adminpergam/login"
    });
  };
  if (isAdmin !== true || !emailAllowed) {
    return /* @__PURE__ */ jsx("div", { className: "min-h-screen grid place-items-center px-4", children: /* @__PURE__ */ jsxs("div", { className: "card-surface rounded-2xl p-8 max-w-md text-center", children: [
      /* @__PURE__ */ jsx("h1", { className: "font-display text-xl font-bold mb-2", children: "Akses Ditolak" }),
      /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground mb-4", children: "Akun ini tidak memiliki izin untuk mengakses halaman admin." }),
      /* @__PURE__ */ jsx("button", { onClick: handleLogout, className: "px-4 py-2 rounded-full bg-primary text-primary-foreground text-sm font-medium", children: "Logout" })
    ] }) });
  }
  const renderNav = (isMobile = false) => /* @__PURE__ */ jsx("nav", { className: isMobile ? "grid gap-2" : "grid gap-1.5", children: adminNavItems.map((item) => {
    const Icon = item.icon;
    const active = item.match(location.pathname);
    return /* @__PURE__ */ jsxs(Link, { to: item.to, onClick: isMobile ? () => setMobileMenuOpen(false) : void 0, className: `flex items-center gap-3 rounded-xl border px-3 py-2.5 text-sm font-bold transition ${active ? "border-primary/30 bg-primary/10 text-primary" : "border-transparent text-muted-foreground hover:border-border hover:bg-card/65 hover:text-foreground"}`, children: [
      /* @__PURE__ */ jsx(Icon, { className: "h-4 w-4 shrink-0" }),
      /* @__PURE__ */ jsx("span", { children: item.label })
    ] }, item.to);
  }) });
  return /* @__PURE__ */ jsx("div", { className: "page-shell min-h-screen", children: /* @__PURE__ */ jsxs("div", { className: "flex min-h-screen", children: [
    /* @__PURE__ */ jsxs("aside", { className: "sticky top-0 hidden h-screen w-72 shrink-0 border-r border-border bg-card/80 px-4 py-5 backdrop-blur-2xl lg:flex lg:flex-col", children: [
      /* @__PURE__ */ jsxs(Link, { to: "/adminpergam", className: "flex items-center gap-3 rounded-2xl px-2 py-1", children: [
        /* @__PURE__ */ jsx("span", { className: "grid h-11 w-11 shrink-0 place-items-center overflow-hidden", children: /* @__PURE__ */ jsx("img", { src: logo, alt: "Pergam Store", className: "h-full w-full object-contain drop-shadow-sm" }) }),
        /* @__PURE__ */ jsxs("span", { className: "min-w-0", children: [
          /* @__PURE__ */ jsx("span", { className: "block font-display text-base font-bold leading-tight text-foreground", children: "Admin Pergam" }),
          /* @__PURE__ */ jsx("span", { className: "mt-0.5 inline-flex rounded-full border border-primary/20 bg-primary/10 px-2 py-0.5 text-[10px] font-bold uppercase tracking-wider text-primary", children: "Live" })
        ] })
      ] }),
      /* @__PURE__ */ jsx("div", { className: "mt-6 flex-1 overflow-y-auto pr-1", children: renderNav() }),
      /* @__PURE__ */ jsxs("div", { className: "mt-5 border-t border-border pt-4", children: [
        /* @__PURE__ */ jsxs("div", { className: "mb-3 min-w-0 rounded-xl border border-border bg-background/25 px-3 py-2", children: [
          /* @__PURE__ */ jsx("span", { className: "block text-[10px] font-bold uppercase tracking-wider text-muted-foreground", children: "Login sebagai" }),
          /* @__PURE__ */ jsx("span", { className: "mt-1 block truncate text-xs font-semibold text-foreground", children: user.email })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "grid gap-2", children: [
          /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2 rounded-xl border border-border bg-background/25 px-3 py-2", children: [
            /* @__PURE__ */ jsx("span", { className: "text-xs font-bold text-muted-foreground", children: "Tema" }),
            /* @__PURE__ */ jsx(ThemeToggle, {})
          ] }),
          /* @__PURE__ */ jsxs("a", { href: "/", target: "_blank", rel: "noopener noreferrer", className: "flex items-center justify-between gap-3 rounded-xl border border-border px-3 py-2.5 text-sm font-bold text-muted-foreground transition hover:border-primary/40 hover:bg-primary/10 hover:text-primary", children: [
            /* @__PURE__ */ jsx("span", { children: "Lihat Website" }),
            /* @__PURE__ */ jsx(ArrowUpRight, { className: "h-4 w-4 shrink-0" })
          ] }),
          /* @__PURE__ */ jsxs("button", { onClick: handleLogout, className: "flex items-center gap-3 rounded-xl border border-border px-3 py-2.5 text-left text-sm font-bold text-muted-foreground transition hover:border-destructive/40 hover:bg-destructive/10 hover:text-destructive", children: [
            /* @__PURE__ */ jsx(LogOut, { className: "h-4 w-4 shrink-0" }),
            "Logout"
          ] })
        ] })
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "flex min-h-screen min-w-0 flex-1 flex-col", children: [
      /* @__PURE__ */ jsx("header", { className: "sticky top-0 z-40 border-b border-border bg-card/90 backdrop-blur-2xl lg:hidden", children: /* @__PURE__ */ jsxs("div", { className: "flex h-16 items-center justify-between px-4 sm:px-6", children: [
        /* @__PURE__ */ jsxs(Link, { to: "/adminpergam", className: "flex min-w-0 items-center gap-2", children: [
          /* @__PURE__ */ jsx("span", { className: "grid h-9 w-9 shrink-0 place-items-center overflow-hidden", children: /* @__PURE__ */ jsx("img", { src: logo, alt: "Pergam Store", className: "h-full w-full object-contain drop-shadow-sm" }) }),
          /* @__PURE__ */ jsxs("span", { className: "min-w-0", children: [
            /* @__PURE__ */ jsx("span", { className: "block truncate font-display font-bold text-foreground", children: "Admin Pergam" }),
            /* @__PURE__ */ jsx("span", { className: "block text-[10px] font-bold uppercase tracking-wider text-primary", children: "Live" })
          ] })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
          /* @__PURE__ */ jsx(ThemeToggle, {}),
          /* @__PURE__ */ jsx("button", { type: "button", onClick: () => setMobileMenuOpen((open) => !open), "aria-expanded": mobileMenuOpen, "aria-label": mobileMenuOpen ? "Tutup menu admin" : "Buka menu admin", className: "grid h-10 w-10 place-items-center rounded-full border border-border bg-background/35 text-foreground transition hover:border-primary/40 hover:bg-primary/10", children: mobileMenuOpen ? /* @__PURE__ */ jsx(X, { className: "h-5 w-5" }) : /* @__PURE__ */ jsx(Menu, { className: "h-5 w-5" }) })
        ] })
      ] }) }),
      mobileMenuOpen && /* @__PURE__ */ jsxs("div", { className: "fixed inset-0 z-50 lg:hidden", children: [
        /* @__PURE__ */ jsx("button", { type: "button", "aria-label": "Tutup menu admin", onClick: () => setMobileMenuOpen(false), className: "absolute inset-0 h-full w-full bg-background/60 backdrop-blur-sm" }),
        /* @__PURE__ */ jsxs("aside", { className: "absolute right-0 top-0 flex h-full w-80 max-w-[calc(100vw-2rem)] flex-col border-l border-border bg-card/95 p-4 shadow-2xl backdrop-blur-2xl", children: [
          /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-3 border-b border-border pb-4", children: [
            /* @__PURE__ */ jsxs(Link, { to: "/adminpergam", onClick: () => setMobileMenuOpen(false), className: "flex min-w-0 items-center gap-2", children: [
              /* @__PURE__ */ jsx("span", { className: "grid h-10 w-10 shrink-0 place-items-center overflow-hidden", children: /* @__PURE__ */ jsx("img", { src: logo, alt: "Pergam Store", className: "h-full w-full object-contain drop-shadow-sm" }) }),
              /* @__PURE__ */ jsxs("span", { className: "min-w-0", children: [
                /* @__PURE__ */ jsx("span", { className: "block truncate font-display font-bold text-foreground", children: "Admin Pergam" }),
                /* @__PURE__ */ jsx("span", { className: "block text-[10px] font-bold uppercase tracking-wider text-primary", children: "Menu" })
              ] })
            ] }),
            /* @__PURE__ */ jsx("button", { type: "button", onClick: () => setMobileMenuOpen(false), "aria-label": "Tutup menu admin", className: "grid h-10 w-10 shrink-0 place-items-center rounded-full border border-border bg-background/35 text-foreground transition hover:border-primary/40 hover:bg-primary/10", children: /* @__PURE__ */ jsx(X, { className: "h-5 w-5" }) })
          ] }),
          /* @__PURE__ */ jsx("div", { className: "mt-4 flex-1 overflow-y-auto pr-1", children: renderNav(true) }),
          /* @__PURE__ */ jsxs("div", { className: "mt-4 grid gap-2 border-t border-border pt-4", children: [
            /* @__PURE__ */ jsx("div", { className: "truncate rounded-xl border border-border bg-background/25 px-3 py-2 text-xs font-semibold text-muted-foreground", children: user.email }),
            /* @__PURE__ */ jsxs("a", { href: "/", target: "_blank", rel: "noopener noreferrer", className: "inline-flex items-center justify-center gap-2 rounded-xl border border-border px-3 py-2.5 text-xs font-bold text-muted-foreground transition hover:border-primary/40 hover:text-primary", children: [
              "Website",
              /* @__PURE__ */ jsx(ArrowUpRight, { className: "h-3.5 w-3.5" })
            ] }),
            /* @__PURE__ */ jsxs("button", { onClick: handleLogout, className: "inline-flex items-center justify-center gap-2 rounded-xl border border-border px-3 py-2.5 text-xs font-bold text-muted-foreground transition hover:border-destructive/40 hover:text-destructive", children: [
              /* @__PURE__ */ jsx(LogOut, { className: "h-3.5 w-3.5" }),
              "Logout"
            ] })
          ] })
        ] })
      ] }),
      /* @__PURE__ */ jsx("main", { className: "min-w-0 flex-1 px-4 py-6 sm:px-6 sm:py-8 lg:px-8 xl:px-10", children: /* @__PURE__ */ jsx("div", { className: "mx-auto w-full max-w-7xl", children: /* @__PURE__ */ jsx(Outlet, {}) }) })
    ] })
  ] }) });
}
export {
  AdminLayout as component
};
