"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { ShoppingCart, ExternalLink, Loader2, X } from "lucide-react";
import { useRouter } from "next/navigation";
import { Button } from "@open-mercato/ui/primitives/button";
import { IconButton } from "@open-mercato/ui/primitives/icon-button";
import { cn } from "@open-mercato/shared/lib/utils";
import { formatRelativeTime } from "@open-mercato/shared/lib/time";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { formatMoney } from "../../components/documents/lineItemUtils.js";
import { useSalesDocumentTotals } from "./useSalesDocumentTotals.js";
function normalizeTotal(value) {
  if (!value) return null;
  let trimmed = value.trim();
  if (trimmed.startsWith("(") && trimmed.endsWith(")")) {
    trimmed = trimmed.slice(1, -1).trim();
  }
  return trimmed.length ? trimmed : null;
}
function SalesOrderCreatedRenderer({
  notification,
  onAction,
  onDismiss,
  actions = []
}) {
  const t = useT();
  const router = useRouter();
  const [executing, setExecuting] = React.useState(false);
  const isUnread = notification.status === "unread";
  const orderNumber = notification.bodyVariables?.orderNumber ?? notification.titleVariables?.orderNumber;
  const fallbackTotal = normalizeTotal(notification.bodyVariables?.totalAmount ?? null) ?? normalizeTotal(notification.bodyVariables?.total ?? null);
  const { totals } = useSalesDocumentTotals("order", notification.sourceEntityId);
  const currentTotal = totals && typeof totals.grandTotalGrossAmount === "number" ? formatMoney(totals.grandTotalGrossAmount, totals.currencyCode) : fallbackTotal;
  const viewAction = actions.find((action) => action.id === "view") ?? actions[0] ?? null;
  const handleView = async () => {
    if (!viewAction) {
      if (notification.linkHref) router.push(notification.linkHref);
      return;
    }
    setExecuting(true);
    try {
      await onAction(viewAction.id);
    } finally {
      setExecuting(false);
    }
  };
  const timeAgo = formatRelativeTime(notification.createdAt, { translate: t }) ?? "";
  return /* @__PURE__ */ jsxs(
    "div",
    {
      className: cn(
        "group relative flex gap-4 items-start rounded-xl p-3 transition-colors hover:bg-muted/40 cursor-pointer",
        isUnread && "bg-muted/20"
      ),
      onClick: handleView,
      onKeyDown: (e) => {
        if (e.key === "Enter" || e.key === " ") {
          e.preventDefault();
          handleView();
        }
      },
      role: "button",
      tabIndex: 0,
      children: [
        /* @__PURE__ */ jsxs("div", { className: "relative shrink-0 flex size-10 items-center justify-center rounded-full bg-status-info-bg", children: [
          /* @__PURE__ */ jsx(ShoppingCart, { className: "size-5 text-status-info-icon", "aria-hidden": "true" }),
          isUnread ? /* @__PURE__ */ jsx(
            "span",
            {
              className: "absolute -right-1 -top-1 size-3 rounded-full bg-accent-indigo ring-2 ring-background",
              "aria-hidden": "true"
            }
          ) : null
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 flex-1 flex-col gap-1", children: [
          /* @__PURE__ */ jsx("p", { className: "truncate text-sm font-medium leading-5 tracking-tight text-foreground", children: notification.title }),
          /* @__PURE__ */ jsxs("div", { className: "text-xs leading-4 text-muted-foreground", children: [
            timeAgo ? /* @__PURE__ */ jsxs(Fragment, { children: [
              /* @__PURE__ */ jsx("span", { className: "whitespace-nowrap", children: timeAgo }),
              /* @__PURE__ */ jsx("span", { "aria-hidden": "true", className: "mx-1 text-text-disabled", children: "\xB7" })
            ] }) : null,
            orderNumber ? /* @__PURE__ */ jsxs(Fragment, { children: [
              /* @__PURE__ */ jsxs("span", { className: "inline-flex items-center rounded bg-muted px-1.5 py-0.5 font-mono text-overline text-foreground", children: [
                "#",
                orderNumber
              ] }),
              /* @__PURE__ */ jsx("span", { "aria-hidden": "true", className: "mx-1 text-text-disabled", children: "\xB7" })
            ] }) : null,
            currentTotal ? /* @__PURE__ */ jsxs(Fragment, { children: [
              /* @__PURE__ */ jsx("span", { className: "whitespace-nowrap font-medium text-foreground", children: currentTotal }),
              /* @__PURE__ */ jsx("span", { "aria-hidden": "true", className: "mx-1 text-text-disabled", children: "\xB7" })
            ] }) : null,
            /* @__PURE__ */ jsx("span", { className: "whitespace-nowrap", children: t("sales.notifications.renderer.assignedToYou", "Assigned to you") })
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "mt-2 flex items-center gap-2", children: [
            /* @__PURE__ */ jsxs(
              Button,
              {
                type: "button",
                size: "sm",
                className: "h-8 rounded-md px-2.5 bg-accent-indigo text-accent-indigo-foreground hover:bg-accent-indigo/90",
                onClick: (e) => {
                  e.stopPropagation();
                  handleView();
                },
                disabled: executing || !viewAction && !notification.linkHref,
                children: [
                  /* @__PURE__ */ jsx(ExternalLink, { className: "size-3.5", "aria-hidden": "true" }),
                  t("sales.notifications.renderer.viewOrder", "View Order"),
                  executing ? /* @__PURE__ */ jsx(Loader2, { className: "ml-1 size-3 animate-spin" }) : null
                ]
              }
            ),
            /* @__PURE__ */ jsx(
              Button,
              {
                type: "button",
                variant: "outline",
                size: "sm",
                className: "h-8 rounded-md px-2.5",
                onClick: (e) => {
                  e.stopPropagation();
                  onDismiss();
                },
                children: t("notifications.actions.dismiss", "Dismiss")
              }
            )
          ] })
        ] }),
        /* @__PURE__ */ jsx(
          IconButton,
          {
            type: "button",
            variant: "ghost",
            size: "xs",
            className: "absolute right-2 top-2 opacity-0 transition-opacity group-hover:opacity-100 focus-visible:opacity-100",
            onClick: (e) => {
              e.stopPropagation();
              onDismiss();
            },
            "aria-label": t("notifications.actions.dismiss", "Dismiss"),
            children: /* @__PURE__ */ jsx(X, { className: "size-3.5" })
          }
        )
      ]
    }
  );
}
var SalesOrderCreatedRenderer_default = SalesOrderCreatedRenderer;
export {
  SalesOrderCreatedRenderer,
  SalesOrderCreatedRenderer_default as default
};
//# sourceMappingURL=SalesOrderCreatedRenderer.js.map
