import { version as x, useMemo as K, useCallback as m, useRef as M, useEffect as w } from "react";
import { $ as S, a as z, b as C } from "./SelectionManager-CX9btso_.es.js";
function T(i) {
  const t = x.split(".");
  return parseInt(t[0], 10) >= 19 ? i : i ? "true" : void 0;
}
class $ {
  *[Symbol.iterator]() {
    yield* this.iterable;
  }
  get size() {
    return this._size;
  }
  getKeys() {
    return this.keyMap.keys();
  }
  getKeyBefore(t) {
    let e = this.keyMap.get(t);
    var l;
    return e && (l = e.prevKey) !== null && l !== void 0 ? l : null;
  }
  getKeyAfter(t) {
    let e = this.keyMap.get(t);
    var l;
    return e && (l = e.nextKey) !== null && l !== void 0 ? l : null;
  }
  getFirstKey() {
    return this.firstKey;
  }
  getLastKey() {
    return this.lastKey;
  }
  getItem(t) {
    var e;
    return (e = this.keyMap.get(t)) !== null && e !== void 0 ? e : null;
  }
  at(t) {
    const e = [
      ...this.getKeys()
    ];
    return this.getItem(e[t]);
  }
  getChildren(t) {
    let e = this.keyMap.get(t);
    return e?.childNodes || [];
  }
  constructor(t) {
    this.keyMap = /* @__PURE__ */ new Map(), this.firstKey = null, this.lastKey = null, this.iterable = t;
    let e = (n) => {
      if (this.keyMap.set(n.key, n), n.childNodes && n.type === "section") for (let o of n.childNodes) e(o);
    };
    for (let n of t) e(n);
    let l = null, c = 0, a = 0;
    for (let [n, o] of this.keyMap)
      l ? (l.nextKey = n, o.prevKey = l.key) : (this.firstKey = n, o.prevKey = void 0), o.type === "item" && (o.index = c++), (o.type === "section" || o.type === "item") && a++, l = o, l.nextKey = void 0;
    this._size = a;
    var d;
    this.lastKey = (d = l?.key) !== null && d !== void 0 ? d : null;
  }
}
function V(i) {
  let { filter: t, layoutDelegate: e } = i, l = S(i), c = K(() => i.disabledKeys ? new Set(i.disabledKeys) : /* @__PURE__ */ new Set(), [
    i.disabledKeys
  ]), a = m((v) => t ? new $(t(v)) : new $(v), [
    t
  ]), d = K(() => ({
    suppressTextValueWarning: i.suppressTextValueWarning
  }), [
    i.suppressTextValueWarning
  ]), n = z(i, a, d), o = K(() => new C(n, l, {
    layoutDelegate: e
  }), [
    n,
    l,
    e
  ]);
  return k(n, o), {
    collection: n,
    disabledKeys: c,
    selectionManager: o
  };
}
function W(i, t) {
  let e = K(() => t ? i.collection.filter(t) : i.collection, [
    i.collection,
    t
  ]), l = i.selectionManager.withCollection(e);
  return k(e, l), {
    collection: e,
    selectionManager: l,
    disabledKeys: i.disabledKeys
  };
}
function k(i, t) {
  const e = M(null);
  w(() => {
    if (t.focusedKey != null && !i.getItem(t.focusedKey) && e.current) {
      const s = e.current.getItem(t.focusedKey), h = [
        ...e.current.getKeys()
      ].map((f) => {
        const r = e.current.getItem(f);
        return r?.type === "item" ? r : null;
      }).filter((f) => f !== null), u = [
        ...i.getKeys()
      ].map((f) => {
        const r = i.getItem(f);
        return r?.type === "item" ? r : null;
      }).filter((f) => f !== null);
      var l, c;
      const p = ((l = h?.length) !== null && l !== void 0 ? l : 0) - ((c = u?.length) !== null && c !== void 0 ? c : 0);
      var a, d, n;
      let y = Math.min(p > 1 ? Math.max(((a = s?.index) !== null && a !== void 0 ? a : 0) - p + 1, 0) : (d = s?.index) !== null && d !== void 0 ? d : 0, ((n = u?.length) !== null && n !== void 0 ? n : 0) - 1), b = null, g = !1;
      for (; y >= 0; ) {
        if (!t.isDisabled(u[y].key)) {
          b = u[y];
          break;
        }
        if (y < u.length - 1 && !g) y++;
        else {
          g = !0;
          var o, v;
          y > ((o = s?.index) !== null && o !== void 0 ? o : 0) && (y = (v = s?.index) !== null && v !== void 0 ? v : 0), y--;
        }
      }
      t.setFocusedKey(b ? b.key : null);
    }
    e.current = i;
  }, [
    i,
    t
  ]);
}
export {
  W as $,
  V as a,
  T as b
};
//# sourceMappingURL=useListState-Dd0Mtev3.es.js.map
