"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useMemo } from "react";
import { useTranslations } from "next-intl";
import { PermissionService } from "../../lib/services/permission.service.js";
import { Badge } from "../ui/badge.js";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow
} from "../ui/table.js";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
  TooltipProvider
} from "../ui/tooltip.js";
import { Check, X, AlertTriangle } from "lucide-react";
const ROLE_COLORS = {
  owner: "text-purple-600 bg-purple-50 dark:bg-purple-900/20 dark:text-purple-400",
  admin: "text-blue-600 bg-blue-50 dark:bg-blue-900/20 dark:text-blue-400",
  member: "text-green-600 bg-green-50 dark:bg-green-900/20 dark:text-green-400",
  viewer: "text-gray-600 bg-gray-50 dark:bg-gray-900/20 dark:text-gray-400"
};
function PermissionsMatrix({
  category,
  compact = false,
  onCellClick
}) {
  const t = useTranslations("permissions");
  const { permissions, matrix } = useMemo(() => {
    const data = PermissionService.getMatrix();
    if (category) {
      return {
        permissions: data.permissions.filter((p) => p.category === category),
        matrix: data.matrix
      };
    }
    return data;
  }, [category]);
  const roles = compact ? ["admin", "member", "viewer"] : ["owner", "admin", "member", "viewer"];
  const groupedPermissions = useMemo(() => {
    const groups = {};
    for (const perm of permissions) {
      if (!groups[perm.category]) {
        groups[perm.category] = [];
      }
      groups[perm.category].push(perm);
    }
    return groups;
  }, [permissions]);
  return /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsx("div", { className: "rounded-md border", "data-cy": "permissions-matrix", children: /* @__PURE__ */ jsxs(Table, { children: [
    /* @__PURE__ */ jsx(TableHeader, { children: /* @__PURE__ */ jsxs(TableRow, { children: [
      /* @__PURE__ */ jsx(TableHead, { className: "w-[300px]", children: t("permission") }),
      roles.map((role) => /* @__PURE__ */ jsx(TableHead, { className: "text-center w-[100px]", children: /* @__PURE__ */ jsx(Badge, { variant: "outline", className: ROLE_COLORS[role], children: t(`roles.${role}.title`) }) }, role))
    ] }) }),
    /* @__PURE__ */ jsx(TableBody, { children: Object.entries(groupedPermissions).map(([cat, perms]) => /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(TableRow, { className: "bg-muted/50", children: /* @__PURE__ */ jsx(TableCell, { colSpan: roles.length + 1, className: "font-semibold", children: cat }) }, `cat-${cat}`),
      perms.map((perm) => /* @__PURE__ */ jsxs(TableRow, { "data-cy": `permission-row-${perm.id}`, children: [
        /* @__PURE__ */ jsxs(TableCell, { children: [
          /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
            /* @__PURE__ */ jsx("span", { children: perm.label }),
            perm.dangerous && /* @__PURE__ */ jsxs(Tooltip, { children: [
              /* @__PURE__ */ jsx(TooltipTrigger, { children: /* @__PURE__ */ jsx(AlertTriangle, { className: "h-4 w-4 text-amber-500" }) }),
              /* @__PURE__ */ jsx(TooltipContent, { children: t("dangerousAction") })
            ] })
          ] }),
          perm.description && /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: perm.description })
        ] }),
        roles.map((role) => {
          var _a;
          return /* @__PURE__ */ jsx(
            TableCell,
            {
              className: "text-center cursor-pointer hover:bg-muted/50 transition-colors",
              onClick: () => onCellClick == null ? void 0 : onCellClick(perm.id, role),
              "data-cy": `permission-cell-${perm.id}-${role}`,
              children: ((_a = matrix[perm.id]) == null ? void 0 : _a[role]) ? /* @__PURE__ */ jsx(Check, { className: "h-5 w-5 text-green-600 dark:text-green-400 mx-auto" }) : /* @__PURE__ */ jsx(X, { className: "h-5 w-5 text-gray-300 dark:text-gray-600 mx-auto" })
            },
            role
          );
        })
      ] }, perm.id))
    ] })) })
  ] }) }) });
}
export {
  PermissionsMatrix
};
