"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { MapPin, MapPinOff } from "lucide-react";
import { Button } from "@open-mercato/ui/primitives/button";
import { EmptyState } from "@open-mercato/ui/primitives/empty-state";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "@open-mercato/ui/primitives/select";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { translateWithFallback } from "@open-mercato/shared/lib/i18n/translate";
import { formatCurrency } from "../../../../../components/detail/utils.js";
const STAGE_BADGE_TONE_CLASS = {
  success: "bg-status-success-bg text-status-success-text",
  error: "bg-status-error-bg text-status-error-text",
  warning: "bg-status-warning-bg text-status-warning-text",
  info: "bg-status-info-bg text-status-info-text",
  neutral: "bg-status-neutral-bg text-status-neutral-text",
  brand: "bg-brand-violet/14 text-brand-violet",
  pink: "bg-status-pink-bg text-status-pink-text"
};
function getStageBadgeClass(tone) {
  if (tone && tone in STAGE_BADGE_TONE_CLASS) return STAGE_BADGE_TONE_CLASS[tone];
  return "bg-muted text-muted-foreground";
}
function haversineKm(a, b) {
  const toRadians = (value) => value * Math.PI / 180;
  const earthRadiusKm = 6371;
  const dLat = toRadians(b.latitude - a.latitude);
  const dLng = toRadians(b.longitude - a.longitude);
  const sinLat = Math.sin(dLat / 2);
  const sinLng = Math.sin(dLng / 2);
  const h = sinLat * sinLat + Math.cos(toRadians(a.latitude)) * Math.cos(toRadians(b.latitude)) * sinLng * sinLng;
  return 2 * earthRadiusKm * Math.asin(Math.min(1, Math.sqrt(h)));
}
function DealsLocationPanel({
  deals,
  locatedCount,
  stageMetaById,
  mapCenter,
  selectedDealId,
  onSelect
}) {
  const t = useT();
  const [panelSort, setPanelSort] = React.useState("proximity");
  const visibleDeals = React.useMemo(() => {
    if (panelSort !== "proximity" || !mapCenter) return deals;
    return deals.slice().sort((a, b) => {
      if (!a.location) return 1;
      if (!b.location) return -1;
      return haversineKm(mapCenter, a.location) - haversineKm(mapCenter, b.location);
    });
  }, [deals, panelSort, mapCenter]);
  const noAddressLabel = translateWithFallback(
    t,
    "customers.deals.map.panel.noAddress",
    "No address coordinates"
  );
  return /* @__PURE__ */ jsxs("div", { className: "flex max-h-[60vh] w-full shrink-0 flex-col overflow-hidden rounded-xl border border-border bg-card lg:max-h-none lg:w-80", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1 border-b border-border px-4 py-3", children: [
      /* @__PURE__ */ jsxs("div", { className: "flex items-baseline justify-between gap-2", children: [
        /* @__PURE__ */ jsx("h2", { className: "text-sm font-semibold leading-normal text-foreground", children: translateWithFallback(t, "customers.deals.map.panel.title", "Deals with location") }),
        /* @__PURE__ */ jsx("span", { className: "text-xs leading-normal text-muted-foreground", children: translateWithFallback(t, "customers.deals.map.panel.count", "{count} located", {
          count: locatedCount
        }) })
      ] }),
      /* @__PURE__ */ jsx("p", { className: "text-xs leading-normal text-muted-foreground", children: translateWithFallback(
        t,
        "customers.deals.map.panel.hint",
        "Click a deal or a map pin to preview it."
      ) }),
      /* @__PURE__ */ jsx("div", { className: "mt-2 flex items-center gap-2", children: /* @__PURE__ */ jsxs(Select, { value: panelSort, onValueChange: (value) => setPanelSort(value), children: [
        /* @__PURE__ */ jsx(
          SelectTrigger,
          {
            size: "sm",
            className: "w-auto min-w-36",
            "aria-label": translateWithFallback(t, "customers.deals.map.panel.sort.label", "Sort deals panel"),
            children: /* @__PURE__ */ jsx(SelectValue, {})
          }
        ),
        /* @__PURE__ */ jsxs(SelectContent, { children: [
          /* @__PURE__ */ jsx(SelectItem, { value: "proximity", children: translateWithFallback(t, "customers.deals.map.panel.sort.proximity", "Closest first") }),
          /* @__PURE__ */ jsx(SelectItem, { value: "listOrder", children: translateWithFallback(t, "customers.deals.map.panel.sort.listOrder", "List order") })
        ] })
      ] }) })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col gap-2 overflow-y-auto p-3", children: [
      deals.length === 0 ? /* @__PURE__ */ jsx(
        EmptyState,
        {
          variant: "subtle",
          size: "sm",
          icon: /* @__PURE__ */ jsx(MapPinOff, { className: "size-6", "aria-hidden": "true" }),
          title: translateWithFallback(
            t,
            "customers.deals.map.panel.empty.title",
            "No deals on the map yet"
          ),
          description: translateWithFallback(
            t,
            "customers.deals.map.panel.empty.description",
            "Add latitude and longitude to company or person addresses to plot their deals here."
          )
        }
      ) : null,
      visibleDeals.map((deal) => {
        const stageMeta = deal.pipelineStageId ? stageMetaById.get(deal.pipelineStageId) ?? null : null;
        const isSelected = deal.id === selectedDealId;
        const locationLine = deal.location ? [deal.location.city, deal.location.region].filter(Boolean).join(", ") || deal.location.country || null : null;
        const valueLabel = typeof deal.valueAmount === "number" ? formatCurrency(deal.valueAmount, deal.valueCurrency) : null;
        const cardAriaLabel = [deal.companyLabel, deal.title, valueLabel, stageMeta?.label, locationLine ?? noAddressLabel].filter((part) => typeof part === "string" && part.length > 0).join(", ") || deal.title;
        return /* @__PURE__ */ jsxs(
          Button,
          {
            type: "button",
            variant: "ghost",
            "data-map-panel-card": deal.id,
            "aria-label": cardAriaLabel,
            "aria-pressed": isSelected,
            onClick: () => onSelect(deal.id),
            className: `h-auto w-full flex-col items-stretch justify-start gap-1 whitespace-normal rounded-lg border border-border p-3 text-left shadow-none ${isSelected ? "bg-muted" : "bg-card"}`,
            children: [
              /* @__PURE__ */ jsxs("div", { className: "flex items-baseline justify-between gap-2", children: [
                /* @__PURE__ */ jsx("span", { className: "truncate text-sm font-medium leading-normal text-foreground", children: deal.companyLabel ?? "\u2014" }),
                valueLabel ? /* @__PURE__ */ jsx("span", { className: "shrink-0 text-sm font-semibold leading-normal text-foreground", children: valueLabel }) : null
              ] }),
              /* @__PURE__ */ jsx("span", { className: "truncate text-sm leading-normal text-muted-foreground", children: deal.title }),
              /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2", children: [
                /* @__PURE__ */ jsxs("span", { className: "flex min-w-0 items-center gap-1.5 text-xs leading-normal text-muted-foreground", children: [
                  /* @__PURE__ */ jsx(MapPin, { className: "size-3.5 shrink-0", "aria-hidden": "true" }),
                  /* @__PURE__ */ jsx("span", { className: "truncate", children: locationLine ?? noAddressLabel })
                ] }),
                stageMeta ? /* @__PURE__ */ jsx(
                  "span",
                  {
                    className: `inline-flex shrink-0 items-center rounded-md px-2 py-0.5 text-xs font-semibold leading-normal ${getStageBadgeClass(stageMeta.tone)}`,
                    children: stageMeta.label
                  }
                ) : null
              ] })
            ]
          },
          deal.id
        );
      })
    ] })
  ] });
}
var DealsLocationPanel_default = DealsLocationPanel;
export {
  DealsLocationPanel,
  DealsLocationPanel_default as default
};
//# sourceMappingURL=DealsLocationPanel.js.map
