import { isElement as s } from "../../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { useId as m } 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 c } 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 a } 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 { useFloatingParentNodeId as u } from "../components/FloatingTree.js";
import { FloatingRootStore as g } from "../components/FloatingRootStore.js";
import { PopupTriggerMap as p } from "../../utils/popups/popupTriggerMap.js";
function O(o) {
  const {
    open: r = !1,
    onOpenChange: i,
    elements: e = {}
  } = o, f = m(), l = u() != null;
  if (process.env.NODE_ENV !== "production") {
    const n = e.reference;
    n && !s(n) && console.error("Cannot pass a virtual element to the `elements.reference` option,", "as it must be a real DOM element. Use `context.setPositionReference()`", "instead.");
  }
  const t = c(() => new g({
    open: r,
    onOpenChange: i,
    referenceElement: e.reference ?? null,
    floatingElement: e.floating ?? null,
    triggerElements: e.triggers ?? new p(),
    floatingId: f,
    nested: l,
    noEmit: o.noEmit || !1
  })).current;
  return a(() => {
    const n = {
      open: r,
      floatingId: f
    };
    e.reference !== void 0 && (n.referenceElement = e.reference, n.domReferenceElement = s(e.reference) ? e.reference : null), e.floating !== void 0 && (n.floatingElement = e.floating), t.update(n);
  }, [r, f, e.reference, e.floating, t]), t.context.onOpenChange = i, t.context.nested = l, t.context.noEmit = o.noEmit || !1, t;
}
export {
  O as useFloatingRootContext
};
