"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { Avatar } from "@open-mercato/ui/primitives/avatar";
import { Badge } from "@open-mercato/ui/primitives/badge";
import { Settings2, SquarePen, Plus, Trash2, UserRoundPlus, ArrowRight } from "lucide-react";
import { useT } from "@open-mercato/shared/lib/i18n/context";
const ACTION_ICONS = {
  create: Plus,
  edit: SquarePen,
  delete: Trash2,
  assign: UserRoundPlus,
  system: Settings2
};
function formatFieldLabel(fieldName) {
  return fieldName.replace(/\./g, " ").replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[_-]+/g, " ").replace(/\s+/g, " ").trim().replace(/^\w/, (char) => char.toUpperCase());
}
function formatRelativeTime(value) {
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return "";
  const diffMs = date.getTime() - Date.now();
  const diffMinutes = Math.round(diffMs / 6e4);
  const formatter = new Intl.RelativeTimeFormat(void 0, { numeric: "auto" });
  if (Math.abs(diffMinutes) < 60) return formatter.format(diffMinutes, "minute");
  const diffHours = Math.round(diffMinutes / 60);
  if (Math.abs(diffHours) < 24) return formatter.format(diffHours, "hour");
  const diffDays = Math.round(diffHours / 24);
  return formatter.format(diffDays, "day");
}
function ChangelogEntryRow({ entry }) {
  const t = useT();
  const ActionIcon = ACTION_ICONS[entry.actionType];
  const createdAt = new Date(entry.createdAt);
  const timeLabel = Number.isNaN(createdAt.getTime()) ? "00:00" : createdAt.toLocaleTimeString(void 0, { hour: "2-digit", minute: "2-digit" });
  const relativeLabel = formatRelativeTime(entry.createdAt);
  const actorLabel = entry.actorUserName ?? t("customers.changelog.user.system", "System");
  const actionLabel = t(`customers.changelog.actions.${entry.actionType}`, formatFieldLabel(entry.actionType));
  const sourceLabel = entry.source === "api" ? t("customers.changelog.source.api", "API") : entry.source === "system" ? t("customers.changelog.source.system", "System") : t("customers.changelog.source.ui", "UI");
  return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[92px_190px_120px_1fr_80px] gap-3 px-5 py-3 text-sm", children: [
    /* @__PURE__ */ jsxs("div", { className: "space-y-0.5", children: [
      /* @__PURE__ */ jsx("div", { className: "font-medium text-foreground", children: timeLabel }),
      /* @__PURE__ */ jsx("div", { className: "text-xs text-muted-foreground", children: relativeLabel })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "flex items-start gap-2", children: [
      entry.actorUserId ? /* @__PURE__ */ jsx(Avatar, { label: actorLabel, size: "sm", variant: "monochrome" }) : /* @__PURE__ */ jsx(Avatar, { label: "", icon: /* @__PURE__ */ jsx(Settings2, {}), size: "sm", variant: "monochrome" }),
      /* @__PURE__ */ jsx("span", { className: "truncate pt-1 text-sm text-foreground", children: actorLabel })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "flex items-start gap-2 pt-0.5 text-sm text-foreground", children: [
      /* @__PURE__ */ jsx(ActionIcon, { className: "mt-0.5 size-4 text-muted-foreground" }),
      /* @__PURE__ */ jsx("span", { children: actionLabel })
    ] }),
    /* @__PURE__ */ jsx("div", { className: "min-w-0 space-y-1.5", children: entry.changes.length > 0 ? entry.changes.map((change) => /* @__PURE__ */ jsxs("div", { className: "flex flex-wrap items-center gap-2 text-sm", children: [
      /* @__PURE__ */ jsx("span", { className: "font-medium text-foreground", children: formatFieldLabel(change.fieldName) }),
      /* @__PURE__ */ jsx(
        Badge,
        {
          variant: "outline",
          className: "max-w-[12rem] rounded-full border-status-error-border bg-status-error-bg px-2 py-0.5 text-xs font-medium text-status-error-text",
          children: /* @__PURE__ */ jsx("span", { className: "truncate", children: change.oldValue || "\u2014" })
        }
      ),
      /* @__PURE__ */ jsx(ArrowRight, { className: "size-3.5 text-muted-foreground" }),
      /* @__PURE__ */ jsx(
        Badge,
        {
          variant: "outline",
          className: "max-w-[12rem] rounded-full border-status-success-border bg-status-success-bg px-2 py-0.5 text-xs font-medium text-status-success-text",
          children: /* @__PURE__ */ jsx("span", { className: "truncate", children: change.newValue || "\u2014" })
        }
      )
    ] }, `${entry.id}-${change.fieldName}`)) : /* @__PURE__ */ jsx("span", { className: "text-sm text-muted-foreground", children: entry.description ?? actionLabel }) }),
    /* @__PURE__ */ jsx("div", { className: "flex items-start justify-end", children: /* @__PURE__ */ jsx(Badge, { variant: "secondary", className: "px-1.5 py-0 text-xs", children: sourceLabel }) })
  ] });
}
var ChangelogEntryRow_default = ChangelogEntryRow;
export {
  ChangelogEntryRow,
  ChangelogEntryRow_default as default
};
//# sourceMappingURL=ChangelogEntryRow.js.map
