"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Tag } from "lucide-react";
import {
  Popover,
  PopoverContent,
  PopoverTrigger
} from "@open-mercato/ui/primitives/popover";
import { Radio, RadioGroup } from "@open-mercato/ui/primitives/radio";
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 SORT_LABEL_KEYS = {
  updated_desc: { key: "customers.deals.kanban.sort.option.updatedNewest", fallback: "Updated (newest)" },
  updated_asc: { key: "customers.deals.kanban.sort.option.updatedOldest", fallback: "Updated (oldest)" },
  created_desc: { key: "customers.deals.kanban.sort.option.createdNewest", fallback: "Created (newest)" },
  value_desc: { key: "customers.deals.kanban.sort.option.valueHigh", fallback: "Value (high to low)" },
  value_asc: { key: "customers.deals.kanban.sort.option.valueLow", fallback: "Value (low to high)" },
  probability_desc: {
    key: "customers.deals.kanban.sort.option.probabilityHigh",
    fallback: "Probability (high to low)"
  },
  close_asc: { key: "customers.deals.kanban.sort.option.closeSoonest", fallback: "Close date (soonest)" },
  owner_asc: { key: "customers.deals.kanban.sort.option.ownerAsc", fallback: "Owner (A \u2192 Z)" }
};
const SORT_ORDER = [
  "updated_desc",
  "updated_asc",
  "created_desc",
  "value_desc",
  "value_asc",
  "probability_desc",
  "close_asc",
  "owner_asc"
];
const DEFAULT_SORT = "updated_desc";
function SortByPopover({ value, onApply }) {
  const t = useT();
  const [open, setOpen] = React.useState(false);
  const [draft, setDraft] = React.useState(value);
  React.useEffect(() => {
    if (open) setDraft(value);
  }, [open, value]);
  const chipLabel = translateWithFallback(t, "customers.deals.kanban.sort.label", "Sort");
  const chipValue = translateWithFallback(t, SORT_LABEL_KEYS[value].key, SORT_LABEL_KEYS[value].fallback);
  const handleApply = () => {
    onApply(draft);
    setOpen(false);
  };
  const handleKeyDown = (event) => {
    if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
      event.preventDefault();
      handleApply();
    }
  };
  const footerLeft = /* @__PURE__ */ jsx("span", { children: translateWithFallback(
    t,
    "customers.deals.kanban.sort.defaultHint",
    "Default: {label}",
    {
      label: translateWithFallback(
        t,
        SORT_LABEL_KEYS[DEFAULT_SORT].key,
        SORT_LABEL_KEYS[DEFAULT_SORT].fallback
      )
    }
  ) });
  return /* @__PURE__ */ jsxs(Popover, { open, onOpenChange: setOpen, children: [
    /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsx(ChipButton, { label: chipLabel, value: chipValue, active: value !== DEFAULT_SORT }) }),
    /* @__PURE__ */ jsx(
      PopoverContent,
      {
        className: "w-96 rounded-2xl border-border bg-transparent p-0 shadow-xl",
        align: "end",
        onKeyDown: handleKeyDown,
        children: /* @__PURE__ */ jsx(
          FilterPopoverShell,
          {
            title: translateWithFallback(t, "customers.deals.kanban.sort.title", "Sort by"),
            leadingIcon: /* @__PURE__ */ jsx(Tag, { className: "size-4" }),
            onClose: () => setOpen(false),
            onCancel: () => setOpen(false),
            onApply: handleApply,
            footerLeft,
            children: /* @__PURE__ */ jsx(
              RadioGroup,
              {
                value: draft,
                onValueChange: (next) => setDraft(next),
                className: "gap-0",
                children: SORT_ORDER.map((option) => {
                  const isSelected = draft === option;
                  const isDefault = option === DEFAULT_SORT;
                  const rowClass = isSelected ? "bg-muted" : "bg-card";
                  const inputId = `sort-by-${option}`;
                  return /* @__PURE__ */ jsxs(
                    "label",
                    {
                      htmlFor: inputId,
                      className: `flex w-full cursor-pointer items-center gap-3 rounded-md px-4 py-2.5 text-left transition-colors hover:bg-muted ${rowClass}`,
                      children: [
                        /* @__PURE__ */ jsx(Radio, { id: inputId, value: option }),
                        /* @__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 ${isSelected ? "font-semibold" : "font-normal"}`,
                              children: translateWithFallback(
                                t,
                                SORT_LABEL_KEYS[option].key,
                                SORT_LABEL_KEYS[option].fallback
                              )
                            }
                          ),
                          isDefault && isSelected ? /* @__PURE__ */ jsx("span", { className: "text-xs font-normal leading-normal text-muted-foreground", children: translateWithFallback(t, "customers.deals.kanban.sort.default", "Default") }) : null
                        ] })
                      ]
                    },
                    option
                  );
                })
              }
            )
          }
        )
      }
    )
  ] });
}
var SortByPopover_default = SortByPopover;
export {
  SORT_LABEL_KEYS,
  SortByPopover,
  SortByPopover_default as default
};
//# sourceMappingURL=SortByPopover.js.map
