"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { Alert, AlertDescription } from "../../primitives/alert.js";
function formatValue(value) {
  if (value == null) return "";
  if (typeof value === "string") return value;
  try {
    return JSON.stringify(value);
  } catch {
    return String(value);
  }
}
function DiffRow({ entry }) {
  const before = formatValue(entry.beforeDisplay ?? entry.before);
  const after = formatValue(entry.afterDisplay ?? entry.after);
  const field = entry.fieldLabel ?? entry.field;
  return /* @__PURE__ */ jsxs("tr", { className: "border-b border-border last:border-b-0", "data-ai-field-diff-row": true, children: [
    /* @__PURE__ */ jsx("td", { className: "py-1.5 pr-4 text-xs font-mono text-muted-foreground align-top", children: field }),
    /* @__PURE__ */ jsx(
      "td",
      {
        className: "py-1.5 pr-4 text-sm align-top text-status-warning-text",
        "data-ai-field-diff-before": true,
        children: /* @__PURE__ */ jsx("span", { className: "line-through break-all", children: before || "\u2014" })
      }
    ),
    /* @__PURE__ */ jsx(
      "td",
      {
        className: "py-1.5 text-sm align-top text-status-success-text",
        "data-ai-field-diff-after": true,
        children: /* @__PURE__ */ jsx("span", { className: "font-medium break-all", children: after || "\u2014" })
      }
    )
  ] });
}
function DiffTable({ rows, fieldHeader, beforeHeader, afterHeader }) {
  return /* @__PURE__ */ jsxs("table", { className: "w-full", "data-ai-field-diff-table": true, children: [
    /* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { className: "border-b border-border", children: [
      /* @__PURE__ */ jsx("th", { className: "py-1 pr-4 text-left text-xs font-semibold uppercase tracking-wider text-muted-foreground", children: fieldHeader }),
      /* @__PURE__ */ jsx("th", { className: "py-1 pr-4 text-left text-xs font-semibold uppercase tracking-wider text-muted-foreground", children: beforeHeader }),
      /* @__PURE__ */ jsx("th", { className: "py-1 text-left text-xs font-semibold uppercase tracking-wider text-muted-foreground", children: afterHeader })
    ] }) }),
    /* @__PURE__ */ jsx("tbody", { children: rows.map((entry, idx) => /* @__PURE__ */ jsx(DiffRow, { entry }, `${entry.field}-${idx}`)) })
  ] });
}
function FieldDiffCard({ fieldDiff, records }) {
  const t = useT();
  const fieldHeader = t(
    "ai_assistant.chat.mutation_cards.diff.fieldHeader",
    "Field"
  );
  const beforeHeader = t(
    "ai_assistant.chat.mutation_cards.diff.beforeHeader",
    "Before"
  );
  const afterHeader = t(
    "ai_assistant.chat.mutation_cards.diff.afterHeader",
    "After"
  );
  const batch = Array.isArray(records) && records.length > 0 ? records : null;
  const flat = Array.isArray(fieldDiff) ? fieldDiff : [];
  if (batch) {
    return /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-3", "data-ai-field-diff-mode": "batch", children: batch.map((record) => /* @__PURE__ */ jsxs(
      "section",
      {
        className: "rounded-md border border-border bg-background p-3",
        "data-ai-field-diff-record": record.recordId,
        children: [
          /* @__PURE__ */ jsxs("header", { className: "mb-2 flex items-baseline justify-between gap-2", children: [
            /* @__PURE__ */ jsx("h4", { className: "text-sm font-semibold", children: record.label }),
            /* @__PURE__ */ jsx("span", { className: "text-xs font-mono text-muted-foreground", children: record.entityType })
          ] }),
          record.fieldDiff.length > 0 ? /* @__PURE__ */ jsx(
            DiffTable,
            {
              rows: record.fieldDiff,
              fieldHeader,
              beforeHeader,
              afterHeader
            }
          ) : /* @__PURE__ */ jsx(Alert, { variant: "info", children: /* @__PURE__ */ jsx(AlertDescription, { children: t(
            "ai_assistant.chat.mutation_cards.diff.empty",
            "No field changes for this record."
          ) }) })
        ]
      },
      record.recordId
    )) });
  }
  if (flat.length === 0) {
    return /* @__PURE__ */ jsx(Alert, { variant: "info", "data-ai-field-diff-mode": "empty", children: /* @__PURE__ */ jsx(AlertDescription, { children: t(
      "ai_assistant.chat.mutation_cards.diff.empty",
      "No field changes for this record."
    ) }) });
  }
  return /* @__PURE__ */ jsx("div", { "data-ai-field-diff-mode": "flat", children: /* @__PURE__ */ jsx(
    DiffTable,
    {
      rows: flat,
      fieldHeader,
      beforeHeader,
      afterHeader
    }
  ) });
}
var FieldDiffCard_default = FieldDiffCard;
export {
  FieldDiffCard,
  FieldDiffCard_default as default
};
//# sourceMappingURL=FieldDiffCard.js.map
