import { jsx as c, jsxs as y, Fragment as Q } from "react/jsx-runtime";
import { useState as E, useRef as R, useEffect as D, useLayoutEffect as Y } from "react";
import { isEmpty as _ } from "es-toolkit/compat";
import { noop as b } from "es-toolkit/function";
import C from "../../utils/classNames.js";
import { useDropDirection as Z } from "../../utils/useDropDirection.js";
import { DOWN as g, scrollItemIntoView as T, UP as N } from "../../utils/scrollItemIntoView.js";
import $ from "../../hooks/useKey.js";
import ee from "./DropdownHeader.js";
import te from "./NoItemMessage.js";
import se from "../spinner/Spinner.js";
const B = "data-item-id", re = 0, H = "focus", L = g, Ie = (h) => {
  const {
    isOpen: s = !1,
    isLoading: l = !1,
    updateDOMValues: O = b,
    onOpen: i = b,
    onSelect: u = b,
    onClose: w = b,
    options: n = [],
    autoDropDirection: V = !0,
    dropup: j = !1,
    pullRight: K = !1,
    useActiveClass: G = !1,
    focusedItemIndex: p,
    keyboardUsed: m,
    noItemMessage: P,
    dropdownClassName: W
  } = h, [o, k] = E(p || re), [X, A] = E(m), x = R(L), r = R(null);
  D(() => {
    s && r.current && (r.current.scrollTop = 0);
  }, [s]), D(() => {
    typeof p == "number" && k(p), typeof m == "boolean" && A(m), s || (x.current = L);
  }, [p, m, s]);
  const f = Z({ pullRight: K, dropup: j, autoDropDirection: V, dropdownMenuRef: r }, [s]);
  Y(() => {
    if (r.current) {
      const e = r.current.parentElement;
      f.dropup ? e?.classList.add("dropup") : e?.classList.remove("dropup");
    }
  }, [f, r]), D(() => {
    O(q());
  }, [n]), D(() => {
    s && T(
      x.current || L,
      r.current,
      I()
    );
  }, [o, s]);
  const [v, U] = E(s);
  s && !v ? (i(f.dropup), U(s)) : !s && v && (w(), U(s)), $((e) => {
    if (s)
      switch (e.key) {
        case "Escape": {
          w();
          break;
        }
        case "Tab": {
          w();
          break;
        }
        case "Enter": {
          z(e);
          break;
        }
        case "ArrowUp": {
          e.preventDefault(), S(N), T(N, r.current, I());
          break;
        }
        case "ArrowDown": {
          e.preventDefault(), S(g), T(g, r.current, I());
          break;
        }
      }
  });
  const S = (e) => {
    let t = 0;
    switch (e) {
      case N: {
        t = o <= 0 ? o : o - 1;
        break;
      }
      case g: {
        t = o === n.length - 1 ? o : o + 1;
        break;
      }
    }
    x.current = e;
    const a = e === N ? n.length - 1 : 0;
    k(t < 0 ? a : t), A(!0);
  }, F = () => r.current?.getElementsByTagName("a") || [], q = () => {
    if (r.current)
      return [...F()].map(
        (t) => ({
          id: t.getAttribute(B),
          text: t.textContent
        })
      );
  }, I = () => [...F()].find((t) => t.className.includes(H)), z = (e) => {
    if (e.preventDefault(), _(n))
      return;
    const t = I();
    if (t) {
      const a = n.find((d) => d.id === t.getAttribute(B));
      u(a);
    }
  }, J = (e) => {
    e.preventDefault();
    const t = e.currentTarget.getElementsByTagName("input")[0].value, a = n.find((d) => d.id === t);
    u(a);
  }, M = C("dropdown-menu", f.pullRight && "pull-right", W);
  return _(n) ? /* @__PURE__ */ c(te, { noItemMessage: P, className: M }) : /* @__PURE__ */ y("ul", { className: M, ref: r, role: "menu", children: [
    l && /* @__PURE__ */ c("div", { className: "display-flex justify-content-center padding-10", children: /* @__PURE__ */ c(se, {}) }),
    !l && n.map((e, t) => {
      if (e.header)
        return /* @__PURE__ */ c(ee, { icon: e.icon, label: e.label }, e.id);
      const a = C(
        X && o === t ? H : "",
        e.disabled && "pointer-events-none",
        "display-flex align-items-center gap-3"
      ), d = C(
        e.disabled && "disabled",
        G && e.selected && "active"
      );
      return /* @__PURE__ */ c("li", { className: d, role: "listitem", children: /* @__PURE__ */ y(
        "a",
        {
          role: "menuitem",
          className: a,
          "data-item-id": e.id,
          "data-item-index": t,
          onClick: e.disabled ? void 0 : J,
          children: [
            /* @__PURE__ */ y("span", { className: "selected-option-dropdown-item", children: [
              e.icon && /* @__PURE__ */ c(Q, { children: e.icon }),
              e.label
            ] }),
            /* @__PURE__ */ c("input", { type: "hidden", value: e.id })
          ]
        }
      ) }, e.id);
    })
  ] });
}, De = (h, s, l) => {
  const O = h.filter((i) => i.text.toLowerCase().includes(s.toLowerCase()));
  return l.filter((i) => O.find((u) => u.id === i.id));
};
export {
  Ie as default,
  De as filterOptions
};
//# sourceMappingURL=BaseSelectDropdown.js.map
