import { useMemo as d, useEffect as f } from "react";
import { useMultipleSelectionState as K, SelectionManager as g } from "../../selection/dist/module.es.js";
import { useCollection as b } from "../../collections/dist/module.es.js";
import { useControlledState as h } from "../../utils/dist/module.es.js";
var S = Symbol.iterator;
class r {
  *[S]() {
    yield* this.iterable;
  }
  get size() {
    return this.keyMap.size;
  }
  getKeys() {
    return this.keyMap.keys();
  }
  getKeyBefore(t) {
    let e = this.keyMap.get(t);
    return e ? e.prevKey : null;
  }
  getKeyAfter(t) {
    let e = this.keyMap.get(t);
    return e ? e.nextKey : null;
  }
  getFirstKey() {
    return this.firstKey;
  }
  getLastKey() {
    return this.lastKey;
  }
  getItem(t) {
    return this.keyMap.get(t);
  }
  at(t) {
    const e = [
      ...this.getKeys()
    ];
    return this.getItem(e[t]);
  }
  constructor(t) {
    this.keyMap = /* @__PURE__ */ new Map(), this.iterable = t;
    let e = (s) => {
      if (this.keyMap.set(s.key, s), s.childNodes && s.type === "section")
        for (let i of s.childNodes)
          e(i);
    };
    for (let s of t)
      e(s);
    let n, a = 0;
    for (let [s, i] of this.keyMap)
      n ? (n.nextKey = s, i.prevKey = n.key) : (this.firstKey = s, i.prevKey = void 0), i.type === "item" && (i.index = a++), n = i, n.nextKey = void 0;
    this.lastKey = n == null ? void 0 : n.key;
  }
}
function $(l) {
  let { filter: t } = l, e = K(l), n = d(
    () => l.disabledKeys ? new Set(l.disabledKeys) : /* @__PURE__ */ new Set(),
    [
      l.disabledKeys
    ]
  ), a = (c) => t ? new r(t(c)) : new r(c), s = d(
    () => ({
      suppressTextValueWarning: l.suppressTextValueWarning
    }),
    [
      l.suppressTextValueWarning
    ]
  ), i = b(l, a, s, [
    t
  ]);
  return f(() => {
    e.focusedKey != null && !i.getItem(e.focusedKey) && e.setFocusedKey(null);
  }, [
    i,
    e.focusedKey
  ]), {
    collection: i,
    disabledKeys: n,
    selectionManager: new g(i, e)
  };
}
function p(l) {
  var t;
  let [e, n] = h(l.selectedKey, (t = l.defaultSelectedKey) !== null && t !== void 0 ? t : null, l.onSelectionChange), a = d(
    () => e != null ? [
      e
    ] : [],
    [
      e
    ]
  ), { collection: s, disabledKeys: i, selectionManager: c } = $({
    ...l,
    selectionMode: "single",
    disallowEmptySelection: !0,
    allowDuplicateSelectionEvents: !0,
    selectedKeys: a,
    onSelectionChange: (u) => {
      let o = u.values().next().value;
      o === e && l.onSelectionChange && l.onSelectionChange(o), n(o);
    }
  }), y = e != null ? s.getItem(e) : null;
  return {
    collection: s,
    disabledKeys: i,
    selectionManager: c,
    selectedKey: e,
    setSelectedKey: n,
    selectedItem: y
  };
}
export {
  r as ListCollection,
  $ as useListState,
  p as useSingleSelectListState
};
//# sourceMappingURL=module.es.js.map
