import { useRef as a, useState as h, useMemo as u, useEffect as F } from "react";
import { useControlledState as A } from "../../utils/dist/module.es.js";
class n extends Set {
  constructor(e, t, l) {
    super(e), e instanceof n ? (this.anchorKey = t || e.anchorKey, this.currentKey = l || e.currentKey) : (this.anchorKey = t, this.currentKey = l);
  }
}
function $(i, e) {
  if (i.size !== e.size)
    return !1;
  for (let t of i)
    if (!e.has(t))
      return !1;
  return !0;
}
function R(i) {
  let { selectionMode: e = "none", disallowEmptySelection: t, allowDuplicateSelectionEvents: l, selectionBehavior: s = "toggle", disabledBehavior: c = "all" } = i, f = a(!1), [, v] = h(!1), y = a(null), S = a(null), [, w] = h(null), p = u(
    () => m(i.selectedKeys),
    [
      i.selectedKeys
    ]
  ), b = u(
    () => m(i.defaultSelectedKeys, new n()),
    [
      i.defaultSelectedKeys
    ]
  ), [o, I] = A(p, b, i.onSelectionChange), M = u(
    () => i.disabledKeys ? new Set(i.disabledKeys) : /* @__PURE__ */ new Set(),
    [
      i.disabledKeys
    ]
  ), [K, d] = h(s);
  s === "replace" && K === "toggle" && typeof o == "object" && o.size === 0 && d("replace");
  let g = a(s);
  return F(() => {
    s !== g.current && (d(s), g.current = s);
  }, [
    s
  ]), {
    selectionMode: e,
    disallowEmptySelection: t,
    selectionBehavior: K,
    setSelectionBehavior: d,
    get isFocused() {
      return f.current;
    },
    setFocused(r) {
      f.current = r, v(r);
    },
    get focusedKey() {
      return y.current;
    },
    get childFocusStrategy() {
      return S.current;
    },
    setFocusedKey(r, B = "first") {
      y.current = r, S.current = B, w(r);
    },
    selectedKeys: o,
    setSelectedKeys(r) {
      (l || !$(r, o)) && I(r);
    },
    disabledKeys: M,
    disabledBehavior: c
  };
}
function m(i, e) {
  return i ? i === "all" ? "all" : new n(i) : e;
}
class C {
  get selectionMode() {
    return this.state.selectionMode;
  }
  get disallowEmptySelection() {
    return this.state.disallowEmptySelection;
  }
  get selectionBehavior() {
    return this.state.selectionBehavior;
  }
  setSelectionBehavior(e) {
    this.state.setSelectionBehavior(e);
  }
  get isFocused() {
    return this.state.isFocused;
  }
  setFocused(e) {
    this.state.setFocused(e);
  }
  get focusedKey() {
    return this.state.focusedKey;
  }
  get childFocusStrategy() {
    return this.state.childFocusStrategy;
  }
  setFocusedKey(e, t) {
    (e == null || this.collection.getItem(e)) && this.state.setFocusedKey(e, t);
  }
  get selectedKeys() {
    return this.state.selectedKeys === "all" ? new Set(this.getSelectAllKeys()) : this.state.selectedKeys;
  }
  get rawSelection() {
    return this.state.selectedKeys;
  }
  isSelected(e) {
    return this.state.selectionMode === "none" ? !1 : (e = this.getKey(e), this.state.selectedKeys === "all" ? this.canSelectItem(e) : this.state.selectedKeys.has(e));
  }
  get isEmpty() {
    return this.state.selectedKeys !== "all" && this.state.selectedKeys.size === 0;
  }
  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 t of this.state.selectedKeys) {
      let l = this.collection.getItem(t);
      (!e || (l == null ? void 0 : l.index) < e.index) && (e = l);
    }
    return e == null ? void 0 : e.key;
  }
  get lastSelectedKey() {
    let e = null;
    for (let t of this.state.selectedKeys) {
      let l = this.collection.getItem(t);
      (!e || (l == null ? void 0 : l.index) > e.index) && (e = l);
    }
    return e == null ? void 0 : e.key;
  }
  get disabledKeys() {
    return this.state.disabledKeys;
  }
  get disabledBehavior() {
    return this.state.disabledBehavior;
  }
  extendSelection(e) {
    if (this.selectionMode === "none")
      return;
    if (this.selectionMode === "single") {
      this.replaceSelection(e);
      return;
    }
    e = this.getKey(e);
    let t;
    if (this.state.selectedKeys === "all")
      t = new n([
        e
      ], e, e);
    else {
      let l = this.state.selectedKeys, s = l.anchorKey || e;
      t = new n(l, s, e);
      for (let c of this.getKeyRange(s, l.currentKey || e))
        t.delete(c);
      for (let c of this.getKeyRange(e, s))
        this.canSelectItem(c) && t.add(c);
    }
    this.state.setSelectedKeys(t);
  }
  getKeyRange(e, t) {
    let l = this.collection.getItem(e), s = this.collection.getItem(t);
    return l && s ? l.index <= s.index ? this.getKeyRangeInternal(e, t) : this.getKeyRangeInternal(t, e) : [];
  }
  getKeyRangeInternal(e, t) {
    let l = [], s = e;
    for (; s; ) {
      let c = this.collection.getItem(s);
      if ((c && c.type === "item" || c.type === "cell" && this.allowsCellSelection) && l.push(s), s === t)
        return l;
      s = this.collection.getKeyAfter(s);
    }
    return [];
  }
  getKey(e) {
    let t = this.collection.getItem(e);
    if (!t || t.type === "cell" && this.allowsCellSelection)
      return e;
    for (; t.type !== "item" && t.parentKey != null; )
      t = this.collection.getItem(t.parentKey);
    return !t || t.type !== "item" ? null : t.key;
  }
  toggleSelection(e) {
    if (this.selectionMode === "none")
      return;
    if (this.selectionMode === "single" && !this.isSelected(e)) {
      this.replaceSelection(e);
      return;
    }
    if (e = this.getKey(e), e == null)
      return;
    let t = new n(this.state.selectedKeys === "all" ? this.getSelectAllKeys() : this.state.selectedKeys);
    t.has(e) ? t.delete(e) : this.canSelectItem(e) && (t.add(e), t.anchorKey = e, t.currentKey = e), !(this.disallowEmptySelection && t.size === 0) && this.state.setSelectedKeys(t);
  }
  replaceSelection(e) {
    if (this.selectionMode === "none" || (e = this.getKey(e), e == null))
      return;
    let t = this.canSelectItem(e) ? new n([
      e
    ], e, e) : new n();
    this.state.setSelectedKeys(t);
  }
  setSelectedKeys(e) {
    if (this.selectionMode === "none")
      return;
    let t = new n();
    for (let l of e)
      if (l = this.getKey(l), l != null && (t.add(l), this.selectionMode === "single"))
        break;
    this.state.setSelectedKeys(t);
  }
  getSelectAllKeys() {
    let e = [], t = (l) => {
      for (; l; ) {
        if (this.canSelectItem(l)) {
          let s = this.collection.getItem(l);
          s.type === "item" && e.push(l), s.hasChildNodes && (this.allowsCellSelection || s.type !== "item") && t([
            ...s.childNodes
          ][0].key);
        }
        l = this.collection.getKeyAfter(l);
      }
    };
    return t(this.collection.getFirstKey()), e;
  }
  selectAll() {
    this.selectionMode === "multiple" && this.state.setSelectedKeys("all");
  }
  clearSelection() {
    !this.disallowEmptySelection && (this.state.selectedKeys === "all" || this.state.selectedKeys.size > 0) && this.state.setSelectedKeys(new n());
  }
  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));
  }
  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) {
    if (this.state.selectionMode === "none" || this.state.disabledKeys.has(e))
      return !1;
    let t = this.collection.getItem(e);
    return !(!t || t.type === "cell" && !this.allowsCellSelection);
  }
  isDisabled(e) {
    return this.state.disabledKeys.has(e) && this.state.disabledBehavior === "all";
  }
  constructor(e, t, l) {
    this.collection = e, this.state = t;
    var s;
    this.allowsCellSelection = (s = l == null ? void 0 : l.allowsCellSelection) !== null && s !== void 0 ? s : !1, this._isSelectAll = null;
  }
}
export {
  C as SelectionManager,
  R as useMultipleSelectionState
};
//# sourceMappingURL=module.es.js.map
