"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Button } from "@open-mercato/ui/primitives/button";
import { Alert } from "@open-mercato/ui/primitives/alert";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import {
  applyAddMissingDependency,
  applyRemoveDependents,
  applyRestoreDependency,
  resolveAclDependencyDiagnostics
} from "@open-mercato/shared/security/aclDependencies";
function AclDependencyDiagnosticsPanel({
  granted,
  catalog,
  onGrantedChange,
  hideUnknownReferences
}) {
  const t = useT();
  const diagnostics = React.useMemo(
    () => resolveAclDependencyDiagnostics(granted, catalog),
    [granted, catalog]
  );
  const titleById = React.useMemo(() => {
    const map = /* @__PURE__ */ new Map();
    for (const entry of catalog) {
      if (entry?.title && !map.has(entry.id)) map.set(entry.id, entry.title);
    }
    return map;
  }, [catalog]);
  const featureLabel = React.useCallback((id) => titleById.get(id) ?? id, [titleById]);
  const hasMissing = diagnostics.missingDependencies.length > 0;
  const hasOrphaned = diagnostics.orphanedDependents.length > 0;
  const showUnknown = !hideUnknownReferences && diagnostics.unknownReferences.length > 0;
  if (!hasMissing && !hasOrphaned && !showUnknown) return null;
  const handleAdd = (dep) => onGrantedChange((prev) => applyAddMissingDependency(prev, dep));
  const handleRestore = (dep) => onGrantedChange((prev) => applyRestoreDependency(prev, dep));
  const handleDropDependents = (dependents) => onGrantedChange((prev) => applyRemoveDependents(prev, dependents));
  return /* @__PURE__ */ jsxs("div", { className: "space-y-3", "data-testid": "acl-dependency-diagnostics", children: [
    hasMissing && /* @__PURE__ */ jsx(Alert, { status: "warning", style: "lighter", children: /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
      /* @__PURE__ */ jsx("div", { className: "text-sm font-medium", children: t(
        "auth.acl.deps.missing.title",
        "Some granted permissions need other permissions to work:"
      ) }),
      /* @__PURE__ */ jsx("ul", { className: "space-y-1 text-sm", children: diagnostics.missingDependencies.map((row) => /* @__PURE__ */ jsxs(
        "li",
        {
          className: "flex flex-wrap items-center gap-x-2 gap-y-1",
          "data-testid": `missing-${row.feature}`,
          children: [
            /* @__PURE__ */ jsx("span", { children: t("auth.acl.deps.missing.item", '"{feature}" needs:', {
              feature: featureLabel(row.feature)
            }) }),
            row.missing.map((dep) => /* @__PURE__ */ jsxs("span", { className: "inline-flex items-center gap-1", children: [
              /* @__PURE__ */ jsx("span", { className: "font-mono text-xs text-muted-foreground", children: featureLabel(dep) }),
              /* @__PURE__ */ jsx(
                Button,
                {
                  type: "button",
                  size: "sm",
                  variant: "outline",
                  onClick: () => handleAdd(dep),
                  "data-testid": `add-missing-${row.feature}-${dep}`,
                  children: t("auth.acl.deps.missing.add", 'Add "{dep}"', {
                    dep: featureLabel(dep)
                  })
                }
              )
            ] }, dep))
          ]
        },
        row.feature
      )) })
    ] }) }),
    hasOrphaned && /* @__PURE__ */ jsx(Alert, { status: "warning", style: "lighter", children: /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
      /* @__PURE__ */ jsx("div", { className: "text-sm font-medium", children: t(
        "auth.acl.deps.orphaned.title",
        "Removing a permission that other granted permissions need:"
      ) }),
      /* @__PURE__ */ jsx("ul", { className: "space-y-1 text-sm", children: diagnostics.orphanedDependents.map((row) => /* @__PURE__ */ jsxs(
        "li",
        {
          className: "flex flex-wrap items-center gap-x-2 gap-y-1",
          "data-testid": `orphaned-${row.dependency}`,
          children: [
            /* @__PURE__ */ jsx("span", { children: t("auth.acl.deps.orphaned.item", '"{dependency}" is required by:', {
              dependency: featureLabel(row.dependency)
            }) }),
            /* @__PURE__ */ jsx("span", { className: "font-mono text-xs text-muted-foreground", children: row.dependents.map((dependent) => featureLabel(dependent)).join(", ") }),
            /* @__PURE__ */ jsx(
              Button,
              {
                type: "button",
                size: "sm",
                variant: "outline",
                onClick: () => handleRestore(row.dependency),
                "data-testid": `restore-${row.dependency}`,
                children: t("auth.acl.deps.orphaned.restore", 'Restore "{dependency}"', {
                  dependency: featureLabel(row.dependency)
                })
              }
            ),
            /* @__PURE__ */ jsx(
              Button,
              {
                type: "button",
                size: "sm",
                variant: "outline",
                onClick: () => handleDropDependents(row.dependents),
                "data-testid": `drop-dependents-${row.dependency}`,
                children: t("auth.acl.deps.orphaned.drop", "Drop dependents")
              }
            )
          ]
        },
        row.dependency
      )) })
    ] }) }),
    showUnknown && /* @__PURE__ */ jsx(Alert, { status: "warning", style: "lighter", children: /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
      /* @__PURE__ */ jsx("div", { className: "text-sm font-medium", children: t(
        "auth.acl.deps.unknown.title",
        "Some declared dependencies do not match any known permission:"
      ) }),
      /* @__PURE__ */ jsx("ul", { className: "space-y-1 text-sm font-mono text-xs text-muted-foreground", children: diagnostics.unknownReferences.map((row) => /* @__PURE__ */ jsxs("li", { "data-testid": `unknown-${row.feature}`, children: [
        row.feature,
        " \u2192 ",
        row.missing.join(", ")
      ] }, row.feature)) })
    ] }) })
  ] });
}
export {
  AclDependencyDiagnosticsPanel
};
//# sourceMappingURL=AclDependencyDiagnosticsPanel.js.map
