"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useAuth } from "../../../hooks/useAuth.js";
import {
  Sheet,
  SheetContent,
  SheetHeader,
  SheetTitle,
  SheetDescription
} from "../../ui/sheet.js";
import { Button } from "../../ui/button.js";
import { Separator } from "../../ui/separator.js";
import Link from "next/link";
import { Shield, LogOut } from "lucide-react";
import { sel } from "../../../lib/test/index.js";
import { TeamSwitcherCompact } from "../../teams/TeamSwitcherCompact.js";
import { useTranslations } from "next-intl";
import { useIsSuperAdmin } from "../../app/guards/SuperAdminGuard.js";
import { useState } from "react";
import { MOBILE_NAV_CONFIG } from "../../../lib/config/config-sync.js";
import { getIcon } from "../../../lib/config/icon-map.js";
import { usePermission } from "../../../lib/permissions/hooks.js";
function MoreSheetItemWithPermission({
  item,
  onLinkClick
}) {
  const permissionToCheck = item.requiresPermission || "teams.read";
  const hasPermission = usePermission(permissionToCheck);
  if (item.requiresPermission && !hasPermission) {
    return null;
  }
  const Icon = item.icon;
  return /* @__PURE__ */ jsxs(
    Link,
    {
      href: item.href,
      onClick: onLinkClick,
      className: "flex items-center gap-3 px-4 py-3 rounded-lg hover:bg-accent transition-colors",
      target: item.external ? "_blank" : void 0,
      rel: item.external ? "noopener noreferrer" : void 0,
      "data-cy": sel("dashboard.mobile.moreSheet.item", { id: item.id }),
      children: [
        /* @__PURE__ */ jsx(Icon, { className: "h-5 w-5 text-muted-foreground", "aria-hidden": "true" }),
        /* @__PURE__ */ jsx("span", { className: "text-sm font-medium", children: item.label })
      ]
    }
  );
}
function MobileMoreSheet({ isOpen, onOpenChange }) {
  const { user, signOut } = useAuth();
  const tCommon = useTranslations("common");
  const t = useTranslations();
  const isSuperAdmin = useIsSuperAdmin();
  const [isSigningOut, setIsSigningOut] = useState(false);
  const handleSignOut = async () => {
    try {
      setIsSigningOut(true);
      await signOut();
      onOpenChange(false);
    } catch (error) {
      console.error("Error signing out:", error);
    } finally {
      setIsSigningOut(false);
    }
  };
  const handleLinkClick = () => {
    onOpenChange(false);
  };
  const menuItems = MOBILE_NAV_CONFIG.moreSheetItems.filter((item) => item.enabled).map((configItem) => {
    const IconComponent = getIcon(configItem.icon);
    return {
      id: configItem.id,
      label: t(configItem.labelKey),
      href: configItem.href,
      icon: IconComponent,
      external: configItem.external || false,
      requiresPermission: configItem.requiresPermission
    };
  });
  if (!user) return null;
  return /* @__PURE__ */ jsx(Sheet, { open: isOpen, onOpenChange, children: /* @__PURE__ */ jsxs(
    SheetContent,
    {
      side: "bottom",
      className: "rounded-t-[20px]",
      "data-cy": sel("dashboard.mobile.moreSheet.container"),
      children: [
        /* @__PURE__ */ jsxs(SheetHeader, { children: [
          /* @__PURE__ */ jsx(SheetTitle, { children: tCommon("mobileNav.moreOptions") }),
          /* @__PURE__ */ jsx(SheetDescription, { children: tCommon("mobileNav.moreOptionsDescription") })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "mt-6 space-y-2", children: [
          menuItems.map((item) => /* @__PURE__ */ jsx(
            MoreSheetItemWithPermission,
            {
              item,
              onLinkClick: handleLinkClick
            },
            item.id
          )),
          isSuperAdmin && /* @__PURE__ */ jsxs(Fragment, { children: [
            /* @__PURE__ */ jsx(Separator, { className: "my-2" }),
            /* @__PURE__ */ jsxs(
              Link,
              {
                href: "/superadmin",
                onClick: handleLinkClick,
                className: "flex items-center gap-3 px-4 py-3 rounded-lg hover:bg-red-50 text-red-600 transition-colors",
                "data-cy": sel("dashboard.mobile.moreSheet.superadminLink"),
                children: [
                  /* @__PURE__ */ jsx(Shield, { className: "h-5 w-5", "aria-hidden": "true" }),
                  /* @__PURE__ */ jsx("span", { className: "text-sm font-medium", children: "Super Admin Panel" })
                ]
              }
            )
          ] }),
          /* @__PURE__ */ jsx(Separator, { className: "my-2" }),
          /* @__PURE__ */ jsx("div", { "data-cy": sel("dashboard.mobile.moreSheet.teamSwitcher"), children: /* @__PURE__ */ jsx(TeamSwitcherCompact, { className: "border-0 p-0" }) }),
          /* @__PURE__ */ jsx(Separator, { className: "my-4" }),
          /* @__PURE__ */ jsxs(
            Button,
            {
              variant: "ghost",
              onClick: handleSignOut,
              disabled: isSigningOut,
              className: "w-full justify-start gap-3 px-4 py-3 h-auto text-red-600 hover:text-red-700 hover:bg-red-50",
              "data-cy": sel("dashboard.mobile.moreSheet.signoutButton"),
              children: [
                /* @__PURE__ */ jsx(LogOut, { className: "h-5 w-5", "aria-hidden": "true" }),
                /* @__PURE__ */ jsx("span", { className: "text-sm font-medium", children: isSigningOut ? tCommon("buttons.signingOut") : tCommon("buttons.signOut") })
              ]
            }
          )
        ] })
      ]
    }
  ) });
}
export {
  MobileMoreSheet
};
