import * as o from "react";
import * as V from "react-dom";
import { isNode as G } from "../../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { useId as T } 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 { useIsoLayoutEffect as _ } 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 { useStableCallback as j } 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/useStableCallback.js";
import { FocusGuard as h } from "../../utils/FocusGuard.js";
import { createChangeEventDetails as S } from "../../utils/createBaseUIEventDetails.js";
import { createAttribute as k } from "../utils/createAttribute.js";
import { useRenderElement as q } from "../../utils/useRenderElement.js";
import { ownerVisuallyHidden as A } from "../../utils/constants.js";
import { jsxs as L, jsx as b } from "react/jsx-runtime";
import { enableFocusInside as D, isOutsideEvent as C, getPreviousTabbable as B, getNextTabbable as H, disableFocusInside as J } from "../utils/tabbable.js";
import { EMPTY_OBJECT 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/empty.js";
import { focusOut as z } from "../../utils/reason-parts.js";
const O = /* @__PURE__ */ o.createContext(null);
process.env.NODE_ENV !== "production" && (O.displayName = "PortalContext");
const K = () => o.useContext(O), Q = k("portal");
function U(y = {}) {
  const {
    ref: d,
    container: l,
    componentProps: N = Y,
    elementProps: F,
    elementState: g
  } = y, m = T(), a = K(), p = a == null ? void 0 : a.portalNode, [e, f] = o.useState(null), [E, i] = o.useState(null), R = j((n) => {
    n !== null && i(n);
  }), r = o.useRef(null);
  _(() => {
    if (l === null) {
      r.current && (r.current = null, i(null), f(null));
      return;
    }
    if (m == null)
      return;
    const n = (l && (G(l) ? l : l.current)) ?? p ?? document.body;
    if (n == null) {
      r.current && (r.current = null, i(null), f(null));
      return;
    }
    r.current !== n && (r.current = n, i(null), f(n));
  }, [l, p, m]);
  const t = q("div", N, {
    ref: [d, R],
    state: g,
    props: [{
      id: m,
      [Q]: ""
    }, F]
  });
  return {
    portalNode: E,
    portalSubtree: e && t ? /* @__PURE__ */ V.createPortal(t, e) : null
  };
}
const W = /* @__PURE__ */ o.forwardRef(function(d, l) {
  const {
    children: N,
    container: F,
    className: g,
    render: m,
    renderGuards: a,
    ...p
  } = d, {
    portalNode: e,
    portalSubtree: f
  } = U({
    container: F,
    ref: l,
    componentProps: d,
    elementProps: p
  }), E = o.useRef(null), i = o.useRef(null), R = o.useRef(null), r = o.useRef(null), [t, x] = o.useState(null), n = t == null ? void 0 : t.modal, I = t == null ? void 0 : t.open, v = typeof a == "boolean" ? a : !!t && !t.modal && t.open && !!e;
  o.useEffect(() => {
    if (!e || n)
      return;
    function s(u) {
      e && u.relatedTarget && C(u) && (u.type === "focusin" ? D : J)(e);
    }
    return e.addEventListener("focusin", s, !0), e.addEventListener("focusout", s, !0), () => {
      e.removeEventListener("focusin", s, !0), e.removeEventListener("focusout", s, !0);
    };
  }, [e, n]), o.useEffect(() => {
    !e || I || D(e);
  }, [I, e]);
  const w = o.useMemo(() => ({
    beforeOutsideRef: E,
    afterOutsideRef: i,
    beforeInsideRef: R,
    afterInsideRef: r,
    portalNode: e,
    setFocusManagerState: x
  }), [e]);
  return /* @__PURE__ */ L(o.Fragment, {
    children: [f, /* @__PURE__ */ L(O.Provider, {
      value: w,
      children: [v && e && /* @__PURE__ */ b(h, {
        "data-type": "outside",
        ref: E,
        onFocus: (s) => {
          var u;
          if (C(s, e))
            (u = R.current) == null || u.focus();
          else {
            const P = t ? t.domReference : null, c = B(P);
            c == null || c.focus();
          }
        }
      }), v && e && /* @__PURE__ */ b("span", {
        "aria-owns": e.id,
        style: A
      }), e && /* @__PURE__ */ V.createPortal(N, e), v && e && /* @__PURE__ */ b(h, {
        "data-type": "outside",
        ref: i,
        onFocus: (s) => {
          var u;
          if (C(s, e))
            (u = r.current) == null || u.focus();
          else {
            const P = t ? t.domReference : null, c = H(P);
            c == null || c.focus(), t != null && t.closeOnFocusOut && (t == null || t.onOpenChange(!1, S(z, s.nativeEvent)));
          }
        }
      })]
    })]
  });
});
process.env.NODE_ENV !== "production" && (W.displayName = "FloatingPortal");
export {
  W as FloatingPortal,
  U as useFloatingPortalNode,
  K as usePortalContext
};
