import React__default,{useContext,useState,useEffect,createContext,useRef}from"react";import{createPortal}from"react-dom";var portalStyles={position:"relative",zIndex:"1"},Portal=function(a){var b=a.children,c=void 0===b?null:b,d=useContext(PortalContext),e=useState(!1),f=e[0],g=e[1];if(useEffect(function(){(null===d||void 0===d?void 0:d.current)&&g(!0)},[d]),!f)return null;var h=React__default.createElement("div",{className:"juno-portal",style:portalStyles},c);return createPortal(h,d.current||document.body)};Portal.displayName="PortalProvider.Portal";var DEFAULT_PORTAL_ROOT_ID="juno-portal-root",PortalContext=createContext(void 0),portalRootStyles={position:"absolute",top:"0",left:"0"};function usePortalRef(){var a=useContext(PortalContext),b=useRef(null),c=useState(!1),d=c[0],e=c[1];return useEffect(function(){if(!a||null===a||void 0===a||!a.current)return void console.warn("usePortalRef must be called inside a PortalProvider. You are probably using a component that renders a portal, e.g. Modal or Select. Make sure your app is wrapped in an AppShellProvider. Alternatively, a PortalProvider can be included manually.");var c=document.createElement("div");return c.style.position="relative",c.style.zIndex="1",c.classList.add("juno-portal"),a.current.append(c),b.current=c,e(!0),function(){b.current&&(null===a||void 0===a?void 0:a.current)&&(a.current.removeChild(b.current),b.current=null)}},[a]),null!==b&&void 0!==b&&b.current?d?b.current:null:null}var PortalProvider=function(a){var b=a.children,c=void 0===b?null:b,d=a.className,e=void 0===d?"":d,f=a.id,g=void 0===f?DEFAULT_PORTAL_ROOT_ID:f,h=useRef(null),i=useState(!1),j=i[0],k=i[1];return useEffect(function(){h.current&&k(!0)},[]),(React__default.createElement(PortalContext.Provider,{value:h},j&&c,React__default.createElement("div",{className:"juno-portal-root ".concat(e),id:g,ref:h,style:portalRootStyles})))};PortalProvider.Portal=Portal;export{PortalContext as P,PortalProvider as a,usePortalRef as u};