import I from "../../../@babel/runtime/helpers/esm/extends.js";
import { forwardRef as T, createElement as p, useState as P, useRef as U, useEffect as Z, useCallback as N, Fragment as be, useMemo as ke } from "react";
import { createPortal as Oe } from "react-dom";
import { clamp as ye } from "../../number/dist/index.js";
import { composeEventHandlers as R } from "../../primitive/dist/index.js";
import { createCollection as We } from "../../react-collection/dist/index.js";
import { useComposedRefs as W } from "../../react-compose-refs/dist/index.js";
import { createContextScope as Fe } from "../../react-context/dist/index.js";
import { useDirection as Ke } from "../../react-direction/dist/index.js";
import { DismissableLayer as Ue } from "../../react-dismissable-layer/dist/index.js";
import { useFocusGuards as Ge } from "../../react-focus-guards/dist/index.js";
import { FocusScope as qe } from "../../react-focus-scope/dist/index.js";
import { useId as we } from "../../react-id/dist/index.js";
import { createPopperScope as De, Anchor as ze, Root as Ye, Content as je } from "../../react-popper/dist/index.js";
import { Portal as Xe } from "../../react-portal/dist/index.js";
import { Primitive as A } from "../../react-primitive/dist/index.js";
import { Slot as Ze } from "../../react-slot/dist/index.js";
import { useCallbackRef as Je } from "../../react-use-callback-ref/dist/index.js";
import { useControllableState as Pe } from "../../react-use-controllable-state/dist/index.js";
import { useLayoutEffect as Y } from "../../react-use-layout-effect/dist/index.js";
import { usePrevious as Qe } from "../../react-use-previous/dist/index.js";
import { VisuallyHidden as et } from "../../react-visually-hidden/dist/index.js";
import { hideOthers as tt } from "../../../aria-hidden/dist/es2015/index.js";
import ot from "../../../react-remove-scroll/dist/es2015/Combination.js";
const nt = [
  " ",
  "Enter",
  "ArrowUp",
  "ArrowDown"
], ct = [
  " ",
  "Enter"
], pe = "Select", [fe, ue, rt] = We(pe), [ce, so] = Fe(pe, [
  rt,
  De
]), Ce = De(), [st, J] = ce(pe), [at, lt] = ce(pe), it = (o) => {
  const { __scopeSelect: a, children: e, open: r, defaultOpen: s, onOpenChange: d, value: n, defaultValue: t, onValueChange: c, dir: l, name: m, autoComplete: C, disabled: _, required: y } = o, u = Ce(a), [v, S] = P(null), [$, i] = P(null), [h, re] = P(!1), L = Ke(l), [se = !1, O] = Pe({
    prop: r,
    defaultProp: s,
    onChange: d
  }), [M, j] = Pe({
    prop: n,
    defaultProp: t,
    onChange: c
  }), ee = U(null), X = v ? !!v.closest("form") : !0, [H, G] = P(/* @__PURE__ */ new Set()), q = Array.from(H).map(
    (E) => E.props.value
  ).join(";");
  return /* @__PURE__ */ p(Ye, u, /* @__PURE__ */ p(st, {
    required: y,
    scope: a,
    trigger: v,
    onTriggerChange: S,
    valueNode: $,
    onValueNodeChange: i,
    valueNodeHasChildren: h,
    onValueNodeHasChildrenChange: re,
    contentId: we(),
    value: M,
    onValueChange: j,
    open: se,
    onOpenChange: O,
    dir: L,
    triggerPointerDownPosRef: ee,
    disabled: _
  }, /* @__PURE__ */ p(fe.Provider, {
    scope: a
  }, /* @__PURE__ */ p(at, {
    scope: o.__scopeSelect,
    onNativeOptionAdd: N((E) => {
      G(
        (k) => new Set(k).add(E)
      );
    }, []),
    onNativeOptionRemove: N((E) => {
      G((k) => {
        const F = new Set(k);
        return F.delete(E), F;
      });
    }, [])
  }, e)), X ? /* @__PURE__ */ p(Ve, {
    key: q,
    "aria-hidden": !0,
    required: y,
    tabIndex: -1,
    name: m,
    autoComplete: C,
    value: M,
    onChange: (E) => j(E.target.value),
    disabled: _
  }, M === void 0 ? /* @__PURE__ */ p("option", {
    value: ""
  }) : null, Array.from(H)) : null));
}, dt = "SelectTrigger", pt = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, disabled: r = !1, ...s } = o, d = Ce(e), n = J(dt, e), t = n.disabled || r, c = W(a, n.onTriggerChange), l = ue(e), [m, C, _] = Be((u) => {
    const v = l().filter(
      (i) => !i.disabled
    ), S = v.find(
      (i) => i.value === n.value
    ), $ = He(v, u, S);
    $ !== void 0 && n.onValueChange($.value);
  }), y = () => {
    t || (n.onOpenChange(!0), _());
  };
  return /* @__PURE__ */ p(ze, I({
    asChild: !0
  }, d), /* @__PURE__ */ p(A.button, I({
    type: "button",
    role: "combobox",
    "aria-controls": n.contentId,
    "aria-expanded": n.open,
    "aria-required": n.required,
    "aria-autocomplete": "none",
    dir: n.dir,
    "data-state": n.open ? "open" : "closed",
    disabled: t,
    "data-disabled": t ? "" : void 0,
    "data-placeholder": Le(n.value) ? "" : void 0
  }, s, {
    ref: c,
    onClick: R(s.onClick, (u) => {
      u.currentTarget.focus();
    }),
    onPointerDown: R(s.onPointerDown, (u) => {
      const v = u.target;
      v.hasPointerCapture(u.pointerId) && v.releasePointerCapture(u.pointerId), u.button === 0 && u.ctrlKey === !1 && (y(), n.triggerPointerDownPosRef.current = {
        x: Math.round(u.pageX),
        y: Math.round(u.pageY)
      }, u.preventDefault());
    }),
    onKeyDown: R(s.onKeyDown, (u) => {
      const v = m.current !== "";
      !(u.ctrlKey || u.altKey || u.metaKey) && u.key.length === 1 && C(u.key), !(v && u.key === " ") && nt.includes(u.key) && (y(), u.preventDefault());
    })
  })));
}), ft = "SelectValue", ut = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, className: r, style: s, children: d, placeholder: n = "", ...t } = o, c = J(ft, e), { onValueNodeHasChildrenChange: l } = c, m = d !== void 0, C = W(a, c.onValueNodeChange);
  return Y(() => {
    l(m);
  }, [
    l,
    m
  ]), /* @__PURE__ */ p(A.span, I({}, t, {
    ref: C,
    style: {
      pointerEvents: "none"
    }
  }), Le(c.value) ? /* @__PURE__ */ p(be, null, n) : d);
}), $t = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, children: r, ...s } = o;
  return /* @__PURE__ */ p(A.span, I({
    "aria-hidden": !0
  }, s, {
    ref: a
  }), r || "▼");
}), mt = (o) => /* @__PURE__ */ p(Xe, I({
  asChild: !0
}, o)), ne = "SelectContent", vt = /* @__PURE__ */ T((o, a) => {
  const e = J(ne, o.__scopeSelect), [r, s] = P();
  if (Y(() => {
    s(new DocumentFragment());
  }, []), !e.open) {
    const d = r;
    return d ? /* @__PURE__ */ Oe(/* @__PURE__ */ p(Ne, {
      scope: o.__scopeSelect
    }, /* @__PURE__ */ p(fe.Slot, {
      scope: o.__scopeSelect
    }, /* @__PURE__ */ p("div", null, o.children))), d) : null;
  }
  return /* @__PURE__ */ p(ht, I({}, o, {
    ref: a
  }));
}), K = 10, [Ne, Q] = ce(ne), ht = /* @__PURE__ */ T((o, a) => {
  const {
    __scopeSelect: e,
    position: r = "item-aligned",
    onCloseAutoFocus: s,
    onEscapeKeyDown: d,
    onPointerDownOutside: n,
    side: t,
    sideOffset: c,
    align: l,
    alignOffset: m,
    arrowPadding: C,
    collisionBoundary: _,
    collisionPadding: y,
    sticky: u,
    hideWhenDetached: v,
    avoidCollisions: S,
    //
    ...$
  } = o, i = J(ne, e), [h, re] = P(null), [L, se] = P(null), O = W(
    a,
    (f) => re(f)
  ), [M, j] = P(null), [ee, X] = P(null), H = ue(e), [G, q] = P(!1), E = U(!1);
  Z(() => {
    if (h)
      return tt(h);
  }, [
    h
  ]), Ge();
  const k = N((f) => {
    const [b, ...D] = H().map(
      (x) => x.ref.current
    ), [w] = D.slice(-1), g = document.activeElement;
    for (const x of f)
      if (x === g || (x == null || x.scrollIntoView({
        block: "nearest"
      }), x === b && L && (L.scrollTop = 0), x === w && L && (L.scrollTop = L.scrollHeight), x == null || x.focus(), document.activeElement !== g))
        return;
  }, [
    H,
    L
  ]), F = N(
    () => k([
      M,
      h
    ]),
    [
      k,
      M,
      h
    ]
  );
  Z(() => {
    G && F();
  }, [
    G,
    F
  ]);
  const { onOpenChange: te, triggerPointerDownPosRef: z } = i;
  Z(() => {
    if (h) {
      let f = {
        x: 0,
        y: 0
      };
      const b = (w) => {
        var g, x, V, B;
        f = {
          x: Math.abs(Math.round(w.pageX) - ((g = (x = z.current) === null || x === void 0 ? void 0 : x.x) !== null && g !== void 0 ? g : 0)),
          y: Math.abs(Math.round(w.pageY) - ((V = (B = z.current) === null || B === void 0 ? void 0 : B.y) !== null && V !== void 0 ? V : 0))
        };
      }, D = (w) => {
        f.x <= 10 && f.y <= 10 ? w.preventDefault() : h.contains(w.target) || te(!1), document.removeEventListener("pointermove", b), z.current = null;
      };
      return z.current !== null && (document.addEventListener("pointermove", b), document.addEventListener("pointerup", D, {
        capture: !0,
        once: !0
      })), () => {
        document.removeEventListener("pointermove", b), document.removeEventListener("pointerup", D, {
          capture: !0
        });
      };
    }
  }, [
    h,
    te,
    z
  ]), Z(() => {
    const f = () => te(!1);
    return window.addEventListener("blur", f), window.addEventListener("resize", f), () => {
      window.removeEventListener("blur", f), window.removeEventListener("resize", f);
    };
  }, [
    te
  ]);
  const [$e, le] = Be((f) => {
    const b = H().filter(
      (g) => !g.disabled
    ), D = b.find(
      (g) => g.ref.current === document.activeElement
    ), w = He(b, f, D);
    w && setTimeout(
      () => w.ref.current.focus()
    );
  }), me = N((f, b, D) => {
    const w = !E.current && !D;
    (i.value !== void 0 && i.value === b || w) && (j(f), w && (E.current = !0));
  }, [
    i.value
  ]), ve = N(
    () => h == null ? void 0 : h.focus(),
    [
      h
    ]
  ), oe = N((f, b, D) => {
    const w = !E.current && !D;
    (i.value !== void 0 && i.value === b || w) && X(f);
  }, [
    i.value
  ]), ie = r === "popper" ? Te : gt, ae = ie === Te ? {
    side: t,
    sideOffset: c,
    align: l,
    alignOffset: m,
    arrowPadding: C,
    collisionBoundary: _,
    collisionPadding: y,
    sticky: u,
    hideWhenDetached: v,
    avoidCollisions: S
  } : {};
  return /* @__PURE__ */ p(Ne, {
    scope: e,
    content: h,
    viewport: L,
    onViewportChange: se,
    itemRefCallback: me,
    selectedItem: M,
    onItemLeave: ve,
    itemTextRefCallback: oe,
    focusSelectedItem: F,
    selectedItemText: ee,
    position: r,
    isPositioned: G,
    searchRef: $e
  }, /* @__PURE__ */ p(ot, {
    as: Ze,
    allowPinchZoom: !0
  }, /* @__PURE__ */ p(qe, {
    asChild: !0,
    trapped: i.open,
    onMountAutoFocus: (f) => {
      f.preventDefault();
    },
    onUnmountAutoFocus: R(s, (f) => {
      var b;
      (b = i.trigger) === null || b === void 0 || b.focus({
        preventScroll: !0
      }), f.preventDefault();
    })
  }, /* @__PURE__ */ p(Ue, {
    asChild: !0,
    disableOutsidePointerEvents: !0,
    onEscapeKeyDown: d,
    onPointerDownOutside: n,
    onFocusOutside: (f) => f.preventDefault(),
    onDismiss: () => i.onOpenChange(!1)
  }, /* @__PURE__ */ p(ie, I({
    role: "listbox",
    id: i.contentId,
    "data-state": i.open ? "open" : "closed",
    dir: i.dir,
    onContextMenu: (f) => f.preventDefault()
  }, $, ae, {
    onPlaced: () => q(!0),
    ref: O,
    style: {
      // flex layout so we can place the scroll buttons properly
      display: "flex",
      flexDirection: "column",
      // reset the outline by default as the content MAY get focused
      outline: "none",
      ...$.style
    },
    onKeyDown: R($.onKeyDown, (f) => {
      const b = f.ctrlKey || f.altKey || f.metaKey;
      if (f.key === "Tab" && f.preventDefault(), !b && f.key.length === 1 && le(f.key), [
        "ArrowUp",
        "ArrowDown",
        "Home",
        "End"
      ].includes(f.key)) {
        let w = H().filter(
          (g) => !g.disabled
        ).map(
          (g) => g.ref.current
        );
        if ([
          "ArrowUp",
          "End"
        ].includes(f.key) && (w = w.slice().reverse()), [
          "ArrowUp",
          "ArrowDown"
        ].includes(f.key)) {
          const g = f.target, x = w.indexOf(g);
          w = w.slice(x + 1);
        }
        setTimeout(
          () => k(w)
        ), f.preventDefault();
      }
    })
  }))))));
}), gt = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, onPlaced: r, ...s } = o, d = J(ne, e), n = Q(ne, e), [t, c] = P(null), [l, m] = P(null), C = W(
    a,
    (O) => m(O)
  ), _ = ue(e), y = U(!1), u = U(!0), { viewport: v, selectedItem: S, selectedItemText: $, focusSelectedItem: i } = n, h = N(() => {
    if (d.trigger && d.valueNode && t && l && v && S && $) {
      const O = d.trigger.getBoundingClientRect(), M = l.getBoundingClientRect(), j = d.valueNode.getBoundingClientRect(), ee = $.getBoundingClientRect();
      if (d.dir !== "rtl") {
        const g = ee.left - M.left, x = j.left - g, V = O.left - x, B = O.width + V, he = Math.max(B, M.width), ge = window.innerWidth - K, xe = ye(x, [
          K,
          ge - he
        ]);
        t.style.minWidth = B + "px", t.style.left = xe + "px";
      } else {
        const g = M.right - ee.right, x = window.innerWidth - j.right - g, V = window.innerWidth - O.right - x, B = O.width + V, he = Math.max(B, M.width), ge = window.innerWidth - K, xe = ye(x, [
          K,
          ge - he
        ]);
        t.style.minWidth = B + "px", t.style.right = xe + "px";
      }
      const X = _(), H = window.innerHeight - K * 2, G = v.scrollHeight, q = window.getComputedStyle(l), E = parseInt(q.borderTopWidth, 10), k = parseInt(q.paddingTop, 10), F = parseInt(q.borderBottomWidth, 10), te = parseInt(q.paddingBottom, 10), z = E + k + G + te + F, $e = Math.min(S.offsetHeight * 5, z), le = window.getComputedStyle(v), me = parseInt(le.paddingTop, 10), ve = parseInt(le.paddingBottom, 10), oe = O.top + O.height / 2 - K, ie = H - oe, ae = S.offsetHeight / 2, f = S.offsetTop + ae, b = E + k + f, D = z - b;
      if (b <= oe) {
        const g = S === X[X.length - 1].ref.current;
        t.style.bottom = "0px";
        const x = l.clientHeight - v.offsetTop - v.offsetHeight, V = Math.max(ie, ae + (g ? ve : 0) + x + F), B = b + V;
        t.style.height = B + "px";
      } else {
        const g = S === X[0].ref.current;
        t.style.top = "0px";
        const V = Math.max(oe, E + v.offsetTop + (g ? me : 0) + ae) + D;
        t.style.height = V + "px", v.scrollTop = b - oe + v.offsetTop;
      }
      t.style.margin = `${K}px 0`, t.style.minHeight = $e + "px", t.style.maxHeight = H + "px", r == null || r(), requestAnimationFrame(
        () => y.current = !0
      );
    }
  }, [
    _,
    d.trigger,
    d.valueNode,
    t,
    l,
    v,
    S,
    $,
    d.dir,
    r
  ]);
  Y(
    () => h(),
    [
      h
    ]
  );
  const [re, L] = P();
  Y(() => {
    l && L(window.getComputedStyle(l).zIndex);
  }, [
    l
  ]);
  const se = N((O) => {
    O && u.current === !0 && (h(), i == null || i(), u.current = !1);
  }, [
    h,
    i
  ]);
  return /* @__PURE__ */ p(xt, {
    scope: e,
    contentWrapper: t,
    shouldExpandOnScrollRef: y,
    onScrollButtonChange: se
  }, /* @__PURE__ */ p("div", {
    ref: c,
    style: {
      display: "flex",
      flexDirection: "column",
      position: "fixed",
      zIndex: re
    }
  }, /* @__PURE__ */ p(A.div, I({}, s, {
    ref: C,
    style: {
      // When we get the height of the content, it includes borders. If we were to set
      // the height without having `boxSizing: 'border-box'` it would be too big.
      boxSizing: "border-box",
      // We need to ensure the content doesn't get taller than the wrapper
      maxHeight: "100%",
      ...s.style
    }
  }))));
}), Te = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, align: r = "start", collisionPadding: s = K, ...d } = o, n = Ce(e);
  return /* @__PURE__ */ p(je, I({}, n, d, {
    ref: a,
    align: r,
    collisionPadding: s,
    style: {
      // Ensure border-box for floating-ui calculations
      boxSizing: "border-box",
      ...d.style,
      "--radix-select-content-transform-origin": "var(--radix-popper-transform-origin)",
      "--radix-select-content-available-width": "var(--radix-popper-available-width)",
      "--radix-select-content-available-height": "var(--radix-popper-available-height)",
      "--radix-select-trigger-width": "var(--radix-popper-anchor-width)",
      "--radix-select-trigger-height": "var(--radix-popper-anchor-height)"
    }
  }));
}), [xt, Ie] = ce(ne, {}), _e = "SelectViewport", St = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, ...r } = o, s = Q(_e, e), d = Ie(_e, e), n = W(a, s.onViewportChange), t = U(0);
  return /* @__PURE__ */ p(be, null, /* @__PURE__ */ p("style", {
    dangerouslySetInnerHTML: {
      __html: "[data-radix-select-viewport]{scrollbar-width:none;-ms-overflow-style:none;-webkit-overflow-scrolling:touch;}[data-radix-select-viewport]::-webkit-scrollbar{display:none}"
    }
  }), /* @__PURE__ */ p(fe.Slot, {
    scope: e
  }, /* @__PURE__ */ p(A.div, I({
    "data-radix-select-viewport": "",
    role: "presentation"
  }, r, {
    ref: n,
    style: {
      // we use position: 'relative' here on the `viewport` so that when we call
      // `selectedItem.offsetTop` in calculations, the offset is relative to the viewport
      // (independent of the scrollUpButton).
      position: "relative",
      flex: 1,
      overflow: "auto",
      ...r.style
    },
    onScroll: R(r.onScroll, (c) => {
      const l = c.currentTarget, { contentWrapper: m, shouldExpandOnScrollRef: C } = d;
      if (C != null && C.current && m) {
        const _ = Math.abs(t.current - l.scrollTop);
        if (_ > 0) {
          const y = window.innerHeight - K * 2, u = parseFloat(m.style.minHeight), v = parseFloat(m.style.height), S = Math.max(u, v);
          if (S < y) {
            const $ = S + _, i = Math.min(y, $), h = $ - i;
            m.style.height = i + "px", m.style.bottom === "0px" && (l.scrollTop = h > 0 ? h : 0, m.style.justifyContent = "flex-end");
          }
        }
      }
      t.current = l.scrollTop;
    })
  }))));
}), bt = "SelectGroup", [wt, Ct] = ce(bt), It = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, ...r } = o, s = we();
  return /* @__PURE__ */ p(wt, {
    scope: e,
    id: s
  }, /* @__PURE__ */ p(A.div, I({
    role: "group",
    "aria-labelledby": s
  }, r, {
    ref: a
  })));
}), yt = "SelectLabel", Pt = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, ...r } = o, s = Ct(yt, e);
  return /* @__PURE__ */ p(A.div, I({
    id: s.id
  }, r, {
    ref: a
  }));
}), Se = "SelectItem", [Tt, Me] = ce(Se), _t = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, value: r, disabled: s = !1, textValue: d, ...n } = o, t = J(Se, e), c = Q(Se, e), l = t.value === r, [m, C] = P(d ?? ""), [_, y] = P(!1), u = W(a, ($) => {
    var i;
    return (i = c.itemRefCallback) === null || i === void 0 ? void 0 : i.call(c, $, r, s);
  }), v = we(), S = () => {
    s || (t.onValueChange(r), t.onOpenChange(!1));
  };
  if (r === "")
    throw new Error("A <Select.Item /> must have a value prop that is not an empty string. This is because the Select value can be set to an empty string to clear the selection and show the placeholder.");
  return /* @__PURE__ */ p(Tt, {
    scope: e,
    value: r,
    disabled: s,
    textId: v,
    isSelected: l,
    onItemTextChange: N(($) => {
      C((i) => {
        var h;
        return i || ((h = $ == null ? void 0 : $.textContent) !== null && h !== void 0 ? h : "").trim();
      });
    }, [])
  }, /* @__PURE__ */ p(fe.ItemSlot, {
    scope: e,
    value: r,
    disabled: s,
    textValue: m
  }, /* @__PURE__ */ p(A.div, I({
    role: "option",
    "aria-labelledby": v,
    "data-highlighted": _ ? "" : void 0,
    "aria-selected": l && _,
    "data-state": l ? "checked" : "unchecked",
    "aria-disabled": s || void 0,
    "data-disabled": s ? "" : void 0,
    tabIndex: s ? void 0 : -1
  }, n, {
    ref: u,
    onFocus: R(
      n.onFocus,
      () => y(!0)
    ),
    onBlur: R(
      n.onBlur,
      () => y(!1)
    ),
    onPointerUp: R(n.onPointerUp, S),
    onPointerMove: R(n.onPointerMove, ($) => {
      if (s) {
        var i;
        (i = c.onItemLeave) === null || i === void 0 || i.call(c);
      } else
        $.currentTarget.focus({
          preventScroll: !0
        });
    }),
    onPointerLeave: R(n.onPointerLeave, ($) => {
      if ($.currentTarget === document.activeElement) {
        var i;
        (i = c.onItemLeave) === null || i === void 0 || i.call(c);
      }
    }),
    onKeyDown: R(n.onKeyDown, ($) => {
      var i;
      ((i = c.searchRef) === null || i === void 0 ? void 0 : i.current) !== "" && $.key === " " || (ct.includes($.key) && S(), $.key === " " && $.preventDefault());
    })
  }))));
}), de = "SelectItemText", Et = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, className: r, style: s, ...d } = o, n = J(de, e), t = Q(de, e), c = Me(de, e), l = lt(de, e), [m, C] = P(null), _ = W(
    a,
    ($) => C($),
    c.onItemTextChange,
    ($) => {
      var i;
      return (i = t.itemTextRefCallback) === null || i === void 0 ? void 0 : i.call(t, $, c.value, c.disabled);
    }
  ), y = m == null ? void 0 : m.textContent, u = ke(
    () => /* @__PURE__ */ p("option", {
      key: c.value,
      value: c.value,
      disabled: c.disabled
    }, y),
    [
      c.disabled,
      c.value,
      y
    ]
  ), { onNativeOptionAdd: v, onNativeOptionRemove: S } = l;
  return Y(() => (v(u), () => S(u)), [
    v,
    S,
    u
  ]), /* @__PURE__ */ p(be, null, /* @__PURE__ */ p(A.span, I({
    id: c.textId
  }, d, {
    ref: _
  })), c.isSelected && n.valueNode && !n.valueNodeHasChildren ? /* @__PURE__ */ Oe(d.children, n.valueNode) : null);
}), Rt = "SelectItemIndicator", Ot = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, ...r } = o;
  return Me(Rt, e).isSelected ? /* @__PURE__ */ p(A.span, I({
    "aria-hidden": !0
  }, r, {
    ref: a
  })) : null;
}), Ee = "SelectScrollUpButton", Dt = /* @__PURE__ */ T((o, a) => {
  const e = Q(Ee, o.__scopeSelect), r = Ie(Ee, o.__scopeSelect), [s, d] = P(!1), n = W(a, r.onScrollButtonChange);
  return Y(() => {
    if (e.viewport && e.isPositioned) {
      let c = function() {
        const l = t.scrollTop > 0;
        d(l);
      };
      const t = e.viewport;
      return c(), t.addEventListener("scroll", c), () => t.removeEventListener("scroll", c);
    }
  }, [
    e.viewport,
    e.isPositioned
  ]), s ? /* @__PURE__ */ p(Ae, I({}, o, {
    ref: n,
    onAutoScroll: () => {
      const { viewport: t, selectedItem: c } = e;
      t && c && (t.scrollTop = t.scrollTop - c.offsetHeight);
    }
  })) : null;
}), Re = "SelectScrollDownButton", Nt = /* @__PURE__ */ T((o, a) => {
  const e = Q(Re, o.__scopeSelect), r = Ie(Re, o.__scopeSelect), [s, d] = P(!1), n = W(a, r.onScrollButtonChange);
  return Y(() => {
    if (e.viewport && e.isPositioned) {
      let c = function() {
        const l = t.scrollHeight - t.clientHeight, m = Math.ceil(t.scrollTop) < l;
        d(m);
      };
      const t = e.viewport;
      return c(), t.addEventListener("scroll", c), () => t.removeEventListener("scroll", c);
    }
  }, [
    e.viewport,
    e.isPositioned
  ]), s ? /* @__PURE__ */ p(Ae, I({}, o, {
    ref: n,
    onAutoScroll: () => {
      const { viewport: t, selectedItem: c } = e;
      t && c && (t.scrollTop = t.scrollTop + c.offsetHeight);
    }
  })) : null;
}), Ae = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, onAutoScroll: r, ...s } = o, d = Q("SelectScrollButton", e), n = U(null), t = ue(e), c = N(() => {
    n.current !== null && (window.clearInterval(n.current), n.current = null);
  }, []);
  return Z(() => () => c(), [
    c
  ]), Y(() => {
    var l;
    const m = t().find(
      (C) => C.ref.current === document.activeElement
    );
    m == null || (l = m.ref.current) === null || l === void 0 || l.scrollIntoView({
      block: "nearest"
    });
  }, [
    t
  ]), /* @__PURE__ */ p(A.div, I({
    "aria-hidden": !0
  }, s, {
    ref: a,
    style: {
      flexShrink: 0,
      ...s.style
    },
    onPointerDown: R(s.onPointerDown, () => {
      n.current === null && (n.current = window.setInterval(r, 50));
    }),
    onPointerMove: R(s.onPointerMove, () => {
      var l;
      (l = d.onItemLeave) === null || l === void 0 || l.call(d), n.current === null && (n.current = window.setInterval(r, 50));
    }),
    onPointerLeave: R(s.onPointerLeave, () => {
      c();
    })
  }));
}), Mt = /* @__PURE__ */ T((o, a) => {
  const { __scopeSelect: e, ...r } = o;
  return /* @__PURE__ */ p(A.div, I({
    "aria-hidden": !0
  }, r, {
    ref: a
  }));
});
function Le(o) {
  return o === "" || o === void 0;
}
const Ve = /* @__PURE__ */ T((o, a) => {
  const { value: e, ...r } = o, s = U(null), d = W(a, s), n = Qe(e);
  return Z(() => {
    const t = s.current, c = window.HTMLSelectElement.prototype, m = Object.getOwnPropertyDescriptor(c, "value").set;
    if (n !== e && m) {
      const C = new Event("change", {
        bubbles: !0
      });
      m.call(t, e), t.dispatchEvent(C);
    }
  }, [
    n,
    e
  ]), /* @__PURE__ */ p(et, {
    asChild: !0
  }, /* @__PURE__ */ p("select", I({}, r, {
    ref: d,
    defaultValue: e
  })));
});
Ve.displayName = "BubbleSelect";
function Be(o) {
  const a = Je(o), e = U(""), r = U(0), s = N((n) => {
    const t = e.current + n;
    a(t), function c(l) {
      e.current = l, window.clearTimeout(r.current), l !== "" && (r.current = window.setTimeout(
        () => c(""),
        1e3
      ));
    }(t);
  }, [
    a
  ]), d = N(() => {
    e.current = "", window.clearTimeout(r.current);
  }, []);
  return Z(() => () => window.clearTimeout(r.current), []), [
    e,
    s,
    d
  ];
}
function He(o, a, e) {
  const s = a.length > 1 && Array.from(a).every(
    (l) => l === a[0]
  ) ? a[0] : a, d = e ? o.indexOf(e) : -1;
  let n = At(o, Math.max(d, 0));
  s.length === 1 && (n = n.filter(
    (l) => l !== e
  ));
  const c = n.find(
    (l) => l.textValue.toLowerCase().startsWith(s.toLowerCase())
  );
  return c !== e ? c : void 0;
}
function At(o, a) {
  return o.map(
    (e, r) => o[(a + r) % o.length]
  );
}
const ao = it, lo = pt, io = ut, po = $t, fo = mt, uo = vt, $o = St, mo = It, vo = Pt, ho = _t, go = Et, xo = Ot, So = Dt, bo = Nt, wo = Mt;
export {
  uo as Content,
  mo as Group,
  po as Icon,
  ho as Item,
  xo as ItemIndicator,
  go as ItemText,
  vo as Label,
  fo as Portal,
  ao as Root,
  bo as ScrollDownButton,
  So as ScrollUpButton,
  it as Select,
  vt as SelectContent,
  It as SelectGroup,
  $t as SelectIcon,
  _t as SelectItem,
  Ot as SelectItemIndicator,
  Et as SelectItemText,
  Pt as SelectLabel,
  mt as SelectPortal,
  Nt as SelectScrollDownButton,
  Dt as SelectScrollUpButton,
  Mt as SelectSeparator,
  pt as SelectTrigger,
  ut as SelectValue,
  St as SelectViewport,
  wo as Separator,
  lo as Trigger,
  io as Value,
  $o as Viewport,
  so as createSelectScope
};
