import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger
} from "@open-mercato/ui/primitives/accordion";
import { extractChangeRows, isRecord as isRecord2 } from "./changeRows.js";
function humanizeField(field) {
  return field.replace(/[_-]+/g, " ").replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/\b\w/g, (s) => s.toUpperCase());
}
function normalizeChangeField(field) {
  const parts = field.split(".");
  const base = parts.length === 2 ? parts[1] : field;
  if (base.startsWith("cf_")) return base.slice(3);
  if (base.startsWith("cf:")) return base.slice(3);
  return base;
}
function renderValue(value, fallback) {
  if (value === void 0 || value === null || value === "") {
    return /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: fallback });
  }
  if (typeof value === "boolean") return /* @__PURE__ */ jsx("span", { children: value ? "true" : "false" });
  if (typeof value === "number" || typeof value === "bigint") return /* @__PURE__ */ jsx("span", { children: String(value) });
  if (value instanceof Date) return /* @__PURE__ */ jsx("span", { children: value.toISOString() });
  if (typeof value === "string") return /* @__PURE__ */ jsx("span", { className: "break-words", children: value });
  return /* @__PURE__ */ jsx("pre", { className: "max-h-40 overflow-y-auto whitespace-pre-wrap break-words rounded-md bg-muted/50 px-2 py-1 text-xs leading-5 text-muted-foreground", children: safeStringify(value) });
}
function safeStringify(value) {
  if (typeof value === "string") return value;
  try {
    return JSON.stringify(value, null, 2);
  } catch {
    return String(value);
  }
}
function formatResource(item, fallback) {
  if (!item.resourceKind && !item.resourceId) return fallback;
  return [item.resourceKind, item.resourceId].filter(Boolean).join(" \xB7 ");
}
function formatDate(value) {
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return value;
  return new Intl.DateTimeFormat(void 0, {
    dateStyle: "medium",
    timeStyle: "short"
  }).format(date);
}
function ChangedFieldsTable({ changeRows, noneLabel, t, beforeLabel, afterLabel }) {
  return /* @__PURE__ */ jsxs("section", { children: [
    /* @__PURE__ */ jsx("h3", { className: "text-sm font-semibold", children: t("audit_logs.actions.details.changed_fields") }),
    changeRows.length ? /* @__PURE__ */ jsx("div", { className: "mt-2 overflow-x-auto rounded-lg border", children: /* @__PURE__ */ jsxs("table", { className: "min-w-full divide-y text-sm", children: [
      /* @__PURE__ */ jsx("thead", { className: "bg-muted/50", children: /* @__PURE__ */ jsxs("tr", { children: [
        /* @__PURE__ */ jsx("th", { scope: "col", className: "px-4 py-2 text-left font-medium text-muted-foreground", children: t("audit_logs.actions.details.field") }),
        /* @__PURE__ */ jsx("th", { scope: "col", className: "px-4 py-2 text-left font-medium text-muted-foreground", children: beforeLabel ?? t("audit_logs.actions.details.before") }),
        /* @__PURE__ */ jsx("th", { scope: "col", className: "px-4 py-2 text-left font-medium text-muted-foreground", children: afterLabel ?? t("audit_logs.actions.details.after") })
      ] }) }),
      /* @__PURE__ */ jsx("tbody", { className: "divide-y", children: changeRows.map((row) => /* @__PURE__ */ jsxs("tr", { className: "align-top", children: [
        /* @__PURE__ */ jsx("td", { className: "px-4 py-2 align-top font-medium", children: humanizeField(normalizeChangeField(row.field)) }),
        /* @__PURE__ */ jsx("td", { className: "px-4 py-2", children: renderValue(row.from, noneLabel) }),
        /* @__PURE__ */ jsx("td", { className: "px-4 py-2", children: renderValue(row.to, noneLabel) })
      ] }, row.field)) })
    ] }) }) : /* @__PURE__ */ jsx("p", { className: "mt-2 text-sm text-muted-foreground", children: t("audit_logs.actions.details.no_changes") })
  ] });
}
const DEFAULT_TRUNCATE_AT = 5e3;
const COLLAPSIBLE_JSON_ITEM_VALUE = "open";
function CollapsibleJsonSection({ label, value, truncateAt = DEFAULT_TRUNCATE_AT }) {
  const [openValue, setOpenValue] = React.useState("");
  const [showFull, setShowFull] = React.useState(false);
  const isOpen = openValue === COLLAPSIBLE_JSON_ITEM_VALUE;
  const stringified = React.useMemo(() => isOpen ? safeStringify(value) : "", [isOpen, value]);
  const isTruncated = stringified.length > truncateAt;
  const displayText = !showFull && isTruncated ? stringified.slice(0, truncateAt) : stringified;
  return /* @__PURE__ */ jsx(Accordion, { type: "single", collapsible: true, value: openValue, onValueChange: setOpenValue, children: /* @__PURE__ */ jsxs(AccordionItem, { value: COLLAPSIBLE_JSON_ITEM_VALUE, children: [
    /* @__PURE__ */ jsx(AccordionTrigger, { children: /* @__PURE__ */ jsx("span", { className: "font-semibold text-foreground group-data-[state=open]/accordion-trigger:text-primary", children: label }) }),
    /* @__PURE__ */ jsxs(AccordionContent, { children: [
      /* @__PURE__ */ jsxs("pre", { className: "max-h-96 overflow-auto whitespace-pre-wrap break-words text-xs leading-5 text-muted-foreground", children: [
        displayText,
        !showFull && isTruncated ? "\n\u2026" : null
      ] }),
      isTruncated ? /* @__PURE__ */ jsx(
        "button",
        {
          type: "button",
          className: "mt-1 text-xs text-primary hover:underline",
          onClick: () => setShowFull((prev) => !prev),
          children: showFull ? "Show less" : `Show all (${Math.ceil(stringified.length / 1024)} KB)`
        }
      ) : null
    ] })
  ] }) });
}
export {
  ChangedFieldsTable,
  CollapsibleJsonSection,
  extractChangeRows,
  formatDate,
  formatResource,
  humanizeField,
  isRecord2 as isRecord,
  normalizeChangeField,
  renderValue,
  safeStringify
};
//# sourceMappingURL=display-helpers.js.map
