import T,{Fragment as W,createContext as q,useContext as U,useEffect as S,useMemo as z,useRef as c,useState as j}from"react";import{Features as de,forwardRefWithAs as k,render as J,RenderStrategy as m}from"../../utils/render.js";import{OpenClosedProvider as fe,State as V,useOpenClosed as K}from"../../internal/open-closed.js";import{match as R}from"../../utils/match.js";import{microTask as Te}from"../../utils/micro-task.js";import{useId as pe}from"../../hooks/use-id.js";import{useIsMounted as me}from"../../hooks/use-is-mounted.js";import{useIsoMorphicEffect as ce}from"../../hooks/use-iso-morphic-effect.js";import{useLatestValue as I}from"../../hooks/use-latest-value.js";import{useServerHandoffComplete as Q}from"../../hooks/use-server-handoff-complete.js";import{useSyncRefs as X}from"../../hooks/use-sync-refs.js";import{useTransition as he}from"../../hooks/use-transition.js";import{useEvent as Y}from"../../hooks/use-event.js";function h(e=""){return e.split(" ").filter(n=>n.trim().length>1)}let N=q(null);N.displayName="TransitionContext";var ge=(t=>(t.Visible="visible",t.Hidden="hidden",t))(ge||{});function ve(){let e=U(N);if(e===null)throw new Error("A <Transition.Child /> is used but it is missing a parent <Transition /> or <Transition.Root />.");return e}function Ce(){let e=U(L);if(e===null)throw new Error("A <Transition.Child /> is used but it is missing a parent <Transition /> or <Transition.Root />.");return e}let L=q(null);L.displayName="NestingContext";function w(e){return"children"in e?w(e.children):e.current.filter(({state:n})=>n==="visible").length>0}function Z(e){let n=I(e),t=c([]),r=me(),s=Y((l,o=m.Hidden)=>{let a=t.current.findIndex(({id:u})=>u===l);a!==-1&&(R(o,{[m.Unmount](){t.current.splice(a,1)},[m.Hidden](){t.current[a].state="hidden"}}),Te(()=>{var u;!w(t)&&r.current&&((u=n.current)==null||u.call(n))}))}),g=Y(l=>{let o=t.current.find(({id:a})=>a===l);return o?o.state!=="visible"&&(o.state="visible"):t.current.push({id:l,state:"visible"}),()=>s(l,m.Unmount)});return z(()=>({children:t,register:g,unregister:s}),[g,s,t])}function be(){}let Se=["beforeEnter","afterEnter","beforeLeave","afterLeave"];function $(e){var t;let n={};for(let r of Se)n[r]=(t=e[r])!=null?t:be;return n}function Ee(e){let n=c($(e));return S(()=>{n.current=$(e)},[e]),n}let xe="div",ee=de.RenderStrategy,te=k(function(n,t){let{beforeEnter:r,afterEnter:s,beforeLeave:g,afterLeave:l,enter:o,enterFrom:a,enterTo:u,entered:A,leave:v,leaveFrom:E,leaveTo:x,...y}=n,d=c(null),D=X(d,t),[i,C]=j("visible"),p=y.unmount?m.Unmount:m.Hidden,{show:b,appear:re,initial:ne}=ve(),{register:H,unregister:P}=Ce(),F=c(null),f=pe();S(()=>{if(!!f)return H(f)},[H,f]),S(()=>{if(p===m.Hidden&&!!f){if(b&&i!=="visible"){C("visible");return}R(i,{["hidden"]:()=>P(f),["visible"]:()=>H(f)})}},[i,f,H,P,b,p]);let ie=I({enter:h(o),enterFrom:h(a),enterTo:h(u),entered:h(A),leave:h(v),leaveFrom:h(E),leaveTo:h(x)}),se=Ee({beforeEnter:r,afterEnter:s,beforeLeave:g,afterLeave:l}),O=Q();S(()=>{if(O&&i==="visible"&&d.current===null)throw new Error("Did you forget to passthrough the `ref` to the actual DOM node?")},[d,i,O]);let M=ne&&!re,oe=(()=>!O||M||F.current===b?"idle":b?"enter":"leave")(),_=c(!1),B=Z(()=>{_.current||(C("hidden"),P(f))});he({container:d,classes:ie,events:se,direction:oe,onStart:I(()=>{_.current=!0}),onStop:I(ue=>{_.current=!1,ue==="leave"&&!w(B)&&(C("hidden"),P(f))})}),S(()=>{!M||(p===m.Hidden?F.current=null:F.current=b)},[b,M,i]);let le=y,ae={ref:D};return T.createElement(L.Provider,{value:B},T.createElement(fe,{value:R(i,{["visible"]:V.Open,["hidden"]:V.Closed})},J({ourProps:ae,theirProps:le,defaultTag:xe,features:ee,visible:i==="visible",name:"Transition.Child"})))}),G=k(function(n,t){let{show:r,appear:s=!1,unmount:g,...l}=n,o=c(null),a=X(o,t);Q();let u=K();if(r===void 0&&u!==null&&(r=R(u,{[V.Open]:!0,[V.Closed]:!1})),![!0,!1].includes(r))throw new Error("A <Transition /> is used but it is missing a `show={true | false}` prop.");let[A,v]=j(r?"visible":"hidden"),E=Z(()=>{v("hidden")}),[x,y]=j(!0),d=c([r]);ce(()=>{x!==!1&&d.current[d.current.length-1]!==r&&(d.current.push(r),y(!1))},[d,r]);let D=z(()=>({show:r,appear:s,initial:x}),[r,s,x]);S(()=>{if(r)v("visible");else if(!w(E))v("hidden");else{let C=o.current;if(!C)return;let p=C.getBoundingClientRect();p.x===0&&p.y===0&&p.width===0&&p.height===0&&v("hidden")}},[r,E]);let i={unmount:g};return T.createElement(L.Provider,{value:E},T.createElement(N.Provider,{value:D},J({ourProps:{...i,as:W,children:T.createElement(te,{ref:a,...i,...l})},theirProps:{},defaultTag:W,features:ee,visible:A==="visible",name:"Transition"})))}),ye=k(function(n,t){let r=U(N)!==null,s=K()!==null;return T.createElement(T.Fragment,null,!r&&s?T.createElement(G,{ref:t,...n}):T.createElement(te,{ref:t,...n}))}),We=Object.assign(G,{Child:ye,Root:G});export{We as Transition};
