import { o as S } from "./rolldown-runtime.js";
import { i as v, t as D } from "./vendor/react.js";
import { u as T } from "./useRenderElement.js";
import { n as d, t as f } from "./useIsoLayoutEffect.js";
import { r as P } from "./useCompositeListItem.js";
var m = /* @__PURE__ */ S(v()), x = D();
function A(N) {
  const { children: M, elementsRef: t, labelsRef: n, onMapChange: g } = N, h = d(g), O = m.useRef(0), p = T(z).current, a = T(R).current, [u, _] = m.useState(0), i = m.useRef(u), E = d((e, o) => {
    a.set(e, o ?? null), i.current += 1, _(i.current);
  }), C = d((e) => {
    a.delete(e), i.current += 1, _(i.current);
  }), r = m.useMemo(() => {
    const e = /* @__PURE__ */ new Map();
    return Array.from(a.keys()).filter((o) => o.isConnected).sort(L).forEach((o, s) => {
      const l = a.get(o) ?? {};
      e.set(o, {
        ...l,
        index: s
      });
    }), e;
  }, [a, u]);
  f(() => {
    if (typeof MutationObserver != "function" || r.size === 0) return;
    const e = new MutationObserver((o) => {
      const s = /* @__PURE__ */ new Set(), l = (c) => s.has(c) ? s.delete(c) : s.add(c);
      o.forEach((c) => {
        c.removedNodes.forEach(l), c.addedNodes.forEach(l);
      }), s.size === 0 && (i.current += 1, _(i.current));
    });
    return r.forEach((o, s) => {
      s.parentElement && e.observe(s.parentElement, { childList: !0 });
    }), () => {
      e.disconnect();
    };
  }, [r]), f(() => {
    i.current === u && (t.current.length !== r.size && (t.current.length = r.size), n && n.current.length !== r.size && (n.current.length = r.size), O.current = r.size), h(r);
  }, [
    h,
    r,
    t,
    n,
    u
  ]), f(() => () => {
    t.current = [];
  }, [t]), f(() => () => {
    n && (n.current = []);
  }, [n]);
  const I = d((e) => (p.add(e), () => {
    p.delete(e);
  }));
  f(() => {
    p.forEach((e) => e(r));
  }, [p, r]);
  const b = m.useMemo(() => ({
    register: E,
    unregister: C,
    subscribeMapChange: I,
    elementsRef: t,
    labelsRef: n,
    nextIndexRef: O
  }), [
    E,
    C,
    I,
    t,
    n,
    O
  ]);
  return /* @__PURE__ */ (0, x.jsx)(P.Provider, {
    value: b,
    children: M
  });
}
function R() {
  return /* @__PURE__ */ new Map();
}
function z() {
  return /* @__PURE__ */ new Set();
}
function L(N, M) {
  const t = N.compareDocumentPosition(M);
  return t & Node.DOCUMENT_POSITION_FOLLOWING || t & Node.DOCUMENT_POSITION_CONTAINED_BY ? -1 : t & Node.DOCUMENT_POSITION_PRECEDING || t & Node.DOCUMENT_POSITION_CONTAINS ? 1 : 0;
}
export {
  A as t
};

//# sourceMappingURL=CompositeList.js.map