"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { VisuallyHidden } from "@radix-ui/react-visually-hidden";
import { ChartColumnIncreasing, Clock3, Medal, Trophy } from "lucide-react";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { Button } from "@open-mercato/ui/primitives/button";
import { Dialog, DialogContent, DialogTitle } from "@open-mercato/ui/primitives/dialog";
function formatCurrency(value, currency) {
  if (value === null || !Number.isFinite(value)) return "\u2014";
  if (!currency) return value.toLocaleString();
  try {
    return new Intl.NumberFormat(void 0, { style: "currency", currency }).format(value);
  } catch {
    return `${value.toLocaleString()} ${currency}`;
  }
}
function formatClosedDate(value, t) {
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return t("customers.deals.detail.won.closed", "Closed");
  return date.toLocaleDateString(void 0, { month: "short", day: "numeric", year: "numeric" });
}
function formatSalesCycle(value) {
  if (value === null || !Number.isFinite(value)) return "\u2014";
  if (value >= 30) {
    const months = Math.max(1, Math.round(value / 30));
    return `${months} mo`;
  }
  return `${value}d`;
}
function StatCard({
  icon,
  label,
  value
}) {
  return /* @__PURE__ */ jsxs("div", { className: "rounded-2xl border bg-background px-4 py-4", children: [
    /* @__PURE__ */ jsxs("div", { className: "mb-2 flex items-center gap-2 text-xs font-semibold uppercase tracking-[0.14em] text-muted-foreground", children: [
      icon,
      label
    ] }),
    /* @__PURE__ */ jsx("div", { className: "text-xl font-semibold text-foreground", children: value })
  ] });
}
function DealWonPopup({
  open,
  onClose,
  dealTitle,
  stats,
  onViewDashboard,
  onBackToPipeline
}) {
  const t = useT();
  return /* @__PURE__ */ jsx(Dialog, { open, onOpenChange: (nextOpen) => {
    if (!nextOpen) onClose();
  }, children: /* @__PURE__ */ jsxs(DialogContent, { className: "max-h-[90vh] overflow-hidden p-0 sm:max-w-[420px]", children: [
    /* @__PURE__ */ jsx(VisuallyHidden, { children: /* @__PURE__ */ jsx(DialogTitle, { children: t("customers.deals.detail.won.title", "Closed successfully") }) }),
    /* @__PURE__ */ jsxs("div", { className: "max-h-[90vh] overflow-y-auto rounded-2xl bg-card", children: [
      /* @__PURE__ */ jsx("div", { className: "px-6 pb-5 pt-6", children: /* @__PURE__ */ jsx("div", { className: "flex h-[200px] items-center justify-center rounded-2xl bg-[linear-gradient(135deg,rgba(141,150,244,0.5),rgba(198,203,254,0.95))] text-foreground", children: /* @__PURE__ */ jsx(Trophy, { className: "size-24", strokeWidth: 1.5 }) }) }),
      /* @__PURE__ */ jsxs("div", { className: "space-y-5 px-7 pb-7 text-center", children: [
        /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
          /* @__PURE__ */ jsx("h2", { className: "text-2xl font-bold leading-tight text-foreground", children: t("customers.deals.detail.won.title", "Closed successfully") }),
          /* @__PURE__ */ jsx("p", { className: "text-sm font-medium text-muted-foreground", children: t("customers.deals.detail.won.subtitle", "You are a sales machine!") })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "rounded-xl border bg-muted/20 px-4 py-4", children: [
          /* @__PURE__ */ jsx("p", { className: "text-overline font-bold uppercase tracking-[0.16em] text-muted-foreground", children: dealTitle }),
          /* @__PURE__ */ jsx("p", { className: "mt-2 text-2xl font-bold text-primary", children: stats ? formatCurrency(stats.dealValue, stats.dealCurrency) : "\u2014" }),
          /* @__PURE__ */ jsxs("p", { className: "mt-1 text-xs text-muted-foreground", children: [
            t("customers.deals.detail.won.closed", "Won"),
            " \xB7 ",
            stats ? formatClosedDate(stats.closedAt, t) : "\u2014"
          ] })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-3 gap-2", children: [
          /* @__PURE__ */ jsx(
            StatCard,
            {
              icon: /* @__PURE__ */ jsx(ChartColumnIncreasing, { className: "size-4" }),
              label: t("customers.deals.detail.won.dealsThisWeek", "Deals this week"),
              value: stats ? String(stats.dealsClosedThisPeriod) : "\u2014"
            }
          ),
          /* @__PURE__ */ jsx(
            StatCard,
            {
              icon: /* @__PURE__ */ jsx(Clock3, { className: "size-4" }),
              label: t("customers.deals.detail.won.salesCycle", "Sales cycle"),
              value: formatSalesCycle(stats?.salesCycleDays ?? null)
            }
          ),
          /* @__PURE__ */ jsx(
            StatCard,
            {
              icon: /* @__PURE__ */ jsx(Medal, { className: "size-4" }),
              label: t("customers.deals.detail.won.rank", "Quarter rank"),
              value: stats?.dealRankInQuarter !== null && stats?.dealRankInQuarter !== void 0 ? `#${stats.dealRankInQuarter}` : "\u2014"
            }
          )
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "rounded-2xl border bg-muted/30 px-4 py-4", children: [
          /* @__PURE__ */ jsx("div", { className: "text-xs font-semibold uppercase tracking-[0.14em] text-muted-foreground", children: t("customers.deals.detail.won.pipeline", "Pipeline") }),
          /* @__PURE__ */ jsx("div", { className: "mt-2 text-sm text-foreground", children: stats?.pipelineName ?? t("customers.deals.detail.won.pipelineFallback", "Current pipeline") })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
          onViewDashboard ? /* @__PURE__ */ jsx(Button, { type: "button", className: "w-full", onClick: onViewDashboard, children: t("customers.deals.detail.won.primaryAction", "View sales report") }) : null,
          /* @__PURE__ */ jsx(Button, { type: "button", variant: onViewDashboard ? "outline" : "default", className: "w-full", onClick: onBackToPipeline ?? onClose, children: t("customers.deals.detail.won.secondaryAction", "Back to pipeline") })
        ] })
      ] })
    ] })
  ] }) });
}
export {
  DealWonPopup
};
//# sourceMappingURL=DealWonPopup.js.map
