import * as o from "react";
import { useFloating as P } from "../../../../../../../@floating-ui_react-dom@2.1.6_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@floating-ui/react-dom/dist/floating-ui.react-dom.js";
import { isElement as r } from "../../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { useIsoLayoutEffect as y } 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 { useFloatingTree as V } from "../components/FloatingTree.js";
import { useFloatingRootContext as v } from "./useFloatingRootContext.js";
function w(c = {}) {
  const {
    nodeId: m,
    externalTree: I
  } = c, M = v(c), t = c.rootContext || M, s = {
    reference: t.useState("referenceElement"),
    floating: t.useState("floatingElement"),
    domReference: t.useState("domReferenceElement")
  }, [d, b] = o.useState(null), i = o.useRef(null), R = V(I);
  y(() => {
    s.domReference && (i.current = s.domReference);
  }, [s.domReference]);
  const n = P({
    ...c,
    elements: {
      ...s,
      ...d && {
        reference: d
      }
    }
  }), S = o.useCallback((e) => {
    const u = r(e) ? {
      getBoundingClientRect: () => e.getBoundingClientRect(),
      getClientRects: () => e.getClientRects(),
      contextElement: e
    } : e;
    b(u), n.refs.setReference(u);
  }, [n.refs]), [g, x] = o.useState(null), [k, L] = o.useState(null);
  t.useSyncedValue("referenceElement", g), t.useSyncedValue("domReferenceElement", r(g) ? g : null), t.useSyncedValue("floatingElement", k);
  const C = o.useCallback((e) => {
    (r(e) || e === null) && (i.current = e, x(e)), (r(n.refs.reference.current) || n.refs.reference.current === null || // Don't allow setting virtual elements using the old technique back to
    // `null` to support `positionReference` + an unstable `reference`
    // callback ref.
    e !== null && !r(e)) && n.refs.setReference(e);
  }, [n.refs, x]), E = o.useCallback((e) => {
    L(e), n.refs.setFloating(e);
  }, [n.refs]), l = o.useMemo(() => ({
    ...n.refs,
    setReference: C,
    setFloating: E,
    setPositionReference: S,
    domReference: i
  }), [n.refs, C, E, S]), f = o.useMemo(() => ({
    ...n.elements,
    domReference: s.domReference
  }), [n.elements, s.domReference]), p = t.useState("open"), F = t.useState("floatingId"), a = o.useMemo(() => ({
    ...n,
    dataRef: t.context.dataRef,
    open: p,
    onOpenChange: t.setOpen,
    events: t.context.events,
    floatingId: F,
    refs: l,
    elements: f,
    nodeId: m,
    rootStore: t
  }), [n, l, f, m, t, p, F]);
  return y(() => {
    t.context.dataRef.current.floatingContext = a;
    const e = R == null ? void 0 : R.nodesRef.current.find((u) => u.id === m);
    e && (e.context = a);
  }), o.useMemo(() => ({
    ...n,
    context: a,
    refs: l,
    elements: f,
    rootStore: t
  }), [n, l, f, a, t]);
}
export {
  w as useFloating
};
