import a, { useMemo as b, useRef as g } from "react";
function C(n) {
  return null;
}
C.getCollectionNode = function* (e, t) {
  let { childItems: d, title: i, children: l } = e, u = e.title || e.children, o = e.textValue || (typeof u == "string" ? u : "") || e["aria-label"] || "";
  !o && !(t != null && t.suppressTextValueWarning) && console.warn("<Item> with non-plain text contents is unsupported by type to select for accessibility. Please add a `textValue` prop."), yield {
    type: "item",
    props: e,
    rendered: u,
    textValue: o,
    "aria-label": e["aria-label"],
    hasChildNodes: k(e),
    *childNodes() {
      if (d)
        for (let r of d)
          yield {
            type: "item",
            value: r
          };
      else if (i) {
        let r = [];
        a.Children.forEach(l, (c) => {
          r.push({
            type: "item",
            element: c
          });
        }), yield* r;
      }
    }
  };
};
function k(n) {
  return n.hasChildItems != null ? n.hasChildItems : !!(n.childItems || n.title && a.Children.count(n.children) > 0);
}
let S = C;
function w(n) {
  return null;
}
w.getCollectionNode = function* (e) {
  let { children: t, title: d, items: i } = e;
  yield {
    type: "section",
    hasChildNodes: !0,
    rendered: d,
    "aria-label": e["aria-label"],
    *childNodes() {
      if (typeof t == "function") {
        if (!i)
          throw new Error("props.children was a function but props.items is missing");
        for (let l of i)
          yield {
            type: "item",
            value: l,
            renderer: t
          };
      } else {
        let l = [];
        a.Children.forEach(t, (u) => {
          l.push({
            type: "item",
            element: u
          });
        }), yield* l;
      }
    }
  };
};
let F = w;
class p {
  build(e, t) {
    return this.context = t, x(
      () => this.iterateCollection(e)
    );
  }
  *iterateCollection(e) {
    let { children: t, items: d } = e;
    if (typeof t == "function") {
      if (!d)
        throw new Error("props.children was a function but props.items is missing");
      for (let i of e.items)
        yield* this.getFullNode({
          value: i
        }, {
          renderer: t
        });
    } else {
      let i = [];
      a.Children.forEach(t, (u) => {
        i.push(u);
      });
      let l = 0;
      for (let u of i) {
        let o = this.getFullNode({
          element: u,
          index: l
        }, {});
        for (let r of o)
          l++, yield r;
      }
    }
  }
  getKey(e, t, d, i) {
    if (e.key != null)
      return e.key;
    if (t.type === "cell" && t.key != null)
      return `${i}${t.key}`;
    let l = t.value;
    if (l != null) {
      var u;
      let o = (u = l.key) !== null && u !== void 0 ? u : l.id;
      if (o == null)
        throw new Error("No key found for item");
      return o;
    }
    return i ? `${i}.${t.index}` : `$.${t.index}`;
  }
  getChildState(e, t) {
    return {
      renderer: t.renderer || e.renderer
    };
  }
  *getFullNode(e, t, d, i) {
    let l = e.element;
    if (!l && e.value && t && t.renderer) {
      let r = this.cache.get(e.value);
      if (r && (!r.shouldInvalidate || !r.shouldInvalidate(this.context))) {
        r.index = e.index, r.parentKey = i ? i.key : null, yield r;
        return;
      }
      l = t.renderer(e.value);
    }
    if (a.isValidElement(l)) {
      let r = l.type;
      if (typeof r != "function" && typeof r.getCollectionNode != "function") {
        let f = typeof l.type == "function" ? l.type.name : l.type;
        throw new Error(`Unknown element <${f}> in collection.`);
      }
      let c = r.getCollectionNode(l.props, this.context), y = e.index, h = c.next();
      for (; !h.done && h.value; ) {
        let f = h.value;
        e.index = y;
        let m = f.key;
        m || (m = f.element ? null : this.getKey(l, e, t, d));
        let v = [
          ...this.getFullNode({
            ...f,
            key: m,
            index: y,
            wrapper: I(e.wrapper, f.wrapper)
          }, this.getChildState(t, f), d ? `${d}${l.key}` : l.key, i)
        ];
        for (let s of v) {
          if (s.value = f.value || e.value, s.value && this.cache.set(s.value, s), e.type && s.type !== e.type)
            throw new Error(`Unsupported type <${$(s.type)}> in <${$(i.type)}>. Only <${$(e.type)}> is supported.`);
          y++, yield s;
        }
        h = c.next(v);
      }
      return;
    }
    if (e.key == null)
      return;
    let u = this, o = {
      type: e.type,
      props: e.props,
      key: e.key,
      parentKey: i ? i.key : null,
      value: e.value,
      level: i ? i.level + 1 : 0,
      index: e.index,
      rendered: e.rendered,
      textValue: e.textValue,
      "aria-label": e["aria-label"],
      wrapper: e.wrapper,
      shouldInvalidate: e.shouldInvalidate,
      hasChildNodes: e.hasChildNodes,
      childNodes: x(function* () {
        if (!e.hasChildNodes)
          return;
        let r = 0;
        for (let c of e.childNodes()) {
          c.key != null && (c.key = `${o.key}${c.key}`), c.index = r;
          let y = u.getFullNode(c, u.getChildState(t, c), o.key, o);
          for (let h of y)
            r++, yield h;
        }
      })
    };
    yield o;
  }
  constructor() {
    this.cache = /* @__PURE__ */ new WeakMap();
  }
}
function x(n) {
  let e = [], t = null;
  return {
    *[Symbol.iterator]() {
      for (let d of e)
        yield d;
      t || (t = n());
      for (let d of t)
        e.push(d), yield d;
    }
  };
}
function I(n, e) {
  if (n && e)
    return (t) => n(e(t));
  if (n)
    return n;
  if (e)
    return e;
}
function $(n) {
  return n[0].toUpperCase() + n.slice(1);
}
function U(n, e, t, d = []) {
  let i = b(
    () => new p(),
    []
  ), l = g(null);
  return b(() => {
    let u = i.build(n, t);
    return l.current = e(u, l.current), l.current;
  }, [
    i,
    n.children,
    n.items,
    t,
    ...d
  ]);
}
export {
  S as Item,
  F as Section,
  U as useCollection
};
//# sourceMappingURL=module.es.js.map
