import{computed as w,defineComponent as m,getCurrentInstance as j,h as I,inject as s,onMounted as R,onUnmounted as y,provide as E,reactive as G,ref as p,Teleport as O,watch as D,watchEffect as K}from"vue";import{usePortalRoot as S}from'../../internal/portal-force-root.js';import{dom as B}from'../../utils/dom.js';import{getOwnerDocument as C}from'../../utils/owner.js';import{render as h}from'../../utils/render.js';function x(e){let t=C(e);if(!t){if(e===null)return null;throw new Error(`[Headless UI]: Cannot find ownerDocument for contextElement: ${e}`)}let l=t.getElementById("headlessui-portal-root");if(l)return l;let r=t.createElement("div");return r.setAttribute("id","headlessui-portal-root"),t.body.appendChild(r)}const f=new WeakMap;function U(e){var t;return(t=f.get(e))!=null?t:0}function M(e,t){let l=t(U(e));return l<=0?f.delete(e):f.set(e,l),l}let $=m({name:"Portal",props:{as:{type:[Object,String],default:"div"}},setup(e,{slots:t,attrs:l}){let r=p(null),i=w(()=>C(r)),o=S(),u=s(H,null),n=p(o===!0||u==null?x(r.value):u.resolveTarget());n.value&&M(n.value,a=>a+1);let c=p(!1);R(()=>{c.value=!0}),K(()=>{o||u!=null&&(n.value=u.resolveTarget())});let v=s(d,null),g=!1,b=j();return D(r,()=>{if(g||!v)return;let a=B(r);a&&(y(v.register(a),b),g=!0)}),y(()=>{var P,T;let a=(P=i.value)==null?void 0:P.getElementById("headlessui-portal-root");!a||n.value!==a||M(n.value,L=>L-1)||n.value.children.length>0||(T=n.value.parentElement)==null||T.removeChild(n.value)}),()=>{if(!c.value||n.value===null)return null;let a={ref:r,"data-headlessui-portal":""};return I(O,{to:n.value},h({ourProps:a,theirProps:e,slot:{},attrs:l,slots:t,name:"Portal"}))}}}),d=Symbol("PortalParentContext");function q(){let e=s(d,null),t=p([]);function l(o){return t.value.push(o),e&&e.register(o),()=>r(o)}function r(o){let u=t.value.indexOf(o);u!==-1&&t.value.splice(u,1),e&&e.unregister(o)}let i={register:l,unregister:r,portals:t};return[t,m({name:"PortalWrapper",setup(o,{slots:u}){return E(d,i),()=>{var n;return(n=u.default)==null?void 0:n.call(u)}}})]}let H=Symbol("PortalGroupContext"),z=m({name:"PortalGroup",props:{as:{type:[Object,String],default:"template"},target:{type:Object,default:null}},setup(e,{attrs:t,slots:l}){let r=G({resolveTarget(){return e.target}});return E(H,r),()=>{let{target:i,...o}=e;return h({theirProps:o,ourProps:{},slot:{},attrs:t,slots:l,name:"PortalGroup"})}}});export{$ as Portal,z as PortalGroup,q as useNestedPortals};
