import { jsxs as _, jsx as i, Fragment as Z } from "react/jsx-runtime";
import { t as V } from "../../classix-DG18itHa.js";
import ee, { useRef as g, useState as O, useEffect as P, Children as te, isValidElement as x } from "react";
import { Button as A } from "../button/index.js";
import { i as ne } from "../../icons-06oXnjRF.js";
import { Input as re } from "../input/index.js";
import { i as oe } from "../../styles.module-BUYR37kC.js";
import { List as se } from "../list/index.js";
import { s as le, S as ie } from "../../index-DXvkoITF.js";
import { r as G } from "../../utils--n2yqjCy.js";
import '../../assets/index18.css';const ae = "_dropdown_1dsxx_1", ce = "_empty_1dsxx_4", pe = "_group_1dsxx_5", ue = "_placeholder_1dsxx_6", de = "_filter_1dsxx_14", fe = "_native_1dsxx_25", me = "_option_1dsxx_35", he = "_options_1dsxx_57", xe = "_panel_1dsxx_81", ge = "_selected_1dsxx_104", _e = "_text_1dsxx_108", ve = "_trigger_1dsxx_117", ye = "_minimal_1dsxx_133", l = {
  dropdown: ae,
  empty: ce,
  group: pe,
  placeholder: ue,
  filter: de,
  native: fe,
  option: me,
  options: he,
  panel: xe,
  selected: ge,
  text: _e,
  trigger: ve,
  minimal: ye
};
function K(e, r) {
  return {
    "--icon-svg": ne(e).replace("currentColor", r)
  };
}
function $(e) {
  return typeof e == "string" || typeof e == "number" ? String(e) : Array.isArray(e) ? e.map($).join(" ") : x(e) ? $(e.props.children) : "";
}
function be(e) {
  return x(e) && e.type === "option";
}
function we(e) {
  return x(e) && e.type === "optgroup";
}
function Se(e) {
  return x(e) && e.type === ee.Fragment;
}
function Ne(e) {
  return x(e) && e.type === ie;
}
function B(e, r, o = !1) {
  const s = [];
  return te.forEach(e, (t) => {
    if (Se(t)) {
      s.push(...B(t.props.children, r, o));
      return;
    }
    if (be(t)) {
      const p = $(t.props.children).trim(), u = t.props.value != null ? String(t.props.value) : p;
      s.push({
        disabled: o || !!t.props.disabled,
        groupLabel: r,
        hidden: !!t.props.hidden,
        isPlaceholder: !!(t.props.hidden && u === ""),
        label: t.props.children,
        selected: !!t.props.selected,
        text: p,
        value: u
      });
      return;
    }
    we(t) && s.push(...B(t.props.children, t.props.label, o || !!t.props.disabled));
  }), s;
}
function Ee(e) {
  const r = e.trim();
  if (!r)
    return null;
  const s = [.../[*?]/.test(r) ? r : `*${r}*`].map((t) => t === "*" ? ".*" : t === "?" ? "." : t.replace(/[|\\{}()[\]^$+.,]/g, "\\$&")).join("");
  return new RegExp(`^${s}$`, "i");
}
function Ce(e, r) {
  const o = Ee(r), s = e.filter((t) => !t.hidden && !t.isPlaceholder);
  return o ? s.filter((t) => o.test([t.groupLabel, t.text, t.value].filter(Boolean).join(" "))) : s;
}
function Ve(e, r) {
  return e.find((o) => o.value === r) ?? e.find((o) => o.isPlaceholder) ?? e[0];
}
function Oe(e, r, o) {
  var t;
  if (r != null)
    return String(r);
  if (o != null)
    return String(o);
  const s = e.find((p) => p.selected);
  return s ? s.value : ((t = e[0]) == null ? void 0 : t.value) ?? "";
}
function Pe(e) {
  if (Ne(e)) {
    const {
      children: r,
      placeholder: o,
      ...s
    } = e.props;
    return {
      children: /* @__PURE__ */ _(Z, { children: [
        o && /* @__PURE__ */ i("option", { hidden: !0, value: "", children: o }),
        r
      ] }),
      props: s
    };
  }
  return {
    children: e.props.children,
    props: e.props
  };
}
const ke = ({
  children: e,
  className: r,
  emptyLabel: o = "No matches found",
  searchPlaceholder: s = "Filter",
  variant: t = "default",
  ...p
}) => {
  const u = Pe(e), {
    defaultValue: T,
    onChange: v,
    value: y,
    ...q
  } = u.props, I = le(q), L = g(null), R = g(null), j = g(null), D = g(null), m = B(u.children), h = y != null, b = Oe(m, y, T), [w, S] = O(() => b), [c, N] = O(!1), [F, W] = O(""), E = h ? String(y) : w, Q = {
    default: G("--colors-semantic-content-secondary"),
    white: G("--color-white", "white")
  }, k = t === "minimal" ? Q.white : Q.default, d = Ve(m, E), H = K("chevron", k), J = K("search", k), z = Ce(m, F);
  P(() => {
    !h && !m.some((n) => n.value === w) && S(b);
  }, [b, w, h, m]), P(() => {
    var n;
    if (!c) {
      W("");
      return;
    }
    (n = D.current) == null || n.focus();
  }, [c]), P(() => {
    if (!c)
      return;
    const n = (a) => {
      var f;
      (f = R.current) != null && f.contains(a.target) || N(!1);
    };
    return document.addEventListener("mousedown", n), () => {
      document.removeEventListener("mousedown", n);
    };
  }, [c]);
  const U = (n) => {
    h || S(n.target.value), v == null || v(n);
  }, C = () => {
    var n;
    N(!1), (n = L.current) == null || n.focus();
  }, X = (n) => {
    var M;
    h || S(n);
    const a = j.current;
    if (!a) {
      C();
      return;
    }
    const f = (M = Object.getOwnPropertyDescriptor(HTMLSelectElement.prototype, "value")) == null ? void 0 : M.set;
    f == null || f.call(a, n), a.dispatchEvent(new Event("change", {
      bubbles: !0
    })), C();
  }, Y = (n) => {
    n.key === "Escape" && C();
  };
  return /* @__PURE__ */ _("div", { className: V(l.dropdown, t === "minimal" && l.minimal, r), onKeyDown: Y, ref: R, ...p, "data-component": "dropdown", children: [
    /* @__PURE__ */ i("select", { ...I, "aria-hidden": "true", className: l.native, onChange: U, ref: j, tabIndex: -1, value: E, children: u.children }),
    /* @__PURE__ */ i(A, { "aria-expanded": c, "aria-haspopup": "dialog", className: V(t === "default" && oe.input, l.trigger, (d == null ? void 0 : d.isPlaceholder) && l.placeholder), "data-open": c ? "true" : void 0, disabled: I.disabled, onClick: () => N((n) => !n), ref: L, style: H, variant: "minimal", children: /* @__PURE__ */ i("span", { className: l.text, children: (d == null ? void 0 : d.label) ?? "Select an option" }) }),
    c && /* @__PURE__ */ _("div", { className: l.panel, children: [
      /* @__PURE__ */ i(re, { "aria-label": "Filter", className: l.filter, icon: "search", onChange: (n) => W(n.target.value), placeholder: s, ref: D, style: J, type: "search", value: F }),
      /* @__PURE__ */ i(se, { "aria-label": "Dropdown", className: l.options, children: z.length > 0 ? z.map((n) => {
        const a = n.value === E;
        return /* @__PURE__ */ i("li", { "data-component": "dropdown", children: /* @__PURE__ */ _(A, { "aria-label": a ? `${n.text}, selected` : n.text, className: V(l.option, a && l.selected), disabled: n.disabled, onClick: () => X(n.value), variant: "minimal", children: [
          /* @__PURE__ */ i("span", { className: l.text, children: n.label }),
          n.groupLabel && /* @__PURE__ */ i("small", { className: l.group, children: n.groupLabel })
        ] }) }, `${n.groupLabel ?? "root"}-${n.value}`);
      }) : /* @__PURE__ */ i("li", { className: l.empty, children: o }) })
    ] })
  ] });
};
export {
  ke as Dropdown
};
//# sourceMappingURL=index.js.map
