import { useId as u } 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/useId.js";
import { useRefWithInit as g } 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/useRefWithInit.js";
import { useIsoLayoutEffect as E } 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 { isElement as d } from "../../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { useFloatingParentNodeId as S } from "../components/FloatingTree.js";
import { FloatingRootStore as x } from "../components/FloatingRootStore.js";
function v(l) {
  const {
    popupStore: n,
    noEmit: m = !1,
    treatPopupAsFloatingElement: a = !1,
    onOpenChange: c
  } = l, o = u(), p = S() != null, r = n.useState("open"), t = n.useState("activeTriggerElement"), s = n.useState(a ? "popupElement" : "positionerElement"), f = n.context.triggerElements, e = g(() => new x({
    open: r,
    referenceElement: t,
    floatingElement: s,
    triggerElements: f,
    onOpenChange: c,
    floatingId: o,
    nested: p,
    noEmit: m
  })).current;
  return E(() => {
    const i = {
      open: r,
      floatingId: o,
      referenceElement: t,
      floatingElement: s
    };
    d(t) && (i.domReferenceElement = t), e.state.positionReference === e.state.referenceElement && (i.positionReference = t), e.update(i);
  }, [r, o, t, s, e]), e.context.onOpenChange = c, e.context.nested = p, e.context.noEmit = m, e;
}
export {
  v as useSyncedFloatingRootContext
};
