"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { cn } from "@open-mercato/shared/lib/utils";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { PriceWithCurrency } from "../PriceWithCurrency.js";
function DocumentTotals({ title, currency, items, className }) {
  const t = useT();
  const emphasizedRows = items.filter((item) => item.emphasize);
  const heading = title ?? t("sales.documents.detail.totals.title");
  const [expanded, setExpanded] = React.useState(false);
  const paidItem = React.useMemo(() => {
    const entry = items.find((item) => item.key === "paidTotalAmount");
    if (!entry) return null;
    const numeric = typeof entry.amount === "number" ? entry.amount : typeof entry.amount === "string" ? Number(entry.amount) : null;
    if (numeric === null || Number.isNaN(numeric) || numeric <= 0) return null;
    return entry;
  }, [items]);
  const collapsedItems = React.useMemo(() => {
    const base = emphasizedRows.length ? [...emphasizedRows] : items.slice(0, 3);
    const augmented = paidItem && !base.some((item) => item.key === paidItem.key) ? [...base, paidItem] : base;
    const seen = /* @__PURE__ */ new Set();
    return augmented.filter((item) => {
      if (seen.has(item.key)) return false;
      seen.add(item.key);
      return true;
    });
  }, [emphasizedRows, items, paidItem]);
  const visibleItems = expanded ? items : collapsedItems;
  const uniqueItemCount = React.useMemo(() => new Set(items.map((item) => item.key)).size, [items]);
  const hiddenCount = Math.max(0, uniqueItemCount - visibleItems.length);
  if (!items.length) return null;
  return /* @__PURE__ */ jsx("div", { className: cn("space-y-3", className), children: /* @__PURE__ */ jsxs("div", { className: "overflow-hidden rounded-xl border bg-card shadow-sm", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between border-b bg-muted/50 px-4 py-3", children: [
      /* @__PURE__ */ jsx("span", { className: "text-xs font-semibold uppercase tracking-wide text-muted-foreground", children: heading }),
      currency ? /* @__PURE__ */ jsx("span", { className: "rounded-full border border-border/70 bg-background/80 px-3 py-1 text-xs font-semibold tracking-wide text-foreground", children: currency }) : null
    ] }),
    /* @__PURE__ */ jsxs("table", { className: "w-full text-sm", children: [
      /* @__PURE__ */ jsx("tbody", { className: "divide-y divide-border/80", children: visibleItems.filter((item) => !item.emphasize).map((item) => /* @__PURE__ */ jsxs("tr", { className: "bg-background/80 transition-colors hover:bg-muted/30", children: [
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 font-medium text-foreground/90", children: item.label }),
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 text-right", children: /* @__PURE__ */ jsx(PriceWithCurrency, { amount: item.amount, currency, className: "font-mono text-base" }) })
      ] }, item.key)) }),
      visibleItems.some((item) => item.emphasize) ? /* @__PURE__ */ jsx("tfoot", { className: "border-t-2 border-primary/40 bg-primary/5", children: visibleItems.filter((item) => item.emphasize).map((item) => /* @__PURE__ */ jsxs("tr", { children: [
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 font-semibold uppercase tracking-wide text-foreground", children: item.label }),
        /* @__PURE__ */ jsx("td", { className: "px-4 py-3 text-right", children: /* @__PURE__ */ jsx(
          PriceWithCurrency,
          {
            amount: item.amount,
            currency,
            className: "font-mono text-lg font-semibold text-foreground"
          }
        ) })
      ] }, item.key)) }) : null
    ] }),
    hiddenCount > 0 ? /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between border-t bg-muted/30 px-4 py-3", children: [
      /* @__PURE__ */ jsx("span", { className: "text-xs text-muted-foreground", children: expanded ? t("sales.documents.detail.totals.showingAll") : t("sales.documents.detail.totals.showingKey", { count: hiddenCount }) }),
      /* @__PURE__ */ jsx(
        "button",
        {
          type: "button",
          className: "text-xs font-semibold text-primary transition-colors hover:text-primary/80",
          onClick: () => setExpanded((prev) => !prev),
          children: expanded ? t("sales.documents.detail.totals.hideDetails") : t("sales.documents.detail.totals.showDetails")
        }
      )
    ] }) : null
  ] }) });
}
export {
  DocumentTotals
};
//# sourceMappingURL=DocumentTotals.js.map
