import * as s from "react";
import { useIsoLayoutEffect as L } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useIsoLayoutEffect.js";
import { useCompositeListContext as I } from "./CompositeListContext.js";
let O = /* @__PURE__ */ (function(n) {
  return n[n.None = 0] = "None", n[n.GuessFromOrder = 1] = "GuessFromOrder", n;
})({});
function k(n = {}) {
  const {
    label: f,
    metadata: m,
    textRef: o,
    indexGuessBehavior: F,
    index: r
  } = n, {
    register: a,
    unregister: x,
    subscribeMapChange: p,
    elementsRef: C,
    labelsRef: l,
    nextIndexRef: R
  } = I(), d = s.useRef(-1), [t, b] = s.useState(r ?? (F === O.GuessFromOrder ? () => {
    if (d.current === -1) {
      const e = R.current;
      R.current += 1, d.current = e;
    }
    return d.current;
  } : -1)), c = s.useRef(null), g = s.useCallback((e) => {
    var u;
    if (c.current = e, t !== -1 && e !== null && (C.current[t] = e, l)) {
      const i = f !== void 0;
      l.current[t] = i ? f : ((u = o == null ? void 0 : o.current) == null ? void 0 : u.textContent) ?? e.textContent;
    }
  }, [t, C, l, f, o]);
  return L(() => {
    if (r != null)
      return;
    const e = c.current;
    if (e)
      return a(e, m), () => {
        x(e);
      };
  }, [r, a, x, m]), L(() => {
    if (r == null)
      return p((e) => {
        var i;
        const u = c.current ? (i = e.get(c.current)) == null ? void 0 : i.index : null;
        u != null && b(u);
      });
  }, [r, p, b]), s.useMemo(() => ({
    ref: g,
    index: t
  }), [t, g]);
}
export {
  O as IndexGuessBehavior,
  k as useCompositeListItem
};
