import c from "../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import * as o from "react";
import { r as L } from "../../../react-dom/index.mjs";
import i from "../../../rc-util/es/Dom/canUseDom.mjs";
import { warningOnce as q } from "../../../rc-util/es/warning.mjs";
import { supportRef as I, useComposeRef as V } from "../../../rc-util/es/ref.mjs";
import z from "./Context.mjs";
import A from "./useDom.mjs";
import M from "./useScrollLocker.mjs";
import { inlineMock as T } from "./mock.mjs";
var E = function(e) {
  return e === !1 ? !1 : !i() || !e ? null : typeof e == "string" ? document.querySelector(e) : typeof e == "function" ? e() : e;
}, U = /* @__PURE__ */ o.forwardRef(function(r, e) {
  var t = r.open, _ = r.autoLock, s = r.getContainer, P = r.debug, d = r.autoDestroy, m = d === void 0 ? !0 : d, n = r.children, h = o.useState(t), v = c(h, 2), C = v[0], g = v[1], p = C || t;
  process.env.NODE_ENV !== "production" && q(i() || !t, "Portal only work in client side. Please call 'useEffect' to show Portal instead default render in SSR."), o.useEffect(function() {
    (m || t) && g(t);
  }, [t, m]);
  var k = o.useState(function() {
    return E(s);
  }), R = c(k, 2), a = R[0], N = R[1];
  o.useEffect(function() {
    var l = E(s);
    N(l ?? null);
  });
  var $ = A(p && !a, P), S = c($, 2), y = S[0], w = S[1], u = a ?? y;
  M(_ && t && i() && (u === y || u === document.body));
  var D = null;
  if (n && I(n) && e) {
    var O = n;
    D = O.ref;
  }
  var b = V(D, e);
  if (!p || !i() || a === void 0)
    return null;
  var x = u === !1 || T(), f = n;
  return e && (f = /* @__PURE__ */ o.cloneElement(n, {
    ref: b
  })), /* @__PURE__ */ o.createElement(z.Provider, {
    value: w
  }, x ? f : /* @__PURE__ */ L.createPortal(f, u));
});
process.env.NODE_ENV !== "production" && (U.displayName = "Portal");
export {
  U as default
};
