"use client";
import * as n from "react";
import { u as g } from "./useIsoLayoutEffect-DpJo20by.js";
const L = /* @__PURE__ */ n.createContext({
  register: () => {
  },
  unregister: () => {
  },
  subscribeMapChange: () => () => {
  },
  elementsRef: {
    current: []
  },
  nextIndexRef: {
    current: 0
  }
});
process.env.NODE_ENV !== "production" && (L.displayName = "CompositeListContext");
function N() {
  return n.useContext(L);
}
let O = /* @__PURE__ */ (function(t) {
  return t[t.None = 0] = "None", t[t.GuessFromOrder = 1] = "GuessFromOrder", t;
})({});
function F(t = {}) {
  const {
    label: i,
    metadata: l,
    textRef: a,
    indexGuessBehavior: I,
    index: u
  } = t, {
    register: d,
    unregister: x,
    subscribeMapChange: m,
    elementsRef: C,
    labelsRef: c,
    nextIndexRef: p
  } = N(), f = n.useRef(-1), [r, R] = n.useState(u ?? (I === O.GuessFromOrder ? () => {
    if (f.current === -1) {
      const e = p.current;
      p.current += 1, f.current = e;
    }
    return f.current;
  } : -1)), s = n.useRef(null), b = n.useCallback((e) => {
    if (s.current = e, r !== -1 && e !== null && (C.current[r] = e, c)) {
      const o = i !== void 0;
      c.current[r] = o ? i : a?.current?.textContent ?? e.textContent;
    }
  }, [r, C, c, i, a]);
  return g(() => {
    if (u != null)
      return;
    const e = s.current;
    if (e)
      return d(e, l), () => {
        x(e);
      };
  }, [u, d, x, l]), g(() => {
    if (u == null)
      return m((e) => {
        const o = s.current ? e.get(s.current)?.index : null;
        o != null && R(o);
      });
  }, [u, m, R]), n.useMemo(() => ({
    ref: b,
    index: r
  }), [r, b]);
}
export {
  L as C,
  O as I,
  F as u
};
