"use client";
import * as f from "react";
import { u as p } from "./useIsoLayoutEffect-DpJo20by.js";
import { u as R } from "./useStableCallback-CudqoENs.js";
import { a as E, N as I } from "./useRenderElement-DNB2vtF1.js";
import { a as x } from "./floating-ui.utils.dom-jr2suqy2.js";
import { u as C } from "./useBaseUiId-C8Dq0BQq.js";
import { u as g } from "./LabelableContext-Dzw1DWuj.js";
function k(b = {}) {
  const {
    id: e,
    implicit: o = !1,
    controlRef: i
  } = b, {
    controlId: c,
    registerControlId: r
  } = g(), u = C(e), a = o ? c : void 0, s = E(() => /* @__PURE__ */ Symbol("labelable-control")), l = f.useRef(!1), d = f.useRef(e != null), n = R(() => {
    !l.current || r === I || (l.current = !1, r(s.current, void 0));
  });
  return p(() => {
    if (r === I)
      return;
    let t;
    if (o) {
      const m = i?.current;
      x(m) && m.closest("label") != null ? t = e ?? null : t = a ?? u;
    } else if (e != null)
      d.current = !0, t = e;
    else if (d.current)
      t = u;
    else {
      n();
      return;
    }
    if (t === void 0) {
      n();
      return;
    }
    l.current = !0, r(s.current, t);
  }, [e, i, a, r, o, u, s, n]), f.useEffect(() => n, [n]), c ?? u;
}
export {
  k as u
};
