"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import {
  Popover,
  PopoverContent,
  PopoverTrigger
} from "@open-mercato/ui/primitives/popover";
import { Button } from "@open-mercato/ui/primitives/button";
import { IconButton } from "@open-mercato/ui/primitives/icon-button";
import { X } from "lucide-react";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { translateWithFallback } from "@open-mercato/shared/lib/i18n/translate";
import { CurrencyBreakdownTable } from "./CurrencyBreakdownTable.js";
function LaneCurrencyBreakdown({
  rows,
  baseCurrencyCode,
  totalInBaseCurrency,
  convertedAll,
  missingRateCurrencies,
  triggerLabel,
  triggerClassName,
  headingLabel,
  headingCount
}) {
  const t = useT();
  const [open, setOpen] = React.useState(false);
  const closeLabel = translateWithFallback(t, "customers.deals.kanban.filter.close", "Close");
  const titleLabel = translateWithFallback(
    t,
    "customers.deals.kanban.currencyBreakdown.title",
    "Lane breakdown"
  );
  const headingCountLabel = translateWithFallback(
    t,
    "customers.deals.kanban.currencyBreakdown.headingCount",
    "({count} deals)",
    { count: headingCount }
  );
  return /* @__PURE__ */ jsxs(Popover, { open, onOpenChange: setOpen, children: [
    /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsx(
      Button,
      {
        variant: "ghost",
        size: "sm",
        type: "button",
        "aria-label": translateWithFallback(
          t,
          "customers.deals.kanban.lane.currencyBreakdown.trigger",
          "Show per-currency breakdown ({count} currencies)",
          { count: rows.length }
        ),
        className: triggerClassName ?? "inline-flex shrink-0 items-center rounded-full bg-muted px-2 py-1 text-xs font-bold leading-normal text-muted-foreground transition-colors hover:bg-muted/80 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
        children: triggerLabel
      }
    ) }),
    /* @__PURE__ */ jsx(
      PopoverContent,
      {
        align: "end",
        className: "w-80 rounded-2xl border-border bg-transparent p-0 shadow-xl",
        onCloseAutoFocus: (event) => event.preventDefault(),
        children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col overflow-hidden rounded-2xl bg-card", children: [
          /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between border-b border-border bg-card px-5 py-4", children: [
            /* @__PURE__ */ jsx("span", { className: "text-base font-bold leading-normal text-foreground", children: titleLabel }),
            /* @__PURE__ */ jsx(
              IconButton,
              {
                variant: "ghost",
                size: "xs",
                onClick: () => setOpen(false),
                "aria-label": closeLabel,
                className: "flex size-7 items-center justify-center rounded-md border border-border bg-card text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
                children: /* @__PURE__ */ jsx(X, { className: "size-3.5", "aria-hidden": "true" })
              }
            )
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1.5 bg-card px-6 pt-3.5 pb-2", children: [
            /* @__PURE__ */ jsxs("span", { className: "text-overline font-semibold uppercase tracking-wider text-muted-foreground", children: [
              headingLabel,
              " ",
              headingCountLabel
            ] }),
            /* @__PURE__ */ jsxs("div", { className: "flex items-baseline gap-2", children: [
              /* @__PURE__ */ jsx("span", { className: "text-2xl font-bold leading-normal text-foreground", children: formatAmount(totalInBaseCurrency) }),
              baseCurrencyCode ? /* @__PURE__ */ jsx("span", { className: "text-sm font-normal leading-normal text-muted-foreground", children: baseCurrencyCode }) : null
            ] })
          ] }),
          /* @__PURE__ */ jsx(
            CurrencyBreakdownTable,
            {
              rows,
              baseCurrencyCode,
              missingRateCurrencies
            }
          ),
          /* @__PURE__ */ jsxs("div", { className: "bg-card px-6 pt-3 pb-3.5", children: [
            /* @__PURE__ */ jsx("p", { className: "text-xs font-normal leading-normal text-muted-foreground", children: translateWithFallback(
              t,
              "customers.deals.kanban.currencyBreakdown.footerCredit",
              "NBP mid rate \xB7 {date}",
              { date: formatToday() }
            ) }),
            !convertedAll && missingRateCurrencies.length > 0 ? /* @__PURE__ */ jsx("p", { className: "mt-2 text-xs leading-normal text-muted-foreground", children: translateWithFallback(
              t,
              "customers.deals.kanban.lane.currencyBreakdown.missingRatesHint",
              "Missing FX rates for {currencies} \u2014 excluded from total. Configure exchange rates to see the full converted value.",
              { currencies: missingRateCurrencies.join(", ") }
            ) }) : null
          ] })
        ] })
      }
    )
  ] });
}
function formatAmount(amount) {
  return new Intl.NumberFormat(void 0, {
    style: "decimal",
    maximumFractionDigits: 0,
    useGrouping: true
  }).format(Math.round(amount));
}
function formatToday() {
  return new Intl.DateTimeFormat(void 0, {
    year: "numeric",
    month: "short",
    day: "2-digit"
  }).format(/* @__PURE__ */ new Date());
}
var LaneCurrencyBreakdown_default = LaneCurrencyBreakdown;
export {
  LaneCurrencyBreakdown,
  LaneCurrencyBreakdown_default as default
};
//# sourceMappingURL=LaneCurrencyBreakdown.js.map
