import{c as f}from"./create-split-props-u5h9OPvL.js";import{r as p}from"./iframe-B8FSoBNY.js";import{c as g,e as v,A as y,r as N,g as E,b as P}from"./normalize-props-D7NI5JRu.js";import{g as U}from"./computed-style-C0hzrq9-.js";import{q as A,c as M}from"./factory-BR3bc_RM.js";const L=e=>f()(e,["immediate","lazyMount","onExitComplete","present","skipAnimationOnMount","unmountOnExit"]);function O(e,t){const{state:o,send:s,context:n}=e,r=o.matches("mounted","unmountSuspended");return{skip:!n.get("initial"),present:r,setNode(i){i&&s({type:"NODE.SET",node:i})},unmount(){s({type:"UNMOUNT"})}}}var C=g({props({props:e}){return{...e,present:!!e.present}},initialState({prop:e}){return e("present")?"mounted":"unmounted"},refs(){return{node:null,styles:null}},context({bindable:e}){return{unmountAnimationName:e(()=>({defaultValue:null})),prevAnimationName:e(()=>({defaultValue:null})),present:e(()=>({defaultValue:!1})),initial:e(()=>({sync:!0,defaultValue:!1}))}},exit:["cleanupNode"],watch({track:e,prop:t,send:o}){e([()=>t("present")],()=>{o({type:"PRESENCE.CHANGED"})})},on:{"NODE.SET":{actions:["setupNode"]},"PRESENCE.CHANGED":{actions:["setInitial","syncPresence"]}},states:{mounted:{on:{UNMOUNT:{target:"unmounted",actions:["clearPrevAnimationName","invokeOnExitComplete"]},"UNMOUNT.SUSPEND":{target:"unmountSuspended"}}},unmountSuspended:{effects:["trackAnimationEvents"],on:{MOUNT:{target:"mounted",actions:["setPrevAnimationName"]},UNMOUNT:{target:"unmounted",actions:["clearPrevAnimationName","invokeOnExitComplete"]}}},unmounted:{on:{MOUNT:{target:"mounted",actions:["setPrevAnimationName"]}}}},implementations:{actions:{setInitial:({context:e})=>{e.get("initial")||queueMicrotask(()=>{e.set("initial",!0)})},invokeOnExitComplete:({prop:e,refs:t})=>{e("onExitComplete")?.();const o=t.get("node");if(!o)return;const s=A(o),n=new s.CustomEvent("exitcomplete",{bubbles:!1});o.dispatchEvent(n)},setupNode:({refs:e,event:t})=>{e.get("node")!==t.node&&(e.set("node",t.node),e.set("styles",U(t.node)))},cleanupNode:({refs:e})=>{e.set("node",null),e.set("styles",null)},syncPresence:({context:e,refs:t,send:o,prop:s})=>{const n=s("present");if(n)return o({type:"MOUNT",src:"presence.changed"});const r=t.get("node");if(!n&&r?.ownerDocument.visibilityState==="hidden")return o({type:"UNMOUNT",src:"visibilitychange"});N(()=>{const i=m(t.get("styles"));e.set("unmountAnimationName",i),i==="none"||i===e.get("prevAnimationName")||t.get("styles")?.display==="none"||t.get("styles")?.animationDuration==="0s"?o({type:"UNMOUNT",src:"presence.changed"}):o({type:"UNMOUNT.SUSPEND"})})},setPrevAnimationName:({context:e,refs:t})=>{N(()=>{e.set("prevAnimationName",m(t.get("styles")))})},clearPrevAnimationName:({context:e})=>{e.set("prevAnimationName",null)}},effects:{trackAnimationEvents:({context:e,refs:t,send:o,prop:s})=>{const n=t.get("node");if(!n)return;const r=a=>{(a.composedPath?.()?.[0]??a.target)===n&&e.set("prevAnimationName",m(t.get("styles")))},i=a=>{const c=m(t.get("styles"));E(a)===n&&c===e.get("unmountAnimationName")&&!s("present")&&o({type:"UNMOUNT",src:"animationend"})},u=a=>{E(a)===n&&!s("present")&&o({type:"UNMOUNT",src:"animationcancel"})};n.addEventListener("animationstart",r),n.addEventListener("animationcancel",u),n.addEventListener("animationend",i);const l=v(n,{animationFillMode:"forwards"});return()=>{n.removeEventListener("animationstart",r),n.removeEventListener("animationcancel",u),n.removeEventListener("animationend",i),y(()=>l())}}}}});function m(e){return e?.animationName||"none"}function S(e,t={}){const{sync:o=!1}=t,s=x(e);return p.useCallback((...n)=>o?queueMicrotask(()=>s.current?.(...n)):s.current?.(...n),[o,s])}function x(e){const t=p.useRef(e);return t.current=e,t}const R=(e={})=>{const{lazyMount:t,unmountOnExit:o,present:s,skipAnimationOnMount:n=!1,...r}=e,i=p.useRef(!1),u={...r,present:s,onExitComplete:S(e.onExitComplete)},l=P(C,u),a=O(l);a.present&&(i.current=!0);const c=!a.present&&!i.current&&t||o&&!a.present&&i.current,d=()=>({"data-state":a.skip&&n?void 0:s?"open":"closed",hidden:!a.present});return{ref:a.setNode,getPresenceProps:d,present:a.present,unmounted:c}},[V,q]=M({name:"PresenceContext",hookName:"usePresenceContext",providerName:"<PresenceProvider />"});export{V as P,q as a,L as s,R as u};
