"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useQuery } from "@tanstack/react-query";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
import { MarkdownContent } from "@open-mercato/ui/backend/markdown/MarkdownContent";
function toErrorMessage(payload) {
  if (!payload) return null;
  if (typeof payload === "string") return payload;
  if (Array.isArray(payload)) {
    for (const item of payload) {
      const nested = toErrorMessage(item);
      if (nested) return nested;
    }
    return null;
  }
  if (typeof payload === "object") {
    const record = payload;
    return toErrorMessage(record.error) ?? toErrorMessage(record.message) ?? toErrorMessage(record.detail) ?? null;
  }
  return null;
}
function formatDateTime(value) {
  if (!value) return "\u2014";
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return "\u2014";
  return date.toLocaleString();
}
function MessageConfirmationContent({ message }) {
  const t = useT();
  const confirmationQuery = useQuery({
    queryKey: ["messages", "confirmation", message.id],
    queryFn: async () => {
      const call = await apiCall(`/api/messages/${encodeURIComponent(message.id)}/confirmation`);
      if (!call.ok || !call.result) {
        throw new Error(
          toErrorMessage(call.result) ?? t("messages.errors.loadDetailFailed", "Failed to load message details.")
        );
      }
      return call.result;
    },
    enabled: Boolean(message.id)
  });
  const confirmation = confirmationQuery.data;
  return /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
    /* @__PURE__ */ jsx("div", { className: "max-h-[60vh] overflow-y-auto pr-1", children: /* @__PURE__ */ jsx(
      MarkdownContent,
      {
        body: message.body,
        format: message.bodyFormat,
        className: "text-sm whitespace-pre-wrap [&>*]:mb-2 [&>*:last-child]:mb-0 [&_ul]:ml-4 [&_ul]:list-disc [&_ol]:ml-4 [&_ol]:list-decimal [&_code]:rounded [&_code]:bg-muted [&_code]:px-1 [&_code]:py-0.5 [&_pre]:rounded-md [&_pre]:bg-muted [&_pre]:p-3 [&_pre]:text-xs"
      }
    ) }),
    /* @__PURE__ */ jsxs("div", { className: "rounded border p-3 text-sm", children: [
      /* @__PURE__ */ jsx("p", { className: "font-medium", children: confirmation?.confirmed ? t("messages.confirmation.confirmed", "Confirmed") : t("messages.confirmation.notConfirmed", "Not confirmed") }),
      /* @__PURE__ */ jsxs("p", { className: "text-xs text-muted-foreground", children: [
        t("messages.confirmation.confirmedAt", "Confirmed at"),
        ": ",
        formatDateTime(confirmation?.confirmedAt ?? null)
      ] })
    ] })
  ] });
}
var MessageConfirmationContent_default = MessageConfirmationContent;
export {
  MessageConfirmationContent,
  MessageConfirmationContent_default as default
};
//# sourceMappingURL=MessageConfirmationContent.js.map
