"use client";
import { jsx as r, jsxs as L, Fragment as X } from "react/jsx-runtime";
import { Command as ue, useCommandState as ce } from "../../node_modules/cmdk/dist/index.js";
import * as o from "react";
import { useEffect as C, forwardRef as fe } from "react";
import { Badge as me } from "./badge.js";
import { CommandItem as I, Command as q, CommandList as ge, CommandGroup as he } from "./command.js";
import { cn as v } from "../../lib/utils.js";
import { Icon as pe } from "./icon.js";
import { Label as ve } from "./label.js";
import { Popover as be, PopoverTrigger as we, PopoverContent as xe } from "./popover.js";
import Ne from "../../node_modules/lucide-react/dist/esm/icons/x.js";
function ye(s, l) {
  const [i, u] = o.useState(s);
  return C(() => {
    const a = setTimeout(() => u(s), l);
    return () => {
      clearTimeout(a);
    };
  }, [s, l]), i;
}
function T(s, l) {
  if (s.length === 0)
    return {};
  if (!l)
    return {
      "": s
    };
  const i = {};
  return s.forEach((u) => {
    const a = u[l] || "";
    i[a] || (i[a] = []), i[a].push(u);
  }), i;
}
function Ce(s, l) {
  const i = JSON.parse(JSON.stringify(s));
  for (const [u, a] of Object.entries(i))
    i[u] = a.filter(
      (V) => !l.find((c) => c.value === V.value)
    );
  return i;
}
function ke(s, l) {
  for (const [, i] of Object.entries(s))
    if (i.some((u) => l.find((a) => a.value === u.value)))
      return !0;
  return !1;
}
const Q = fe(({ className: s, ...l }, i) => ce((a) => a.filtered.count === 0) ? /* @__PURE__ */ r(
  "div",
  {
    ref: i,
    className: v("py-6 text-center text-sm", s),
    "cmdk-empty": "",
    role: "presentation",
    ...l
  }
) : null);
Q.displayName = "CommandEmpty";
const Ee = o.forwardRef(
  ({
    value: s,
    onChange: l,
    placeholder: i = "Cerca...",
    defaultOptions: u = [],
    options: a,
    delay: V,
    onSearch: c,
    onSearchSync: j,
    loadingIndicator: Y,
    emptyIndicator: D,
    maxSelected: _ = Number.MAX_SAFE_INTEGER,
    onMaxSelected: A,
    hidePlaceholderWhenSelected: U = !0,
    disabled: N,
    groupBy: b,
    className: Z,
    badgeClassName: B,
    selectFirstItem: P = !0,
    creatable: k = !1,
    triggerSearchOnFocus: R = !1,
    commandProps: m,
    inputProps: E,
    hideClearAllButton: ee = !1,
    label: W
  }, te) => {
    const g = o.useRef(null), [f, M] = o.useState(!1), [ne, z] = o.useState(!1), [H, $] = o.useState(!1), [re, oe] = o.useState(
      void 0
    ), F = o.useRef(null), J = o.useRef(null), [t, y] = o.useState(s || []), [w, K] = o.useState(
      T(u, b)
    ), [h, G] = o.useState(""), x = ye(h, V || 500);
    o.useImperativeHandle(
      te,
      () => ({
        selectedValue: [...t],
        input: g.current,
        focus: () => g?.current?.focus(),
        reset: () => y([])
      }),
      [t]
    );
    const p = o.useCallback(
      (e) => {
        F.current && !F.current.contains(e.target) && g.current && !g.current.contains(e.target) && (M(!1), g.current.blur());
      },
      []
    ), S = o.useCallback(
      (e) => {
        const n = t.filter((d) => d.value !== e.value);
        y(n), l?.(n);
      },
      [l, t]
    ), se = o.useCallback(
      (e) => {
        const n = g.current;
        n && ((e.key === "Delete" || e.key === "Backspace") && n.value === "" && t.length > 0 && (t[t.length - 1].fixed || S(t[t.length - 1])), e.key === "Escape" && n.blur());
      },
      [S, t]
    );
    C(() => {
      if (typeof document < "u")
        return f ? (document.addEventListener("mousedown", p), document.addEventListener("touchend", p)) : (document.removeEventListener("mousedown", p), document.removeEventListener("touchend", p)), () => {
          document.removeEventListener("mousedown", p), document.removeEventListener("touchend", p);
        };
    }, [f, p]), C(() => {
      s && y(s);
    }, [s]), C(() => {
      if (!a || c)
        return;
      const e = T(a || [], b);
      JSON.stringify(e) !== JSON.stringify(w) && K(e);
    }, [u, a, b, c, w]), C(() => {
      const e = () => {
        const d = j?.(x);
        K(T(d || [], b));
      };
      (async () => {
        !j || !f || (R && e(), x && e());
      })();
    }, [
      x,
      b,
      f,
      R,
      j
    ]), C(() => {
      const e = async () => {
        $(!0);
        const d = await c?.(x);
        K(T(d || [], b)), $(!1);
      };
      (async () => {
        !c || !f || (R && await e(), x && await e());
      })();
    }, [x, b, f, R, c]), o.useEffect(() => {
      if (f && J.current) {
        const e = J.current.getBoundingClientRect();
        oe(e.width);
      }
    }, [f, p]);
    const le = () => {
      if (!k || ke(w, [{ value: h, label: h }]) || t.find((n) => n.value === h))
        return;
      const e = /* @__PURE__ */ r(
        I,
        {
          value: h,
          className: "cursor-pointer",
          onMouseDown: (n) => {
            n.preventDefault(), n.stopPropagation();
          },
          onSelect: (n) => {
            if (t.length >= _) {
              A?.(t.length);
              return;
            }
            G("");
            const d = [...t, { value: n, label: n }];
            y(d), l?.(d);
          },
          children: `Create "${h}"`
        }
      );
      if (!c && h.length > 0 || c && x.length > 0 && !H)
        return e;
    }, ie = o.useCallback(() => {
      if (D)
        return c && !k && Object.keys(w).length === 0 ? /* @__PURE__ */ r(I, { value: "-", disabled: !0, children: D }) : /* @__PURE__ */ r(Q, { children: D });
    }, [k, D, c, w]), de = o.useMemo(
      () => Ce(w, t),
      [w, t]
    ), ae = o.useCallback(() => {
      if (m?.filter)
        return m.filter;
      if (k)
        return (e, n) => e.toLowerCase().includes(n.toLowerCase()) ? 1 : -1;
    }, [k, m?.filter]);
    return /* @__PURE__ */ L("div", { className: "flex flex-col space-y-1.5", children: [
      W && /* @__PURE__ */ r(ve, { className: "w-fit", children: W }),
      /* @__PURE__ */ r(
        q,
        {
          ref: F,
          ...m,
          onKeyDown: (e) => {
            se(e), m?.onKeyDown?.(e);
          },
          className: v(
            "bg-d-input border-d-border h-auto overflow-visible",
            m?.className
          ),
          shouldFilter: m?.shouldFilter !== void 0 ? m.shouldFilter : !c,
          filter: ae(),
          children: /* @__PURE__ */ L(be, { open: f, onOpenChange: M, children: [
            /* @__PURE__ */ r(we, { asChild: !0, children: /* @__PURE__ */ r(
              "div",
              {
                ref: J,
                className: v(
                  "border-d-border/50 ring-offset-d-background min-h-10 rounded-md border text-base md:text-sm",
                  "focus-within:outline-none",
                  "focus-visible-within:ring-d-ring focus-visible-within:ring-1",
                  f && "ring-d-ring ring-1",
                  {
                    "px-3 py-2": t.length !== 0,
                    "cursor-text": !N && t.length !== 0,
                    "disabled:cursor-not-allowed disabled:opacity-50": N
                  },
                  Z
                ),
                children: /* @__PURE__ */ L("div", { className: "relative flex flex-wrap gap-1", children: [
                  t.map((e) => /* @__PURE__ */ L(
                    me,
                    {
                      className: v(
                        "data-[disabled]:bg-d-secondary-foreground data-[disabled]:text-d-secondary data-[disabled]:hover:bg-d-secondary-foreground",
                        "data-[fixed]:bg-d-secondary-foreground data-[fixed]:text-d-secondary data-[fixed]:hover:bg-d-secondary-foreground",
                        B
                      ),
                      "data-fixed": e.fixed,
                      "data-disabled": N || void 0,
                      children: [
                        e.label,
                        /* @__PURE__ */ r(
                          "button",
                          {
                            type: "button",
                            className: v(
                              "text-d-secondary-foreground ring-offset-d-background focus:ring-d-ring ml-1 rounded-full outline-none focus:ring-1 focus:ring-offset-2",
                              (N || e.fixed) && "hidden"
                            ),
                            onKeyDown: (n) => {
                              n.key === "Enter" && S(e);
                            },
                            onMouseDown: (n) => {
                              n.preventDefault(), n.stopPropagation();
                            },
                            onClick: () => S(e),
                            children: /* @__PURE__ */ r(Ne, { className: "text-d-secondary-foreground hover:text-d-foreground h-3 w-3 cursor-pointer" })
                          }
                        )
                      ]
                    },
                    e.value
                  )),
                  /* @__PURE__ */ r(
                    ue.Input,
                    {
                      ...E,
                      ref: g,
                      value: h,
                      disabled: N,
                      onValueChange: (e) => {
                        G(e), E?.onValueChange?.(e);
                      },
                      onBlur: (e) => {
                        ne || M(!1), E?.onBlur?.(e);
                      },
                      onFocus: (e) => {
                        M(!0), E?.onFocus?.(e);
                      },
                      placeholder: U && t.length !== 0 ? "" : i,
                      className: v(
                        "placeholder:text-d-muted-foreground flex-1 bg-transparent outline-none",
                        {
                          "w-full": U,
                          "px-3 py-2": t.length === 0,
                          "ml-1": t.length !== 0
                        },
                        E?.className
                      )
                    }
                  ),
                  /* @__PURE__ */ r(
                    "div",
                    {
                      className: v(
                        "border-d-input bg-d-accent cursor-pointer rounded-full p-1",
                        (ee || N || t.length < 1 || t.filter((e) => e.fixed).length === t.length) && "hidden"
                      ),
                      onClick: () => {
                        y(t.filter((e) => e.fixed)), l?.(t.filter((e) => e.fixed));
                      },
                      "aria-label": "Clear selection",
                      children: /* @__PURE__ */ r(
                        pe,
                        {
                          name: "X",
                          size: "xs",
                          className: "!text-d-foreground opacity-50"
                        }
                      )
                    }
                  )
                ] })
              }
            ) }),
            /* @__PURE__ */ r(
              xe,
              {
                className: "border-none p-0 shadow-sm",
                style: { width: re },
                onWheel: (e) => e.stopPropagation(),
                children: /* @__PURE__ */ r(q, { shouldFilter: !1, className: "w-full", children: /* @__PURE__ */ r(
                  ge,
                  {
                    className: "bg-d-popover text-d-popover-foreground animate-in border-d-border w-full rounded-md border shadow-md outline-none",
                    onMouseLeave: () => {
                      z(!1);
                    },
                    onMouseEnter: () => {
                      z(!0);
                    },
                    onMouseUp: () => {
                      g?.current?.focus();
                    },
                    children: H ? /* @__PURE__ */ r(X, { children: Y }) : /* @__PURE__ */ L(X, { children: [
                      ie(),
                      le(),
                      !P && /* @__PURE__ */ r(I, { value: "-", className: "hidden" }),
                      Object.entries(de).map(([e, n]) => /* @__PURE__ */ r(
                        he,
                        {
                          heading: e,
                          className: "h-full overflow-auto",
                          children: /* @__PURE__ */ r(X, { children: n.map((d) => /* @__PURE__ */ r(
                            I,
                            {
                              value: d.label,
                              disabled: d.disable,
                              onMouseDown: (O) => {
                                O.preventDefault(), O.stopPropagation();
                              },
                              onSelect: () => {
                                if (t.length >= _) {
                                  A?.(t.length);
                                  return;
                                }
                                G("");
                                const O = [...t, d];
                                y(O), l?.(O);
                              },
                              className: v(
                                "cursor-pointer",
                                d.disable && "text-d-secondary-foreground cursor-default"
                              ),
                              children: d.label
                            },
                            d.value
                          )) })
                        },
                        e
                      ))
                    ] })
                  }
                ) })
              }
            )
          ] })
        }
      )
    ] });
  }
);
Ee.displayName = "MultipleSelector";
export {
  Ee as MultipleSelector,
  ye as useDebounce
};
