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