"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useSession } from "../../../lib/auth-client.js";
import { useRouter } from "next/navigation";
import { useEffect } from "react";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../../ui/card.js";
import { AlertTriangle, ArrowLeft } from "lucide-react";
import { Button } from "../../ui/button.js";
import { useTranslations } from "next-intl";
function SuperAdminGuard({ children, fallback }) {
  var _a, _b;
  const { data: session, isPending } = useSession();
  const router = useRouter();
  const t = useTranslations();
  useEffect(() => {
    var _a2, _b2;
    if (!isPending && session && ((_a2 = session.user) == null ? void 0 : _a2.role) !== "superadmin" && ((_b2 = session.user) == null ? void 0 : _b2.role) !== "developer") {
      router.push("/dashboard?error=access_denied");
    }
  }, [session, isPending, router]);
  if (isPending) {
    return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center min-h-screen", children: /* @__PURE__ */ jsxs("div", { className: "animate-pulse", children: [
      /* @__PURE__ */ jsx("div", { className: "h-8 bg-muted rounded w-48 mb-4" }),
      /* @__PURE__ */ jsx("div", { className: "h-4 bg-muted rounded w-32" })
    ] }) });
  }
  if (!session) {
    return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center min-h-screen", children: /* @__PURE__ */ jsxs(Card, { className: "w-full max-w-md", children: [
      /* @__PURE__ */ jsxs(CardHeader, { children: [
        /* @__PURE__ */ jsxs(CardTitle, { className: "flex items-center gap-2", children: [
          /* @__PURE__ */ jsx(AlertTriangle, { className: "h-5 w-5 text-destructive" }),
          t("common.status.error")
        ] }),
        /* @__PURE__ */ jsx(CardDescription, { children: "Session not found. Please sign in again." })
      ] }),
      /* @__PURE__ */ jsx(CardContent, { children: /* @__PURE__ */ jsx(Button, { onClick: () => router.push("/login"), className: "w-full", children: "Sign In" }) })
    ] }) });
  }
  if (((_a = session.user) == null ? void 0 : _a.role) !== "superadmin" && ((_b = session.user) == null ? void 0 : _b.role) !== "developer") {
    if (fallback) {
      return /* @__PURE__ */ jsx(Fragment, { children: fallback });
    }
    return /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center min-h-screen", children: /* @__PURE__ */ jsxs(Card, { className: "w-full max-w-md", children: [
      /* @__PURE__ */ jsxs(CardHeader, { children: [
        /* @__PURE__ */ jsxs(CardTitle, { className: "flex items-center gap-2", children: [
          /* @__PURE__ */ jsx(AlertTriangle, { className: "h-5 w-5 text-destructive" }),
          "Access Denied"
        ] }),
        /* @__PURE__ */ jsx(CardDescription, { children: "You need superadmin or developer privileges to access this area." })
      ] }),
      /* @__PURE__ */ jsxs(CardContent, { className: "space-y-4", children: [
        /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: "This section is restricted to system administrators only. If you believe this is an error, please contact support." }),
        /* @__PURE__ */ jsxs("div", { className: "flex gap-2", children: [
          /* @__PURE__ */ jsxs(
            Button,
            {
              variant: "outline",
              onClick: () => router.back(),
              className: "flex items-center gap-2",
              children: [
                /* @__PURE__ */ jsx(ArrowLeft, { className: "h-4 w-4" }),
                "Go Back"
              ]
            }
          ),
          /* @__PURE__ */ jsx(Button, { onClick: () => router.push("/dashboard"), children: "Dashboard" })
        ] })
      ] })
    ] }) });
  }
  return /* @__PURE__ */ jsx(Fragment, { children });
}
function useIsSuperAdmin() {
  var _a;
  const { data: session } = useSession();
  return ((_a = session == null ? void 0 : session.user) == null ? void 0 : _a.role) === "superadmin";
}
function useCanAccessAdmin() {
  var _a, _b;
  const { data: session } = useSession();
  return ((_a = session == null ? void 0 : session.user) == null ? void 0 : _a.role) === "superadmin" || ((_b = session == null ? void 0 : session.user) == null ? void 0 : _b.role) === "developer";
}
export {
  SuperAdminGuard,
  useCanAccessAdmin,
  useIsSuperAdmin
};
