import*as e from"react";import{useComposedRefs as n}from"../../react-compose-refs/dist/index.js";import{useLayoutEffect as t}from"../../react-use-layout-effect/dist/index.js";var r=r=>{const{present:i,children:u}=r,s=function(n){const[r,i]=e.useState(),u=e.useRef({}),s=e.useRef(n),a=e.useRef("none"),c=n?"mounted":"unmounted",[d,m]=function(n,t){return e.useReducer(((e,n)=>t[e][n]??e),n)}(c,{mounted:{UNMOUNT:"unmounted",ANIMATION_OUT:"unmountSuspended"},unmountSuspended:{MOUNT:"mounted",ANIMATION_END:"unmounted"},unmounted:{MOUNT:"mounted"}});return e.useEffect((()=>{const e=o(u.current);a.current="mounted"===d?e:"none"}),[d]),t((()=>{const e=u.current,t=s.current;if(t!==n){const r=a.current,i=o(e);if(n)m("MOUNT");else if("none"===i||"none"===e?.display)m("UNMOUNT");else{m(t&&r!==i?"ANIMATION_OUT":"UNMOUNT")}s.current=n}}),[n,m]),t((()=>{if(r){let e;const n=r.ownerDocument.defaultView??window,t=t=>{const i=o(u.current).includes(t.animationName);if(t.target===r&&i&&(m("ANIMATION_END"),!s.current)){const t=r.style.animationFillMode;r.style.animationFillMode="forwards",e=n.setTimeout((()=>{"forwards"===r.style.animationFillMode&&(r.style.animationFillMode=t)}))}},i=e=>{e.target===r&&(a.current=o(u.current))};return r.addEventListener("animationstart",i),r.addEventListener("animationcancel",t),r.addEventListener("animationend",t),()=>{n.clearTimeout(e),r.removeEventListener("animationstart",i),r.removeEventListener("animationcancel",t),r.removeEventListener("animationend",t)}}m("ANIMATION_END")}),[r,m]),{isPresent:["mounted","unmountSuspended"].includes(d),ref:e.useCallback((e=>{e&&(u.current=getComputedStyle(e)),i(e)}),[])}}(i),a="function"==typeof u?u({present:s.isPresent}):e.Children.only(u),c=n(s.ref,function(e){let n=Object.getOwnPropertyDescriptor(e.props,"ref")?.get,t=n&&"isReactWarning"in n&&n.isReactWarning;if(t)return e.ref;if(n=Object.getOwnPropertyDescriptor(e,"ref")?.get,t=n&&"isReactWarning"in n&&n.isReactWarning,t)return e.props.ref;return e.props.ref||e.ref}(a));return"function"==typeof u||s.isPresent?e.cloneElement(a,{ref:c}):null};function o(e){return e?.animationName||"none"}r.displayName="Presence";export{r as Presence};
//# sourceMappingURL=index.js.map
