import { o as b } from "./rolldown-runtime.js";
import { i as R } from "./vendor/react.js";
import { f as E } from "./safeReact.js";
import { i as p, u as v } from "./useRenderElement.js";
import { n as x, t as C } from "./useIsoLayoutEffect.js";
import { t as S } from "./useBaseUiId.js";
import { r as _ } from "./useRegisterFieldControl.js";
var i = /* @__PURE__ */ b(R());
function B(I = {}) {
  const { id: e, implicit: l = !1, controlRef: c } = I, { controlId: f, registerControlId: r } = _(), s = S(e), a = l ? f : void 0, n = v(() => /* @__PURE__ */ Symbol("labelable-control")), u = i.useRef(!1), m = i.useRef(e != null), o = x(() => {
    !u.current || r === p || (u.current = !1, r(n.current, void 0));
  });
  return C(() => {
    if (r === p) return;
    let t;
    if (l) {
      const d = c?.current;
      E(d) && d.closest("label") != null ? t = e ?? null : t = a ?? s;
    } else if (e != null)
      m.current = !0, t = e;
    else if (m.current) t = s;
    else {
      o();
      return;
    }
    if (t === void 0) {
      o();
      return;
    }
    u.current = !0, r(n.current, t);
  }, [
    e,
    c,
    a,
    r,
    l,
    s,
    n,
    o
  ]), i.useEffect(() => o, [o]), f ?? s;
}
export {
  B as t
};

//# sourceMappingURL=useLabelableId.js.map