import { slicedToArray as _slicedToArray } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { useContext, useRef, useState, createContext } from 'react';
import { hasModals } from '../Modal/ModalContext.js';
import { ClientOnlyPortal } from './ClientOnlyPortal.js';
import { StyledPortal } from './Popup.styles.js';
import { canUseDOM } from '../../utils/canUseDOM.js';
import { safeUseId } from '../../utils/react.js';
import { Portal } from '../Portal/Portal.js';

var POPUP_PORTAL_ID = 'plasma-popup-root';
var initialItems = /*#__PURE__*/new Map();
var PopupContext = /*#__PURE__*/createContext({
  items: initialItems,
  rootId: POPUP_PORTAL_ID,
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  register: function register(_info) {
    throw new Error('Function not implemented. Add PopupProvider');
  },
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  unregister: function unregister(_id) {
    throw new Error('Function not implemented. Add PopupProvider');
  }
});
var usePopupContext = function usePopupContext() {
  return useContext(PopupContext);
};

// TODO: #1599
var PopupProvider = function PopupProvider(_ref) {
  var _StyledPortal, _StyledPortal2, _StyledPortal3;
  var children = _ref.children,
    providerFrame = _ref.providerFrame,
    UNSAFE_SSR_ENABLED = _ref.UNSAFE_SSR_ENABLED;
  var prevBodyOverflowY = useRef(canUseDOM ? document.body.style.overflowY : '');
  var _useState = useState(initialItems),
    _useState2 = _slicedToArray(_useState, 2),
    items = _useState2[0],
    setItems = _useState2[1];
  var uuid = safeUseId();
  var rootId = "".concat(POPUP_PORTAL_ID, "-").concat(uuid);
  var register = function register(info) {
    if (!canUseDOM) {
      return;
    }
    setItems(function (prevItems) {
      var _info$info;
      var newItems = new Map(prevItems);
      if ((_info$info = info.info) !== null && _info$info !== void 0 && _info$info.isModal && !hasModals(Array.from(prevItems.values()))) {
        prevBodyOverflowY.current = document.body.style.overflowY;
        document.body.style.overflowY = 'hidden';
      }
      newItems.set(info.id, info);
      return newItems;
    });
  };
  var unregister = function unregister(id) {
    if (!canUseDOM) {
      return;
    }
    setItems(function (prevItems) {
      if (!prevItems.has(id)) {
        return prevItems;
      }
      var newItems = new Map(prevItems);
      var prevHasModals = hasModals(Array.from(newItems.values()));
      newItems["delete"](id);
      if (prevHasModals && !hasModals(Array.from(newItems.values()))) {
        document.body.style.overflowY = prevBodyOverflowY.current;
      }
      return newItems;
    });
  };
  var context = {
    items: items,
    rootId: rootId,
    register: register,
    unregister: unregister
  };
  var getDefaultPortal = function getDefaultPortal() {
    if (typeof providerFrame !== 'string' && providerFrame && providerFrame.current && canUseDOM) {
      return /*#__PURE__*/React__default.createElement(Portal, {
        container: providerFrame.current
      }, _StyledPortal || (_StyledPortal = /*#__PURE__*/React__default.createElement(StyledPortal, {
        id: rootId
      })));
    }
    var withFrameId = typeof providerFrame === 'string' && providerFrame !== 'document';
    var containerElement = withFrameId && canUseDOM && document.getElementById(providerFrame);
    if (containerElement) {
      return /*#__PURE__*/React__default.createElement(Portal, {
        container: containerElement
      }, _StyledPortal2 || (_StyledPortal2 = /*#__PURE__*/React__default.createElement(StyledPortal, {
        id: rootId
      })));
    }
    return /*#__PURE__*/React__default.createElement(Portal, {
      container: document.body
    }, _StyledPortal3 || (_StyledPortal3 = /*#__PURE__*/React__default.createElement(StyledPortal, {
      id: rootId
    })));
  };
  return /*#__PURE__*/React__default.createElement(PopupContext.Provider, {
    value: context
  }, children, UNSAFE_SSR_ENABLED ? /*#__PURE__*/React__default.createElement(ClientOnlyPortal, null, /*#__PURE__*/React__default.createElement(StyledPortal, {
    id: rootId
  })) : getDefaultPortal());
};

export { POPUP_PORTAL_ID, PopupProvider, usePopupContext };
//# sourceMappingURL=PopupContext.js.map
