"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { cn } from "@open-mercato/shared/lib/utils";
import { CheckCircle2, FileText, Paperclip, Zap } from "lucide-react";
function formatDateTime(value) {
  if (!value) return "\u2014";
  if (Number.isNaN(value.getTime())) return "\u2014";
  return value.toLocaleString();
}
function formatSentTime(value) {
  if (!value) return "\u2014";
  if (Number.isNaN(value.getTime())) return "\u2014";
  const now = /* @__PURE__ */ new Date();
  const isSameDay = now.toDateString() === value.toDateString();
  if (isSameDay) {
    return value.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
  }
  const isSameYear = now.getFullYear() === value.getFullYear();
  if (isSameYear) {
    return value.toLocaleDateString([], { month: "short", day: "numeric" });
  }
  return value.toLocaleDateString([], { year: "numeric", month: "short", day: "numeric" });
}
function truncateWords(value, maxWords) {
  const normalized = value.trim().replace(/\s+/g, " ");
  if (!normalized) return "";
  const words = normalized.split(" ");
  if (words.length <= maxWords) return normalized;
  return `${words.slice(0, maxWords).join(" ")}...`;
}
function DefaultMessageListItem({ message, onClick }) {
  const t = useT();
  const senderLabel = message.senderName?.trim() || t("messages.list.noRecipient", "(No recipient)");
  const subject = message.subject.trim() || t("messages.list.noSubject", "(No subject)");
  const absoluteSentAt = formatDateTime(message.sentAt);
  const sentAtLabel = formatSentTime(message.sentAt);
  const bodyPreview = truncateWords(message.body || "", 16);
  return /* @__PURE__ */ jsx(
    "div",
    {
      className: cn(
        "group min-w-0 w-full cursor-pointer rounded-md px-2 py-2 transition-colors hover:bg-muted/50",
        "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
      ),
      role: "button",
      tabIndex: 0,
      "aria-label": t("messages.openMessageDetailedA11y", "Open message {subject} from {sender}, sent {sentAt}", {
        subject,
        sender: senderLabel,
        sentAt: absoluteSentAt
      }),
      onClick,
      onKeyDown: (event) => {
        if (event.key === "Enter" || event.key === " ") {
          event.preventDefault();
          onClick();
        }
      },
      children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-3", children: [
        /* @__PURE__ */ jsx("span", { className: cn("w-40 flex-shrink-0 truncate text-sm md:w-56", message.unread ? "font-semibold" : "font-normal"), children: senderLabel }),
        /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 flex-1 items-center gap-1.5 text-sm", children: [
          /* @__PURE__ */ jsx("span", { className: cn("truncate text-foreground", message.unread ? "font-semibold" : "font-normal"), children: subject }),
          bodyPreview ? /* @__PURE__ */ jsxs(Fragment, { children: [
            /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: "-" }),
            /* @__PURE__ */ jsx("span", { className: "truncate text-muted-foreground", children: bodyPreview })
          ] }) : null
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "flex flex-shrink-0 items-center gap-1.5 text-muted-foreground", children: [
          message.hasObjects ? /* @__PURE__ */ jsx(FileText, { className: "h-3.5 w-3.5", "aria-hidden": true }) : null,
          message.hasAttachments ? /* @__PURE__ */ jsx(Paperclip, { className: "h-3.5 w-3.5", "aria-hidden": true }) : null,
          message.hasActions && !message.actionTaken ? /* @__PURE__ */ jsx(Zap, { className: "h-3.5 w-3.5 text-amber-600", "aria-hidden": true }) : null,
          message.actionTaken ? /* @__PURE__ */ jsx(CheckCircle2, { className: "h-3.5 w-3.5 text-green-600", "aria-hidden": true }) : null
        ] }),
        /* @__PURE__ */ jsx(
          "span",
          {
            className: cn("flex-shrink-0 text-xs", message.unread ? "font-semibold text-foreground" : "font-normal text-muted-foreground"),
            title: absoluteSentAt,
            children: sentAtLabel
          }
        )
      ] })
    }
  );
}
var DefaultMessageListItem_default = DefaultMessageListItem;
export {
  DefaultMessageListItem,
  DefaultMessageListItem_default as default
};
//# sourceMappingURL=DefaultMessageListItem.js.map
