"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 { useT } from "@open-mercato/shared/lib/i18n/context";
import { translateWithFallback } from "@open-mercato/shared/lib/i18n/translate";
import { ChipButton } from "./ChipButton.js";
import { FilterPopoverShell } from "./FilterPopoverShell.js";
function RadioDot({ selected }) {
  return /* @__PURE__ */ jsx(
    "span",
    {
      className: `flex size-4 shrink-0 items-center justify-center rounded-full border ${selected ? "border-accent-indigo" : "border-input bg-card"}`,
      "aria-hidden": "true",
      children: selected ? /* @__PURE__ */ jsx("span", { className: "size-2 rounded-full bg-accent-indigo" }) : null
    }
  );
}
function PipelineFilterPopover({
  pipelines,
  selectedPipelineId,
  onApply
}) {
  const t = useT();
  const [open, setOpen] = React.useState(false);
  const [draft, setDraft] = React.useState(selectedPipelineId);
  React.useEffect(() => {
    if (open) setDraft(selectedPipelineId);
  }, [open, selectedPipelineId]);
  const activePipeline = pipelines.find((pipeline) => pipeline.id === selectedPipelineId);
  const totalCount = pipelines.reduce((sum, pipeline) => sum + (pipeline.dealCount ?? 0), 0);
  const chipLabel = translateWithFallback(t, "customers.deals.kanban.filter.pipeline", "Pipeline");
  const chipValue = activePipeline ? activePipeline.name : translateWithFallback(t, "customers.deals.kanban.filter.allCount", "All ({count})", {
    count: pipelines.length
  });
  const handleApply = () => {
    onApply(draft);
    setOpen(false);
  };
  const handleKeyDown = (event) => {
    if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
      event.preventDefault();
      handleApply();
    }
  };
  const allSelected = draft === null;
  const allRowClass = allSelected ? "bg-muted" : "bg-card";
  const footerLeft = draft ? /* @__PURE__ */ jsx("span", { children: translateWithFallback(
    t,
    "customers.deals.kanban.filter.pipeline.selectedOne",
    "1 selected \xB7 {name}",
    { name: pipelines.find((pipeline) => pipeline.id === draft)?.name ?? "" }
  ) }) : /* @__PURE__ */ jsx("span", { children: translateWithFallback(
    t,
    "customers.deals.kanban.filter.pipeline.selectedNone",
    "No pipeline filter"
  ) });
  return /* @__PURE__ */ jsxs(Popover, { open, onOpenChange: setOpen, children: [
    /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsx(ChipButton, { label: chipLabel, value: chipValue, active: !!selectedPipelineId }) }),
    /* @__PURE__ */ jsx(
      PopoverContent,
      {
        className: "w-96 rounded-2xl border-border bg-transparent p-0 shadow-xl",
        align: "start",
        onKeyDown: handleKeyDown,
        children: /* @__PURE__ */ jsxs(
          FilterPopoverShell,
          {
            title: translateWithFallback(t, "customers.deals.kanban.filter.pipeline.title", "Filter \xB7 Pipeline"),
            onClose: () => setOpen(false),
            onCancel: () => setOpen(false),
            onApply: handleApply,
            footerLeft,
            children: [
              /* @__PURE__ */ jsxs(
                Button,
                {
                  type: "button",
                  variant: "ghost",
                  role: "radio",
                  "aria-checked": allSelected,
                  onClick: () => setDraft(null),
                  className: `h-auto w-full justify-start gap-3 px-4 py-2.5 text-left font-normal ${allRowClass}`,
                  children: [
                    /* @__PURE__ */ jsx(RadioDot, { selected: allSelected }),
                    /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 flex-1 flex-col gap-px", children: [
                      /* @__PURE__ */ jsx(
                        "span",
                        {
                          className: `text-sm leading-normal text-foreground ${allSelected ? "font-semibold" : "font-normal"}`,
                          children: translateWithFallback(t, "customers.deals.kanban.filter.pipeline.all", "All pipelines")
                        }
                      ),
                      /* @__PURE__ */ jsx("span", { className: "text-xs font-normal leading-normal text-muted-foreground", children: translateWithFallback(
                        t,
                        "customers.deals.kanban.filter.pipeline.allHelper",
                        "{count} pipelines",
                        { count: pipelines.length }
                      ) })
                    ] }),
                    /* @__PURE__ */ jsx("span", { className: "text-xs font-semibold leading-normal text-accent-indigo", children: totalCount })
                  ]
                }
              ),
              pipelines.map((pipeline) => {
                const isSelected = draft === pipeline.id;
                const rowClass = isSelected ? "bg-muted" : "bg-card";
                return /* @__PURE__ */ jsxs(
                  Button,
                  {
                    type: "button",
                    variant: "ghost",
                    role: "radio",
                    "aria-checked": isSelected,
                    onClick: () => setDraft(pipeline.id),
                    className: `h-auto w-full justify-start gap-3 px-4 py-2.5 text-left font-normal ${rowClass}`,
                    children: [
                      /* @__PURE__ */ jsx(RadioDot, { selected: isSelected }),
                      /* @__PURE__ */ jsx(
                        "span",
                        {
                          className: `flex-1 truncate text-sm leading-normal text-foreground ${isSelected ? "font-semibold" : "font-normal"}`,
                          children: pipeline.name
                        }
                      ),
                      typeof pipeline.dealCount === "number" ? /* @__PURE__ */ jsx(
                        "span",
                        {
                          className: `text-xs leading-normal ${isSelected ? "font-semibold text-accent-indigo" : "font-normal text-muted-foreground"}`,
                          children: pipeline.dealCount
                        }
                      ) : null
                    ]
                  },
                  pipeline.id
                );
              })
            ]
          }
        )
      }
    )
  ] });
}
var PipelineFilterPopover_default = PipelineFilterPopover;
export {
  PipelineFilterPopover,
  PipelineFilterPopover_default as default
};
//# sourceMappingURL=PipelineFilterPopover.js.map
