import { filterDOMProps as x, mergeProps as $, useId as F, chain as v } from "../../utils/dist/module.es.js";
import d, { useMemo as C } from "react";
import { ListKeyboardDelegate as h, useTypeSelect as S } from "../../selection/dist/module.es.js";
import { setInteractionModality as k, useInteractionModality as w } from "../../interactions/dist/module.es.js";
import { useField as M } from "../../label/dist/module.es.js";
import { useMenuTrigger as B } from "../../menu/dist/module.es.js";
import { useVisuallyHidden as E } from "../../visually-hidden/dist/module.es.js";
import { useCollator as T } from "../../i18n/dist/real-module.es.js";
function L(l, e, c) {
  let { keyboardDelegate: u, isDisabled: i } = l, s = T({
    usage: "search",
    sensitivity: "base"
  }), t = C(
    () => u || new h(e.collection, e.disabledKeys, null, s),
    [
      u,
      e.collection,
      e.disabledKeys,
      s
    ]
  ), { menuTriggerProps: b, menuProps: r } = B({
    isDisabled: i,
    type: "listbox"
  }, e, c), f = (o) => {
    switch (o.key) {
      case "ArrowLeft": {
        o.preventDefault();
        let y = e.selectedKey != null ? t.getKeyAbove(e.selectedKey) : t.getFirstKey();
        y && e.setSelectedKey(y);
        break;
      }
      case "ArrowRight": {
        o.preventDefault();
        let y = e.selectedKey != null ? t.getKeyBelow(e.selectedKey) : t.getFirstKey();
        y && e.setSelectedKey(y);
        break;
      }
    }
  }, { typeSelectProps: a } = S({
    keyboardDelegate: t,
    selectionManager: e.selectionManager,
    onTypeSelect(o) {
      e.setSelectedKey(o);
    }
  }), { labelProps: g, fieldProps: p, descriptionProps: P, errorMessageProps: K } = M({
    ...l,
    labelElementType: "span"
  });
  a.onKeyDown = a.onKeyDownCapture, delete a.onKeyDownCapture;
  let D = x(l, {
    labelable: !0
  }), n = $(a, b, p), m = F();
  return {
    labelProps: {
      ...g,
      onClick: () => {
        l.isDisabled || (c.current.focus(), k("keyboard"));
      }
    },
    triggerProps: $(D, {
      ...n,
      isDisabled: i,
      onKeyDown: v(n.onKeyDown, f, l.onKeyDown),
      onKeyUp: l.onKeyUp,
      "aria-labelledby": [
        n["aria-labelledby"],
        n["aria-label"] && !n["aria-labelledby"] ? n.id : null,
        m
      ].filter(Boolean).join(" "),
      onFocus(o) {
        e.isFocused || (l.onFocus && l.onFocus(o), l.onFocusChange && l.onFocusChange(!0), e.setFocused(!0));
      },
      onBlur(o) {
        e.isOpen || (l.onBlur && l.onBlur(o), l.onFocusChange && l.onFocusChange(!1), e.setFocused(!1));
      }
    }),
    valueProps: {
      id: m
    },
    menuProps: {
      ...r,
      autoFocus: e.focusStrategy || !0,
      shouldSelectOnPressUp: !0,
      shouldFocusOnHover: !0,
      disallowEmptySelection: !0,
      onBlur: (o) => {
        o.currentTarget.contains(o.relatedTarget) || (l.onBlur && l.onBlur(o), l.onFocusChange && l.onFocusChange(!1), e.setFocused(!1));
      },
      "aria-labelledby": [
        p["aria-labelledby"],
        n["aria-label"] && !p["aria-labelledby"] ? n.id : null
      ].filter(Boolean).join(" ")
    },
    descriptionProps: P,
    errorMessageProps: K
  };
}
function I(l, e, c) {
  let { autoComplete: u, name: i, isDisabled: s } = l, t = w(), { visuallyHiddenProps: b } = E();
  var r;
  return {
    containerProps: {
      ...b,
      "aria-hidden": !0
    },
    inputProps: {
      type: "text",
      tabIndex: t == null || e.isFocused || e.isOpen ? -1 : 0,
      style: {
        fontSize: 16
      },
      onFocus: () => c.current.focus(),
      disabled: s
    },
    selectProps: {
      tabIndex: -1,
      autoComplete: u,
      disabled: s,
      name: i,
      size: e.collection.size,
      value: (r = e.selectedKey) !== null && r !== void 0 ? r : "",
      onChange: (f) => e.setSelectedKey(f.target.value)
    }
  };
}
function V(l) {
  let { state: e, triggerRef: c, label: u, name: i, isDisabled: s } = l, { containerProps: t, inputProps: b, selectProps: r } = I(l, e, c);
  return e.collection.size <= 300 ? /* @__PURE__ */ d.createElement("div", t, /* @__PURE__ */ d.createElement("input", b), /* @__PURE__ */ d.createElement("label", null, u, /* @__PURE__ */ d.createElement("select", r, /* @__PURE__ */ d.createElement("option", null), [
    ...e.collection.getKeys()
  ].map((f) => {
    let a = e.collection.getItem(f);
    if (a.type === "item")
      return /* @__PURE__ */ d.createElement("option", {
        key: a.key,
        value: a.key
      }, a.textValue);
  })))) : i ? /* @__PURE__ */ d.createElement("input", {
    type: "hidden",
    autoComplete: r.autoComplete,
    name: i,
    disabled: s,
    value: e.selectedKey
  }) : null;
}
export {
  V as HiddenSelect,
  I as useHiddenSelect,
  L as useSelect
};
//# sourceMappingURL=module.es.js.map
