import React__default,{useContext,useState,useEffect,useRef,createContext}from"react";import PropTypes from"prop-types";import{createPortal}from"react-dom";const DEFAULT_PORTAL_ROOT_ID="juno-portal-root",PortalContext=createContext(),portalRootStyles={position:"absolute",top:"0",left:"0"},portalStyles={position:"relative",zIndex:"1"},Portal=a=>{let{children:b=null}=a;const c=useContext(PortalContext),[d,e]=useState(!1);if(useEffect(()=>{c?.current&&e(!0)},[c]),!d)return null;const f=React__default.createElement("div",{className:`juno-portal`,style:portalStyles},b);return createPortal(f,c.current||document.body)};Portal.propTypes={children:PropTypes.node};function usePortalRef(){const a=useContext(PortalContext),b=useRef(null),[c,d]=useState(!1);return useEffect(()=>{if(!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.");const 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,d(!0),()=>{b.current&&a?.current&&(a.current.removeChild(b.current),b.current=null)}},[a]),b?.current?c?b.current:null:null}const PortalProvider=a=>{let{children:b=null,className:c="",id:d=DEFAULT_PORTAL_ROOT_ID}=a;const e=useRef(),[f,g]=useState(!1);return useEffect(()=>{e.current&&g(!0)},[]),React__default.createElement(PortalContext.Provider,{value:e},f&&b,React__default.createElement("div",{className:`juno-portal-root ${c}`,id:d,ref:e,style:portalRootStyles}))};PortalProvider.Portal=Portal,Portal.displayName="PortalProvider.Portal",PortalProvider.propTypes={className:PropTypes.string,id:PropTypes.string,children:PropTypes.node},PortalProvider.Portal.propTypes={children:PropTypes.node};export{PortalProvider as P,usePortalRef as u};