"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { ShieldCheck, ShieldAlert, ShieldX } from "lucide-react";
const CONSENT_TYPE_LABELS = {
  marketing_email: "Marketing Email"
};
function formatDate(iso) {
  if (!iso) return "-";
  try {
    return new Date(iso).toLocaleString();
  } catch {
    return iso;
  }
}
function UserConsentsPanel({ userId }) {
  const t = useT();
  const tRef = React.useRef(t);
  tRef.current = t;
  const [consents, setConsents] = React.useState([]);
  const [loading, setLoading] = React.useState(true);
  const [error, setError] = React.useState(null);
  React.useEffect(() => {
    let cancelled = false;
    async function load() {
      setLoading(true);
      setError(null);
      try {
        const { ok, result } = await apiCall(
          `/api/auth/users/consents?userId=${encodeURIComponent(userId)}`
        );
        if (!cancelled) {
          if (!ok) {
            setError(tRef.current("auth.users.consents.loadError", "Failed to load consents"));
          } else {
            setConsents(result?.items ?? []);
          }
        }
      } catch {
        if (!cancelled) setError(tRef.current("auth.users.consents.loadError", "Failed to load consents"));
      }
      if (!cancelled) setLoading(false);
    }
    load();
    return () => {
      cancelled = true;
    };
  }, [userId]);
  if (loading) {
    return /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: t("auth.users.consents.loading", "Loading consents...") });
  }
  if (error) {
    return /* @__PURE__ */ jsx("p", { className: "text-sm text-red-600", children: error });
  }
  if (consents.length === 0) {
    return /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: t("auth.users.consents.empty", "No consent records found.") });
  }
  return /* @__PURE__ */ jsx("div", { className: "space-y-3", children: consents.map((consent) => /* @__PURE__ */ jsxs(
    "div",
    {
      className: "rounded-lg border bg-background p-4 text-sm",
      children: [
        /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between mb-2", children: [
          /* @__PURE__ */ jsx("span", { className: "font-medium", children: CONSENT_TYPE_LABELS[consent.consentType] ?? consent.consentType }),
          /* @__PURE__ */ jsx("span", { className: "flex items-center gap-1.5", children: consent.isGranted ? /* @__PURE__ */ jsxs(Fragment, { children: [
            /* @__PURE__ */ jsx(ShieldCheck, { className: "size-4 text-emerald-600" }),
            /* @__PURE__ */ jsx("span", { className: "text-emerald-700 font-medium", children: t("auth.users.consents.granted", "Granted") })
          ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
            /* @__PURE__ */ jsx(ShieldX, { className: "size-4 text-red-500" }),
            /* @__PURE__ */ jsx("span", { className: "text-red-600 font-medium", children: t("auth.users.consents.withdrawn", "Withdrawn") })
          ] }) })
        ] }),
        /* @__PURE__ */ jsxs("dl", { className: "grid grid-cols-[auto_1fr] gap-x-4 gap-y-1 text-xs text-muted-foreground", children: [
          /* @__PURE__ */ jsx("dt", { children: t("auth.users.consents.field.grantedAt", "Granted at") }),
          /* @__PURE__ */ jsx("dd", { children: formatDate(consent.grantedAt) }),
          consent.withdrawnAt && /* @__PURE__ */ jsxs(Fragment, { children: [
            /* @__PURE__ */ jsx("dt", { children: t("auth.users.consents.field.withdrawnAt", "Withdrawn at") }),
            /* @__PURE__ */ jsx("dd", { children: formatDate(consent.withdrawnAt) })
          ] }),
          /* @__PURE__ */ jsx("dt", { children: t("auth.users.consents.field.source", "Source") }),
          /* @__PURE__ */ jsx("dd", { children: consent.source ?? "-" }),
          /* @__PURE__ */ jsx("dt", { children: t("auth.users.consents.field.ipAddress", "IP address") }),
          /* @__PURE__ */ jsx("dd", { className: "font-mono", children: consent.ipAddress ?? "-" }),
          /* @__PURE__ */ jsx("dt", { children: t("auth.users.consents.field.integrity", "Integrity") }),
          /* @__PURE__ */ jsx("dd", { className: "flex items-center gap-1", children: consent.integrityValid ? /* @__PURE__ */ jsxs(Fragment, { children: [
            /* @__PURE__ */ jsx(ShieldCheck, { className: "size-3 text-emerald-600" }),
            /* @__PURE__ */ jsx("span", { className: "text-emerald-700", children: t("auth.users.consents.integrityValid", "Valid") })
          ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
            /* @__PURE__ */ jsx(ShieldAlert, { className: "size-3 text-amber-500" }),
            /* @__PURE__ */ jsx("span", { className: "text-amber-600", children: t("auth.users.consents.integrityInvalid", "Tampered or missing") })
          ] }) }),
          /* @__PURE__ */ jsx("dt", { children: t("auth.users.consents.field.createdAt", "Created") }),
          /* @__PURE__ */ jsx("dd", { children: formatDate(consent.createdAt) })
        ] })
      ]
    },
    consent.id
  )) });
}
export {
  UserConsentsPanel
};
//# sourceMappingURL=UserConsentsPanel.js.map
