"use strict";

exports.__esModule = true;
exports.WixPatternsModalsContainer = WixPatternsModalsContainer;
var _SidePanelModal = require("./SidePanelModal");
var _react = _interopRequireWildcard(require("react"));
var _reactDom = require("react-dom");
var _react2 = require("@wix/bex-core/react");
var _bexCore = require("@wix/bex-core");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/WixPatternsModalsContainer.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function WixPatternsModalsContainer(props) {
  const {
    state,
    rootRef,
    anchorRef,
    zIndex = 1
  } = props;
  const {
    appendModalsToBody,
    lodash,
    useRelativeSidePanel
  } = (0, _react2.useWixPatternsContainer)();
  const [render, setRender] = (0, _react.useState)({});
  (0, _react.useEffect)(() => {
    const {
      container,
      top
    } = appendModalsToBody ? {
      container: document.body,
      top: 0
    } : {
      container: (rootRef == null ? void 0 : rootRef.current) ?? document.body,
      top: null
    };
    const setTop = lodash.throttle(() => {
      const id = window.requestAnimationFrame(() => {
        setRender({
          top: top != null ? top : container.getBoundingClientRect().top,
          left: container.getBoundingClientRect().right,
          anchor: (anchorRef == null ? void 0 : anchorRef.current) ?? container
        });
      });
      disposers.push(() => window.cancelAnimationFrame(id));
    }, 300);
    const disposers = [(0, _bexCore.addResizeObserver)(container, setTop)];
    setTop();
    return () => {
      setTop.cancel();
      disposers.forEach(d => d == null ? void 0 : d());
    };
  }, []);

  // creates a new stacking context
  const modalsContainerStyle = (0, _react.useMemo)(() => ({
    position: 'relative',
    zIndex
  }), [zIndex]);
  let child = render.top != null && /*#__PURE__*/_react.default.createElement(_SidePanelModal.SidePanelModal, {
    state: state,
    top: useRelativeSidePanel ? 0 : render.top,
    left: render.left,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 78,
      columnNumber: 5
    }
  });
  if (!useRelativeSidePanel) {
    child = /*#__PURE__*/_react.default.createElement("div", {
      style: modalsContainerStyle,
      "data-hook": "cairo-modals-container",
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 87,
        columnNumber: 7
      }
    }, child);
  }
  if (render.anchor == null) {
    return null;
  }
  child = /*#__PURE__*/(0, _reactDom.createPortal)(child, render.anchor);
  return child;
}
//# sourceMappingURL=WixPatternsModalsContainer.js.map