import s,{createContext as E,useContext as h,useState as p}from"react";import{Hidden as b,HiddenFeatures as M}from'../internal/hidden.js';import*as f from'../utils/dom.js';import{getOwnerDocument as v}from'../utils/owner.js';import{useEvent as m}from'./use-event.js';import{useOwnerDocument as x}from'./use-owner.js';function H({defaultContainers:r=[],portals:n,mainTreeNode:o}={}){let l=x(o),u=m(()=>{var i,c;let t=[];for(let e of r)e!==null&&(f.isElement(e)?t.push(e):"current"in e&&f.isElement(e.current)&&t.push(e.current));if(n!=null&&n.current)for(let e of n.current)t.push(e);for(let e of(i=l==null?void 0:l.querySelectorAll("html > *, body > *"))!=null?i:[])e!==document.body&&e!==document.head&&f.isElement(e)&&e.id!=="headlessui-portal-root"&&(o&&(e.contains(o)||e.contains((c=o==null?void 0:o.getRootNode())==null?void 0:c.host))||t.some(d=>e.contains(d))||t.push(e));return t});return{resolveContainers:u,contains:m(t=>u().some(i=>i.contains(t)))}}let a=E(null);function P({children:r,node:n}){let[o,l]=p(null),u=y(n!=null?n:o);return s.createElement(a.Provider,{value:u},r,u===null&&s.createElement(b,{features:M.Hidden,ref:t=>{var i,c;if(t){for(let e of(c=(i=v(t))==null?void 0:i.querySelectorAll("html > *, body > *"))!=null?c:[])if(e!==document.body&&e!==document.head&&f.isElement(e)&&e!=null&&e.contains(t)){l(e);break}}}}))}function y(r=null){var n;return(n=h(a))!=null?n:r}export{P as MainTreeProvider,y as useMainTreeNode,H as useRootContainers};
