"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { X } from "lucide-react";
import { Button } from "@open-mercato/ui/primitives/button";
import {
  Popover,
  PopoverContent,
  PopoverTrigger
} from "@open-mercato/ui/primitives/popover";
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";
const STATUS_OPTIONS = [
  {
    value: "open",
    labelKey: "customers.deals.kanban.filter.status.open",
    labelFallback: "Open",
    dotClass: "bg-status-success-icon"
  },
  {
    value: "win",
    labelKey: "customers.deals.kanban.filter.status.won",
    labelFallback: "Won",
    dotClass: "bg-status-warning-icon"
  },
  {
    value: "loose",
    labelKey: "customers.deals.kanban.filter.status.lost",
    labelFallback: "Lost",
    dotClass: "bg-status-neutral-icon"
  }
];
function StatusFilterPopover({ values, onApply }) {
  const t = useT();
  const [open, setOpen] = React.useState(false);
  const [draft, setDraft] = React.useState(values);
  React.useEffect(() => {
    if (open) setDraft(values);
  }, [open, values]);
  const chipLabel = translateWithFallback(t, "customers.deals.kanban.filter.status", "Status");
  const chipValue = values.length === 0 ? translateWithFallback(t, "customers.deals.kanban.filter.all", "All") : values.map((value) => {
    const option = STATUS_OPTIONS.find((entry) => entry.value === value);
    return option ? translateWithFallback(t, option.labelKey, option.labelFallback) : value;
  }).join(", ");
  const toggleDraft = (value) => {
    setDraft(
      (prev) => prev.includes(value) ? prev.filter((entry) => entry !== value) : [...prev, value]
    );
  };
  const handleApply = () => {
    onApply(draft);
    setOpen(false);
  };
  const handleKeyDown = (event) => {
    if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
      event.preventDefault();
      handleApply();
    }
  };
  return /* @__PURE__ */ jsxs(Popover, { open, onOpenChange: setOpen, children: [
    /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsx(ChipButton, { label: chipLabel, value: chipValue, active: values.length > 0 }) }),
    /* @__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: /* @__PURE__ */ jsxs(Fragment, { children: [
              /* @__PURE__ */ jsx("span", { className: "font-bold", children: translateWithFallback(t, "customers.deals.kanban.filter.status.title.label", "Filter : ") }),
              /* @__PURE__ */ jsx("span", { className: "font-normal", children: translateWithFallback(t, "customers.deals.kanban.filter.status", "Status") })
            ] }),
            onClose: () => setOpen(false),
            onCancel: () => setOpen(false),
            onApply: handleApply,
            footerLeft: /* @__PURE__ */ jsxs("span", { children: [
              draft.length,
              " ",
              translateWithFallback(t, "customers.deals.kanban.filter.selected", "selected")
            ] }),
            children: [
              /* @__PURE__ */ jsx("span", { className: "text-xs font-semibold uppercase leading-normal tracking-wide text-muted-foreground", children: translateWithFallback(t, "customers.deals.kanban.filter.status", "Status") }),
              /* @__PURE__ */ jsx("div", { className: "flex flex-wrap items-center gap-x-1.5 gap-y-2", children: STATUS_OPTIONS.map((option) => {
                const isSelected = draft.includes(option.value);
                const label = translateWithFallback(t, option.labelKey, option.labelFallback);
                return /* @__PURE__ */ jsxs(
                  Button,
                  {
                    type: "button",
                    variant: "ghost",
                    size: "2xs",
                    onClick: () => toggleDraft(option.value),
                    "aria-pressed": isSelected,
                    className: `gap-1.5 rounded-full px-2.5 py-1.5 text-xs leading-normal ${isSelected ? "bg-muted font-semibold text-foreground" : "border border-border bg-card font-normal text-muted-foreground hover:bg-muted"}`,
                    children: [
                      /* @__PURE__ */ jsx(
                        "span",
                        {
                          className: `inline-block size-2 shrink-0 rounded-full ${option.dotClass}`,
                          "aria-hidden": "true"
                        }
                      ),
                      /* @__PURE__ */ jsx("span", { children: label }),
                      isSelected ? /* @__PURE__ */ jsx(X, { className: "size-2.5 shrink-0 text-muted-foreground", "aria-hidden": "true" }) : null
                    ]
                  },
                  option.value
                );
              }) })
            ]
          }
        )
      }
    )
  ] });
}
var StatusFilterPopover_default = StatusFilterPopover;
export {
  StatusFilterPopover,
  StatusFilterPopover_default as default
};
//# sourceMappingURL=StatusFilterPopover.js.map
