import R, { useRef as E, useEffect as q, useMemo as j, createContext as ge, isValidElement as Ie, cloneElement as Ae, useState as ie } from "react";
import { $ as Se, c as xe, h as Ce } from "./utils-V7nfNEif.es.js";
import { b as fe, $ as Y, a as we, d as Me } from "./mergeProps-B-cqsBhL.es.js";
import { $ as se } from "./focusWithoutScrolling-CDw3JYiq.es.js";
import { c as Be, j as Le, e as Pe, b as Re } from "./openLink-m90p5mer.es.js";
import { $ as Z } from "./useEvent-CKoH5wGJ.es.js";
import { a as Oe, b as Ue, c as de } from "./scrollIntoView-Btu7DgIo.es.js";
import { a as ze } from "./FocusScope-DI1zU4s2.es.js";
import { b as ye } from "./useFocusVisible-_CKiSck5.es.js";
import { $ as ce } from "./focusSafely-hLKtxoJI.es.js";
import { flushSync as Ve } from "react-dom";
import { $ as He } from "./context-BfHy4NgG.es.js";
import { $ as Fe } from "./usePress-UxAvyvOO.es.js";
import { $ as We } from "./useGlobalListeners-BOOfV5Wh.es.js";
import { i as Ge } from "./CollectionBuilder-CTjJUDzO.es.js";
import { $ as je } from "./useControlledState-3TGZUXux.es.js";
function pe(i, e) {
  const t = E(!0), l = E(null);
  fe(() => (t.current = !0, () => {
    t.current = !1;
  }), []), fe(() => {
    t.current ? t.current = !1 : (!l.current || e.some((n, c) => !Object.is(n, l[c]))) && i(), l.current = e;
  }, e);
}
function G(i) {
  return Be() ? i.metaKey : i.ctrlKey;
}
const qe = /* @__PURE__ */ new Set([
  "checkbox",
  "radio",
  "range",
  "color",
  "file",
  "image",
  "button",
  "submit",
  "reset"
]);
function Tt(i) {
  return i instanceof HTMLInputElement && !qe.has(i.type) || i instanceof HTMLTextAreaElement || i instanceof HTMLElement && i.isContentEditable;
}
const Ye = "react-aria-clear-focus", Ze = "react-aria-focus", Je = 500;
function Qe(i) {
  let { isDisabled: e, onLongPressStart: t, onLongPressEnd: l, onLongPress: n, threshold: c = Je, accessibilityDescription: o } = i;
  const f = E(void 0);
  let { addGlobalListener: a, removeGlobalListener: m } = We(), { pressProps: I } = Fe({
    isDisabled: e,
    onPressStart(y) {
      if (y.continuePropagation(), (y.pointerType === "mouse" || y.pointerType === "touch") && (t && t({
        ...y,
        type: "longpressstart"
      }), f.current = setTimeout(() => {
        y.target.dispatchEvent(new PointerEvent("pointercancel", {
          bubbles: !0
        })), Se(y.target).activeElement !== y.target && se(y.target), n && n({
          ...y,
          type: "longpress"
        }), f.current = void 0;
      }, c), y.pointerType === "touch")) {
        let g = (v) => {
          v.preventDefault();
        };
        a(y.target, "contextmenu", g, {
          once: !0
        }), a(window, "pointerup", () => {
          setTimeout(() => {
            m(y.target, "contextmenu", g);
          }, 30);
        }, {
          once: !0
        });
      }
    },
    onPressEnd(y) {
      f.current && clearTimeout(f.current), l && (y.pointerType === "mouse" || y.pointerType === "touch") && l({
        ...y,
        type: "longpressend"
      });
    }
  }), K = Oe(n && !e ? o : void 0);
  return {
    longPressProps: Y(I, K)
  };
}
function Ee(i) {
  let e = Ne(Se(i));
  e !== i && (e && Xe(e, i), i && Te(i, e));
}
function Xe(i, e) {
  i.dispatchEvent(new FocusEvent("blur", {
    relatedTarget: e
  })), i.dispatchEvent(new FocusEvent("focusout", {
    bubbles: !0,
    relatedTarget: e
  }));
}
function Te(i, e) {
  i.dispatchEvent(new FocusEvent("focus", {
    relatedTarget: e
  })), i.dispatchEvent(new FocusEvent("focusin", {
    bubbles: !0,
    relatedTarget: e
  }));
}
function Ne(i) {
  let e = xe(i), t = e?.getAttribute("aria-activedescendant");
  return t && i.getElementById(t) || e;
}
function ue(i) {
  return Le() ? i.altKey : i.ctrlKey;
}
function ne(i, e) {
  var t, l;
  let n = `[data-key="${CSS.escape(String(e))}"]`, c = (t = i.current) === null || t === void 0 ? void 0 : t.dataset.collection;
  return c && (n = `[data-collection="${CSS.escape(c)}"]${n}`), (l = i.current) === null || l === void 0 ? void 0 : l.querySelector(n);
}
const ke = /* @__PURE__ */ new WeakMap();
function _e(i) {
  let e = we();
  return ke.set(i, e), e;
}
function et(i) {
  return ke.get(i);
}
const tt = 1e3;
function lt(i) {
  let { keyboardDelegate: e, selectionManager: t, onTypeSelect: l } = i, n = E({
    search: "",
    timeout: void 0
  }).current, c = (o) => {
    let f = it(o.key);
    if (!(!f || o.ctrlKey || o.metaKey || !o.currentTarget.contains(o.target) || n.search.length === 0 && f === " ")) {
      if (f === " " && n.search.trim().length > 0 && (o.preventDefault(), "continuePropagation" in o || o.stopPropagation()), n.search += f, e.getKeyForSearch != null) {
        let a = e.getKeyForSearch(n.search, t.focusedKey);
        a == null && (a = e.getKeyForSearch(n.search)), a != null && (t.setFocusedKey(a), l && l(a));
      }
      clearTimeout(n.timeout), n.timeout = setTimeout(() => {
        n.search = "";
      }, tt);
    }
  };
  return {
    typeSelectProps: {
      // Using a capturing listener to catch the keydown event before
      // other hooks in order to handle the Spacebar event.
      onKeyDownCapture: e.getKeyForSearch ? c : void 0
    }
  };
}
function it(i) {
  return i.length === 1 || !/^[A-Z]/i.test(i) ? i : "";
}
function kt(i) {
  let { selectionManager: e, keyboardDelegate: t, ref: l, autoFocus: n = !1, shouldFocusWrap: c = !1, disallowEmptySelection: o = !1, disallowSelectAll: f = !1, escapeKeyBehavior: a = "clearSelection", selectOnFocus: m = e.selectionBehavior === "replace", disallowTypeAhead: I = !1, shouldUseVirtualFocus: K, allowsTabNavigation: y = !1, isVirtualized: g, scrollRef: v = l, linkBehavior: A = "action" } = i, { direction: p } = He(), x = Pe(), h = (s) => {
    var d;
    if (s.altKey && s.key === "Tab" && s.preventDefault(), !(!((d = l.current) === null || d === void 0) && d.contains(s.target))) return;
    const $ = (u, V) => {
      if (u != null) {
        if (e.isLink(u) && A === "selection" && m && !ue(s)) {
          Ve(() => {
            e.setFocusedKey(u, V);
          });
          let H = ne(l, u), ae = e.getItemProps(u);
          H && x.open(H, s, ae.href, ae.routerOptions);
          return;
        }
        if (e.setFocusedKey(u, V), e.isLink(u) && A === "override") return;
        s.shiftKey && e.selectionMode === "multiple" ? e.extendSelection(u) : m && !ue(s) && e.replaceSelection(u);
      }
    };
    switch (s.key) {
      case "ArrowDown":
        if (t.getKeyBelow) {
          var w, k, r;
          let u = e.focusedKey != null ? (w = t.getKeyBelow) === null || w === void 0 ? void 0 : w.call(t, e.focusedKey) : (k = t.getFirstKey) === null || k === void 0 ? void 0 : k.call(t);
          u == null && c && (u = (r = t.getFirstKey) === null || r === void 0 ? void 0 : r.call(t, e.focusedKey)), u != null && (s.preventDefault(), $(u));
        }
        break;
      case "ArrowUp":
        if (t.getKeyAbove) {
          var F, U, Q;
          let u = e.focusedKey != null ? (F = t.getKeyAbove) === null || F === void 0 ? void 0 : F.call(t, e.focusedKey) : (U = t.getLastKey) === null || U === void 0 ? void 0 : U.call(t);
          u == null && c && (u = (Q = t.getLastKey) === null || Q === void 0 ? void 0 : Q.call(t, e.focusedKey)), u != null && (s.preventDefault(), $(u));
        }
        break;
      case "ArrowLeft":
        if (t.getKeyLeftOf) {
          var X, N, _;
          let u = e.focusedKey != null ? (X = t.getKeyLeftOf) === null || X === void 0 ? void 0 : X.call(t, e.focusedKey) : null;
          u == null && c && (u = p === "rtl" ? (N = t.getFirstKey) === null || N === void 0 ? void 0 : N.call(t, e.focusedKey) : (_ = t.getLastKey) === null || _ === void 0 ? void 0 : _.call(t, e.focusedKey)), u != null && (s.preventDefault(), $(u, p === "rtl" ? "first" : "last"));
        }
        break;
      case "ArrowRight":
        if (t.getKeyRightOf) {
          var ee, te, le;
          let u = e.focusedKey != null ? (ee = t.getKeyRightOf) === null || ee === void 0 ? void 0 : ee.call(t, e.focusedKey) : null;
          u == null && c && (u = p === "rtl" ? (te = t.getLastKey) === null || te === void 0 ? void 0 : te.call(t, e.focusedKey) : (le = t.getFirstKey) === null || le === void 0 ? void 0 : le.call(t, e.focusedKey)), u != null && (s.preventDefault(), $(u, p === "rtl" ? "last" : "first"));
        }
        break;
      case "Home":
        if (t.getFirstKey) {
          if (e.focusedKey === null && s.shiftKey) return;
          s.preventDefault();
          let u = t.getFirstKey(e.focusedKey, G(s));
          e.setFocusedKey(u), u != null && (G(s) && s.shiftKey && e.selectionMode === "multiple" ? e.extendSelection(u) : m && e.replaceSelection(u));
        }
        break;
      case "End":
        if (t.getLastKey) {
          if (e.focusedKey === null && s.shiftKey) return;
          s.preventDefault();
          let u = t.getLastKey(e.focusedKey, G(s));
          e.setFocusedKey(u), u != null && (G(s) && s.shiftKey && e.selectionMode === "multiple" ? e.extendSelection(u) : m && e.replaceSelection(u));
        }
        break;
      case "PageDown":
        if (t.getKeyPageBelow && e.focusedKey != null) {
          let u = t.getKeyPageBelow(e.focusedKey);
          u != null && (s.preventDefault(), $(u));
        }
        break;
      case "PageUp":
        if (t.getKeyPageAbove && e.focusedKey != null) {
          let u = t.getKeyPageAbove(e.focusedKey);
          u != null && (s.preventDefault(), $(u));
        }
        break;
      case "a":
        G(s) && e.selectionMode === "multiple" && f !== !0 && (s.preventDefault(), e.selectAll());
        break;
      case "Escape":
        a === "clearSelection" && !o && e.selectedKeys.size !== 0 && (s.stopPropagation(), s.preventDefault(), e.clearSelection());
        break;
      case "Tab":
        if (!y) {
          if (s.shiftKey) l.current.focus();
          else {
            let u = ze(l.current, {
              tabbable: !0
            }), V, H;
            do
              H = u.lastChild(), H && (V = H);
            while (H);
            V && (!V.contains(document.activeElement) || document.activeElement && !Ce(document.activeElement)) && se(V);
          }
          break;
        }
    }
  }, b = E({
    top: 0,
    left: 0
  });
  Z(v, "scroll", g ? void 0 : () => {
    var s, d, $, w;
    b.current = {
      top: ($ = (s = v.current) === null || s === void 0 ? void 0 : s.scrollTop) !== null && $ !== void 0 ? $ : 0,
      left: (w = (d = v.current) === null || d === void 0 ? void 0 : d.scrollLeft) !== null && w !== void 0 ? w : 0
    };
  });
  let S = (s) => {
    if (e.isFocused) {
      s.currentTarget.contains(s.target) || e.setFocused(!1);
      return;
    }
    if (s.currentTarget.contains(s.target)) {
      if (e.setFocused(!0), e.focusedKey == null) {
        var d, $;
        let r = (U) => {
          U != null && (e.setFocusedKey(U), m && !e.isSelected(U) && e.replaceSelection(U));
        }, F = s.relatedTarget;
        var w, k;
        F && s.currentTarget.compareDocumentPosition(F) & Node.DOCUMENT_POSITION_FOLLOWING ? r((w = e.lastSelectedKey) !== null && w !== void 0 ? w : (d = t.getLastKey) === null || d === void 0 ? void 0 : d.call(t)) : r((k = e.firstSelectedKey) !== null && k !== void 0 ? k : ($ = t.getFirstKey) === null || $ === void 0 ? void 0 : $.call(t));
      } else !g && v.current && (v.current.scrollTop = b.current.top, v.current.scrollLeft = b.current.left);
      if (e.focusedKey != null && v.current) {
        let r = ne(l, e.focusedKey);
        r instanceof HTMLElement && (!r.contains(document.activeElement) && !K && se(r), ye() === "keyboard" && de(r, {
          containingElement: l.current
        }));
      }
    }
  }, B = (s) => {
    s.currentTarget.contains(s.relatedTarget) || e.setFocused(!1);
  }, D = E(!1);
  Z(l, Ze, K ? (s) => {
    let { detail: d } = s;
    s.stopPropagation(), e.setFocused(!0), d?.focusStrategy === "first" && (D.current = !0);
  } : void 0), pe(() => {
    if (D.current) {
      var s, d;
      let $ = (d = (s = t.getFirstKey) === null || s === void 0 ? void 0 : s.call(t)) !== null && d !== void 0 ? d : null;
      if ($ == null) {
        let w = xe();
        Ee(l.current), Te(w, null), e.collection.size > 0 && (D.current = !1);
      } else
        e.setFocusedKey($), D.current = !1;
    }
  }, [
    e.collection
  ]), pe(() => {
    e.collection.size > 0 && (D.current = !1);
  }, [
    e.focusedKey
  ]), Z(l, Ye, K ? (s) => {
    var d;
    s.stopPropagation(), e.setFocused(!1), !((d = s.detail) === null || d === void 0) && d.clearFocusKey && e.setFocusedKey(null);
  } : void 0);
  const M = E(n), P = E(!1);
  q(() => {
    if (M.current) {
      var s, d;
      let k = null;
      var $;
      n === "first" && (k = ($ = (s = t.getFirstKey) === null || s === void 0 ? void 0 : s.call(t)) !== null && $ !== void 0 ? $ : null);
      var w;
      n === "last" && (k = (w = (d = t.getLastKey) === null || d === void 0 ? void 0 : d.call(t)) !== null && w !== void 0 ? w : null);
      let r = e.selectedKeys;
      if (r.size) {
        for (let F of r) if (e.canSelectItem(F)) {
          k = F;
          break;
        }
      }
      e.setFocused(!0), e.setFocusedKey(k), k == null && !K && l.current && ce(l.current), e.collection.size > 0 && (M.current = !1, P.current = !0);
    }
  });
  let z = E(e.focusedKey), O = E(null);
  q(() => {
    if (e.isFocused && e.focusedKey != null && (e.focusedKey !== z.current || P.current) && v.current && l.current) {
      let s = ye(), d = ne(l, e.focusedKey);
      if (!(d instanceof HTMLElement))
        return;
      (s === "keyboard" || P.current) && (O.current && cancelAnimationFrame(O.current), O.current = requestAnimationFrame(() => {
        v.current && (Ue(v.current, d), s !== "virtual" && de(d, {
          containingElement: l.current
        }));
      }));
    }
    !K && e.isFocused && e.focusedKey == null && z.current != null && l.current && ce(l.current), z.current = e.focusedKey, P.current = !1;
  }), q(() => () => {
    O.current && cancelAnimationFrame(O.current);
  }, []), Z(l, "react-aria-focus-scope-restore", (s) => {
    s.preventDefault(), e.setFocused(!0);
  });
  let L = {
    onKeyDown: h,
    onFocus: S,
    onBlur: B,
    onMouseDown(s) {
      v.current === s.target && s.preventDefault();
    }
  }, { typeSelectProps: W } = lt({
    keyboardDelegate: t,
    selectionManager: e
  });
  I || (L = Y(W, L));
  let T;
  K || (T = e.focusedKey == null ? 0 : -1);
  let J = _e(e.collection);
  return {
    collectionProps: Y(L, {
      tabIndex: T,
      "data-collection": J
    })
  };
}
function Dt(i) {
  let { id: e, selectionManager: t, key: l, ref: n, shouldSelectOnPressUp: c, shouldUseVirtualFocus: o, focus: f, isDisabled: a, onAction: m, allowsDifferentPressOrigin: I, linkBehavior: K = "action" } = i, y = Pe();
  e = we(e);
  let g = (r) => {
    if (r.pointerType === "keyboard" && ue(r)) t.toggleSelection(l);
    else {
      if (t.selectionMode === "none") return;
      if (t.isLink(l)) {
        if (K === "selection" && n.current) {
          let F = t.getItemProps(l);
          y.open(n.current, r, F.href, F.routerOptions), t.setSelectedKeys(t.selectedKeys);
          return;
        } else if (K === "override" || K === "none") return;
      }
      t.selectionMode === "single" ? t.isSelected(l) && !t.disallowEmptySelection ? t.toggleSelection(l) : t.replaceSelection(l) : r && r.shiftKey ? t.extendSelection(l) : t.selectionBehavior === "toggle" || r && (G(r) || r.pointerType === "touch" || r.pointerType === "virtual") ? t.toggleSelection(l) : t.replaceSelection(l);
    }
  };
  q(() => {
    l === t.focusedKey && t.isFocused && (o ? Ee(n.current) : f ? f() : document.activeElement !== n.current && n.current && ce(n.current));
  }, [
    n,
    l,
    t.focusedKey,
    t.childFocusStrategy,
    t.isFocused,
    o
  ]), a = a || t.isDisabled(l);
  let v = {};
  !o && !a ? v = {
    tabIndex: l === t.focusedKey ? 0 : -1,
    onFocus(r) {
      r.target === n.current && t.setFocusedKey(l);
    }
  } : a && (v.onMouseDown = (r) => {
    r.preventDefault();
  });
  let A = t.isLink(l) && K === "override", p = m && i.UNSTABLE_itemBehavior === "action", x = t.isLink(l) && K !== "selection" && K !== "none", h = !a && t.canSelectItem(l) && !A && !p, b = (m || x) && !a, S = b && (t.selectionBehavior === "replace" ? !h : !h || t.isEmpty), B = b && h && t.selectionBehavior === "replace", D = S || B, M = E(null), P = D && h, z = E(!1), O = E(!1), L = t.getItemProps(l), W = (r) => {
    if (m) {
      var F;
      m(), (F = n.current) === null || F === void 0 || F.dispatchEvent(new CustomEvent("react-aria-item-action", {
        bubbles: !0
      }));
    }
    x && n.current && y.open(n.current, r, L.href, L.routerOptions);
  }, T = {
    ref: n
  };
  if (c ? (T.onPressStart = (r) => {
    M.current = r.pointerType, z.current = P, r.pointerType === "keyboard" && (!D || ve()) && g(r);
  }, I ? (T.onPressUp = S ? void 0 : (r) => {
    r.pointerType === "mouse" && h && g(r);
  }, T.onPress = S ? W : (r) => {
    r.pointerType !== "keyboard" && r.pointerType !== "mouse" && h && g(r);
  }) : T.onPress = (r) => {
    if (S || B && r.pointerType !== "mouse") {
      if (r.pointerType === "keyboard" && !he()) return;
      W(r);
    } else r.pointerType !== "keyboard" && h && g(r);
  }) : (T.onPressStart = (r) => {
    M.current = r.pointerType, z.current = P, O.current = S, h && (r.pointerType === "mouse" && !S || r.pointerType === "keyboard" && (!b || ve())) && g(r);
  }, T.onPress = (r) => {
    (r.pointerType === "touch" || r.pointerType === "pen" || r.pointerType === "virtual" || r.pointerType === "keyboard" && D && he() || r.pointerType === "mouse" && O.current) && (D ? W(r) : h && g(r));
  }), v["data-collection"] = et(t.collection), v["data-key"] = l, T.preventFocusOnPress = o, o && (T = Y(T, {
    onPressStart(r) {
      r.pointerType !== "touch" && (t.setFocused(!0), t.setFocusedKey(l));
    },
    onPress(r) {
      r.pointerType === "touch" && (t.setFocused(!0), t.setFocusedKey(l));
    }
  })), L)
    for (let r of [
      "onPressStart",
      "onPressEnd",
      "onPressChange",
      "onPress",
      "onPressUp",
      "onClick"
    ]) L[r] && (T[r] = Me(T[r], L[r]));
  let { pressProps: J, isPressed: s } = Fe(T), d = B ? (r) => {
    M.current === "mouse" && (r.stopPropagation(), r.preventDefault(), W(r));
  } : void 0, { longPressProps: $ } = Qe({
    isDisabled: !P,
    onLongPress(r) {
      r.pointerType === "touch" && (g(r), t.setSelectionBehavior("toggle"));
    }
  }), w = (r) => {
    M.current === "touch" && z.current && r.preventDefault();
  }, k = K !== "none" && t.isLink(l) ? (r) => {
    Re.isOpening || r.preventDefault();
  } : void 0;
  return {
    itemProps: Y(
      v,
      h || S || o && !a ? J : {},
      P ? $ : {},
      {
        onDoubleClick: d,
        onDragStartCapture: w,
        onClick: k,
        id: e
      },
      // Prevent DOM focus from moving on mouse down when using virtual focus
      o ? {
        onMouseDown: (r) => r.preventDefault()
      } : void 0
    ),
    isPressed: s,
    isSelected: t.isSelected(l),
    isFocused: t.isFocused && t.focusedKey === l,
    isDisabled: a,
    allowsSelection: h,
    hasAction: D
  };
}
function he() {
  let i = window.event;
  return i?.key === "Enter";
}
function ve() {
  let i = window.event;
  return i?.key === " " || i?.code === "Space";
}
class nt {
  build(e, t) {
    return this.context = t, be(() => this.iterateCollection(e));
  }
  *iterateCollection(e) {
    let { children: t, items: l } = e;
    if (R.isValidElement(t) && t.type === R.Fragment) yield* this.iterateCollection({
      children: t.props.children,
      items: l
    });
    else if (typeof t == "function") {
      if (!l) throw new Error("props.children was a function but props.items is missing");
      let n = 0;
      for (let c of l)
        yield* this.getFullNode({
          value: c,
          index: n
        }, {
          renderer: t
        }), n++;
    } else {
      let n = [];
      R.Children.forEach(t, (o) => {
        o && n.push(o);
      });
      let c = 0;
      for (let o of n) {
        let f = this.getFullNode({
          element: o,
          index: c
        }, {});
        for (let a of f)
          c++, yield a;
      }
    }
  }
  getKey(e, t, l, n) {
    if (e.key != null) return e.key;
    if (t.type === "cell" && t.key != null) return `${n}${t.key}`;
    let c = t.value;
    if (c != null) {
      var o;
      let f = (o = c.key) !== null && o !== void 0 ? o : c.id;
      if (f == null) throw new Error("No key found for item");
      return f;
    }
    return n ? `${n}.${t.index}` : `$.${t.index}`;
  }
  getChildState(e, t) {
    return {
      renderer: t.renderer || e.renderer
    };
  }
  *getFullNode(e, t, l, n) {
    if (R.isValidElement(e.element) && e.element.type === R.Fragment) {
      let p = [];
      R.Children.forEach(e.element.props.children, (h) => {
        p.push(h);
      });
      var c;
      let x = (c = e.index) !== null && c !== void 0 ? c : 0;
      for (const h of p) yield* this.getFullNode({
        element: h,
        index: x++
      }, t, l, n);
      return;
    }
    let o = e.element;
    if (!o && e.value && t && t.renderer) {
      let p = this.cache.get(e.value);
      if (p && (!p.shouldInvalidate || !p.shouldInvalidate(this.context))) {
        p.index = e.index, p.parentKey = n ? n.key : null, yield p;
        return;
      }
      o = t.renderer(e.value);
    }
    if (R.isValidElement(o)) {
      let p = o.type;
      if (typeof p != "function" && typeof p.getCollectionNode != "function") {
        let S = o.type;
        throw new Error(`Unknown element <${S}> in collection.`);
      }
      let x = p.getCollectionNode(o.props, this.context);
      var f;
      let h = (f = e.index) !== null && f !== void 0 ? f : 0, b = x.next();
      for (; !b.done && b.value; ) {
        let S = b.value;
        e.index = h;
        var a;
        let B = (a = S.key) !== null && a !== void 0 ? a : null;
        B == null && (B = S.element ? null : this.getKey(o, e, t, l));
        let M = [
          ...this.getFullNode({
            ...S,
            key: B,
            index: h,
            wrapper: rt(e.wrapper, S.wrapper)
          }, this.getChildState(t, S), l ? `${l}${o.key}` : o.key, n)
        ];
        for (let P of M) {
          var m, I;
          P.value = (I = (m = S.value) !== null && m !== void 0 ? m : e.value) !== null && I !== void 0 ? I : null, P.value && this.cache.set(P.value, P);
          var K;
          if (e.type && P.type !== e.type) throw new Error(`Unsupported type <${re(P.type)}> in <${re((K = n?.type) !== null && K !== void 0 ? K : "unknown parent type")}>. Only <${re(e.type)}> is supported.`);
          h++, yield P;
        }
        b = x.next(M);
      }
      return;
    }
    if (e.key == null || e.type == null) return;
    let y = this;
    var g, v;
    let A = {
      type: e.type,
      props: e.props,
      key: e.key,
      parentKey: n ? n.key : null,
      value: (g = e.value) !== null && g !== void 0 ? g : null,
      level: n ? n.level + 1 : 0,
      index: e.index,
      rendered: e.rendered,
      textValue: (v = e.textValue) !== null && v !== void 0 ? v : "",
      "aria-label": e["aria-label"],
      wrapper: e.wrapper,
      shouldInvalidate: e.shouldInvalidate,
      hasChildNodes: e.hasChildNodes || !1,
      childNodes: be(function* () {
        if (!e.hasChildNodes || !e.childNodes) return;
        let p = 0;
        for (let x of e.childNodes()) {
          x.key != null && (x.key = `${A.key}${x.key}`);
          let h = y.getFullNode({
            ...x,
            index: p
          }, y.getChildState(t, x), A.key, A);
          for (let b of h)
            p++, yield b;
        }
      })
    };
    yield A;
  }
  constructor() {
    this.cache = /* @__PURE__ */ new WeakMap();
  }
}
function be(i) {
  let e = [], t = null;
  return {
    *[Symbol.iterator]() {
      for (let l of e) yield l;
      t || (t = i());
      for (let l of t)
        e.push(l), yield l;
    }
  };
}
function rt(i, e) {
  if (i && e) return (t) => i(e(t));
  if (i) return i;
  if (e) return e;
}
function re(i) {
  return i[0].toUpperCase() + i.slice(1);
}
function It(i, e, t) {
  let l = j(() => new nt(), []), { children: n, items: c, collection: o } = i;
  return j(() => {
    if (o) return o;
    let a = l.build({
      children: n,
      items: c
    }, t);
    return e(a);
  }, [
    l,
    n,
    c,
    o,
    t,
    e
  ]);
}
function ot(i, e) {
  return typeof e.getChildren == "function" ? e.getChildren(i.key) : i.childNodes;
}
function st(i) {
  return ct(i, 0);
}
function ct(i, e) {
  if (e < 0) return;
  let t = 0;
  for (let l of i) {
    if (t === e) return l;
    t++;
  }
}
function At(i) {
  let e;
  for (let t of i) e = t;
  return e;
}
function oe(i, e, t) {
  if (e.parentKey === t.parentKey) return e.index - t.index;
  let l = [
    ...$e(i, e),
    e
  ], n = [
    ...$e(i, t),
    t
  ], c = l.slice(0, n.length).findIndex((o, f) => o !== n[f]);
  return c !== -1 ? (e = l[c], t = n[c], e.index - t.index) : l.findIndex((o) => o === t) >= 0 ? 1 : (n.findIndex((o) => o === e) >= 0, -1);
}
function $e(i, e) {
  let t = [], l = e;
  for (; l?.parentKey != null; )
    l = i.getItem(l.parentKey), l && t.unshift(l);
  return t;
}
const Ct = /* @__PURE__ */ ge(null), ut = {
  CollectionRoot({ collection: i, renderDropIndicator: e }) {
    return me(i, null, e);
  },
  CollectionBranch({ collection: i, parent: e, renderDropIndicator: t }) {
    return me(i, e, t);
  }
};
function me(i, e, t) {
  return Ge({
    items: e ? i.getChildren(e.key) : i,
    dependencies: [
      t
    ],
    children(l) {
      let n = l.render(l);
      return !t || l.type !== "item" ? n : /* @__PURE__ */ R.createElement(R.Fragment, null, t({
        type: "item",
        key: l.key,
        dropPosition: "before"
      }), n, at(i, l, t));
    }
  });
}
function at(i, e, t) {
  let l = e.key, n = i.getKeyAfter(l), c = n != null ? i.getItem(n) : null;
  for (; c != null && c.type !== "item"; )
    n = i.getKeyAfter(c.key), c = n != null ? i.getItem(n) : null;
  let o = e.nextKey != null ? i.getItem(e.nextKey) : null;
  for (; o != null && o.type !== "item"; ) o = o.nextKey != null ? i.getItem(o.nextKey) : null;
  let f = [];
  if (o == null) {
    let a = e;
    for (; a?.type === "item" && (!c || a.parentKey !== c.parentKey && c.level < a.level); ) {
      let m = t({
        type: "item",
        key: a.key,
        dropPosition: "after"
      });
      /* @__PURE__ */ Ie(m) && f.push(/* @__PURE__ */ Ae(m, {
        key: `${a.key}-after`
      })), a = a.parentKey != null ? i.getItem(a.parentKey) : null;
    }
  }
  return f;
}
const Mt = /* @__PURE__ */ ge(ut);
function Bt(i) {
  return j(() => i != null ? /* @__PURE__ */ new Set([
    i
  ]) : null, [
    i
  ]);
}
class C extends Set {
  constructor(e, t, l) {
    super(e), e instanceof C ? (this.anchorKey = t ?? e.anchorKey, this.currentKey = l ?? e.currentKey) : (this.anchorKey = t ?? null, this.currentKey = l ?? null);
  }
}
function ft(i, e) {
  if (i.size !== e.size) return !1;
  for (let t of i)
    if (!e.has(t)) return !1;
  return !0;
}
function Lt(i) {
  let { selectionMode: e = "none", disallowEmptySelection: t = !1, allowDuplicateSelectionEvents: l, selectionBehavior: n = "toggle", disabledBehavior: c = "all" } = i, o = E(!1), [, f] = ie(!1), a = E(null), m = E(null), [, I] = ie(null), K = j(() => Ke(i.selectedKeys), [
    i.selectedKeys
  ]), y = j(() => Ke(i.defaultSelectedKeys, new C()), [
    i.defaultSelectedKeys
  ]), [g, v] = je(K, y, i.onSelectionChange), A = j(() => i.disabledKeys ? new Set(i.disabledKeys) : /* @__PURE__ */ new Set(), [
    i.disabledKeys
  ]), [p, x] = ie(n);
  n === "replace" && p === "toggle" && typeof g == "object" && g.size === 0 && x("replace");
  let h = E(n);
  return q(() => {
    n !== h.current && (x(n), h.current = n);
  }, [
    n
  ]), {
    selectionMode: e,
    disallowEmptySelection: t,
    selectionBehavior: p,
    setSelectionBehavior: x,
    get isFocused() {
      return o.current;
    },
    setFocused(b) {
      o.current = b, f(b);
    },
    get focusedKey() {
      return a.current;
    },
    get childFocusStrategy() {
      return m.current;
    },
    setFocusedKey(b, S = "first") {
      a.current = b, m.current = S, I(b);
    },
    selectedKeys: g,
    setSelectedKeys(b) {
      (l || !ft(b, g)) && v(b);
    },
    disabledKeys: A,
    disabledBehavior: c
  };
}
function Ke(i, e) {
  return i ? i === "all" ? "all" : new C(i) : e;
}
class De {
  /**
  * The type of selection that is allowed in the collection.
  */
  get selectionMode() {
    return this.state.selectionMode;
  }
  /**
  * Whether the collection allows empty selection.
  */
  get disallowEmptySelection() {
    return this.state.disallowEmptySelection;
  }
  /**
  * The selection behavior for the collection.
  */
  get selectionBehavior() {
    return this.state.selectionBehavior;
  }
  /**
  * Sets the selection behavior for the collection.
  */
  setSelectionBehavior(e) {
    this.state.setSelectionBehavior(e);
  }
  /**
  * Whether the collection is currently focused.
  */
  get isFocused() {
    return this.state.isFocused;
  }
  /**
  * Sets whether the collection is focused.
  */
  setFocused(e) {
    this.state.setFocused(e);
  }
  /**
  * The current focused key in the collection.
  */
  get focusedKey() {
    return this.state.focusedKey;
  }
  /** Whether the first or last child of the focused key should receive focus. */
  get childFocusStrategy() {
    return this.state.childFocusStrategy;
  }
  /**
  * Sets the focused key.
  */
  setFocusedKey(e, t) {
    (e == null || this.collection.getItem(e)) && this.state.setFocusedKey(e, t);
  }
  /**
  * The currently selected keys in the collection.
  */
  get selectedKeys() {
    return this.state.selectedKeys === "all" ? new Set(this.getSelectAllKeys()) : this.state.selectedKeys;
  }
  /**
  * The raw selection value for the collection.
  * Either 'all' for select all, or a set of keys.
  */
  get rawSelection() {
    return this.state.selectedKeys;
  }
  /**
  * Returns whether a key is selected.
  */
  isSelected(e) {
    if (this.state.selectionMode === "none") return !1;
    let t = this.getKey(e);
    return t == null ? !1 : this.state.selectedKeys === "all" ? this.canSelectItem(t) : this.state.selectedKeys.has(t);
  }
  /**
  * Whether the selection is empty.
  */
  get isEmpty() {
    return this.state.selectedKeys !== "all" && this.state.selectedKeys.size === 0;
  }
  /**
  * Whether all items in the collection are selected.
  */
  get isSelectAll() {
    if (this.isEmpty) return !1;
    if (this.state.selectedKeys === "all") return !0;
    if (this._isSelectAll != null) return this._isSelectAll;
    let e = this.getSelectAllKeys(), t = this.state.selectedKeys;
    return this._isSelectAll = e.every((l) => t.has(l)), this._isSelectAll;
  }
  get firstSelectedKey() {
    let e = null;
    for (let l of this.state.selectedKeys) {
      let n = this.collection.getItem(l);
      (!e || n && oe(this.collection, n, e) < 0) && (e = n);
    }
    var t;
    return (t = e?.key) !== null && t !== void 0 ? t : null;
  }
  get lastSelectedKey() {
    let e = null;
    for (let l of this.state.selectedKeys) {
      let n = this.collection.getItem(l);
      (!e || n && oe(this.collection, n, e) > 0) && (e = n);
    }
    var t;
    return (t = e?.key) !== null && t !== void 0 ? t : null;
  }
  get disabledKeys() {
    return this.state.disabledKeys;
  }
  get disabledBehavior() {
    return this.state.disabledBehavior;
  }
  /**
  * Extends the selection to the given key.
  */
  extendSelection(e) {
    if (this.selectionMode === "none") return;
    if (this.selectionMode === "single") {
      this.replaceSelection(e);
      return;
    }
    let t = this.getKey(e);
    if (t == null) return;
    let l;
    if (this.state.selectedKeys === "all") l = new C([
      t
    ], t, t);
    else {
      let o = this.state.selectedKeys;
      var n;
      let f = (n = o.anchorKey) !== null && n !== void 0 ? n : t;
      l = new C(o, f, t);
      var c;
      for (let a of this.getKeyRange(f, (c = o.currentKey) !== null && c !== void 0 ? c : t)) l.delete(a);
      for (let a of this.getKeyRange(t, f)) this.canSelectItem(a) && l.add(a);
    }
    this.state.setSelectedKeys(l);
  }
  getKeyRange(e, t) {
    let l = this.collection.getItem(e), n = this.collection.getItem(t);
    return l && n ? oe(this.collection, l, n) <= 0 ? this.getKeyRangeInternal(e, t) : this.getKeyRangeInternal(t, e) : [];
  }
  getKeyRangeInternal(e, t) {
    var l;
    if (!((l = this.layoutDelegate) === null || l === void 0) && l.getKeyRange) return this.layoutDelegate.getKeyRange(e, t);
    let n = [], c = e;
    for (; c != null; ) {
      let o = this.collection.getItem(c);
      if (o && (o.type === "item" || o.type === "cell" && this.allowsCellSelection) && n.push(c), c === t) return n;
      c = this.collection.getKeyAfter(c);
    }
    return [];
  }
  getKey(e) {
    let t = this.collection.getItem(e);
    if (!t || t.type === "cell" && this.allowsCellSelection) return e;
    for (; t && t.type !== "item" && t.parentKey != null; ) t = this.collection.getItem(t.parentKey);
    return !t || t.type !== "item" ? null : t.key;
  }
  /**
  * Toggles whether the given key is selected.
  */
  toggleSelection(e) {
    if (this.selectionMode === "none") return;
    if (this.selectionMode === "single" && !this.isSelected(e)) {
      this.replaceSelection(e);
      return;
    }
    let t = this.getKey(e);
    if (t == null) return;
    let l = new C(this.state.selectedKeys === "all" ? this.getSelectAllKeys() : this.state.selectedKeys);
    l.has(t) ? l.delete(t) : this.canSelectItem(t) && (l.add(t), l.anchorKey = t, l.currentKey = t), !(this.disallowEmptySelection && l.size === 0) && this.state.setSelectedKeys(l);
  }
  /**
  * Replaces the selection with only the given key.
  */
  replaceSelection(e) {
    if (this.selectionMode === "none") return;
    let t = this.getKey(e);
    if (t == null) return;
    let l = this.canSelectItem(t) ? new C([
      t
    ], t, t) : new C();
    this.state.setSelectedKeys(l);
  }
  /**
  * Replaces the selection with the given keys.
  */
  setSelectedKeys(e) {
    if (this.selectionMode === "none") return;
    let t = new C();
    for (let l of e) {
      let n = this.getKey(l);
      if (n != null && (t.add(n), this.selectionMode === "single"))
        break;
    }
    this.state.setSelectedKeys(t);
  }
  getSelectAllKeys() {
    let e = [], t = (l) => {
      for (; l != null; ) {
        if (this.canSelectItem(l)) {
          var n;
          let o = this.collection.getItem(l);
          o?.type === "item" && e.push(l);
          var c;
          o?.hasChildNodes && (this.allowsCellSelection || o.type !== "item") && t((c = (n = st(ot(o, this.collection))) === null || n === void 0 ? void 0 : n.key) !== null && c !== void 0 ? c : null);
        }
        l = this.collection.getKeyAfter(l);
      }
    };
    return t(this.collection.getFirstKey()), e;
  }
  /**
  * Selects all items in the collection.
  */
  selectAll() {
    !this.isSelectAll && this.selectionMode === "multiple" && this.state.setSelectedKeys("all");
  }
  /**
  * Removes all keys from the selection.
  */
  clearSelection() {
    !this.disallowEmptySelection && (this.state.selectedKeys === "all" || this.state.selectedKeys.size > 0) && this.state.setSelectedKeys(new C());
  }
  /**
  * Toggles between select all and an empty selection.
  */
  toggleSelectAll() {
    this.isSelectAll ? this.clearSelection() : this.selectAll();
  }
  select(e, t) {
    this.selectionMode !== "none" && (this.selectionMode === "single" ? this.isSelected(e) && !this.disallowEmptySelection ? this.toggleSelection(e) : this.replaceSelection(e) : this.selectionBehavior === "toggle" || t && (t.pointerType === "touch" || t.pointerType === "virtual") ? this.toggleSelection(e) : this.replaceSelection(e));
  }
  /**
  * Returns whether the current selection is equal to the given selection.
  */
  isSelectionEqual(e) {
    if (e === this.state.selectedKeys) return !0;
    let t = this.selectedKeys;
    if (e.size !== t.size) return !1;
    for (let l of e)
      if (!t.has(l)) return !1;
    for (let l of t)
      if (!e.has(l)) return !1;
    return !0;
  }
  canSelectItem(e) {
    var t;
    if (this.state.selectionMode === "none" || this.state.disabledKeys.has(e)) return !1;
    let l = this.collection.getItem(e);
    return !(!l || !(l == null || (t = l.props) === null || t === void 0) && t.isDisabled || l.type === "cell" && !this.allowsCellSelection);
  }
  isDisabled(e) {
    var t, l;
    return this.state.disabledBehavior === "all" && (this.state.disabledKeys.has(e) || !!(!((l = this.collection.getItem(e)) === null || l === void 0 || (t = l.props) === null || t === void 0) && t.isDisabled));
  }
  isLink(e) {
    var t, l;
    return !!(!((l = this.collection.getItem(e)) === null || l === void 0 || (t = l.props) === null || t === void 0) && t.href);
  }
  getItemProps(e) {
    var t;
    return (t = this.collection.getItem(e)) === null || t === void 0 ? void 0 : t.props;
  }
  withCollection(e) {
    return new De(e, this.state, {
      allowsCellSelection: this.allowsCellSelection,
      layoutDelegate: this.layoutDelegate || void 0
    });
  }
  constructor(e, t, l) {
    this.collection = e, this.state = t;
    var n;
    this.allowsCellSelection = (n = l?.allowsCellSelection) !== null && n !== void 0 ? n : !1, this._isSelectAll = null, this.layoutDelegate = l?.layoutDelegate || null;
  }
}
export {
  Lt as $,
  It as a,
  De as b,
  Tt as c,
  ne as d,
  kt as e,
  ot as f,
  Dt as g,
  Mt as h,
  Ct as i,
  ut as j,
  Bt as k,
  lt as l,
  At as m,
  st as n,
  ct as o,
  Qe as p
};
//# sourceMappingURL=SelectionManager-GNsyMc-q.es.js.map
