import{r as t,f as e,h as i,e as s,c as r}from"./p-5da71329.js";import{c as a,i as n,s as h,p as l,a as o,e as d,b as u,h as f}from"./p-e070304f.js";const c=t=>async e=>{t.dataset.hold="";await e(t).then((t=>{if(t&&"finished"in t){return t.finished}return}));delete t.dataset.hold};let m=class{constructor(i){t(this,i);this.animatePresenceExitComplete=e(this,"animatePresenceExitComplete",7);this.animatePresenceEnter=e(this,"animatePresenceEnter",7);this.animatePresenceExit=e(this,"animatePresenceExit",7);this.__presenceKey=`animate-presence-${v++}`;this.descendants=[];this.getClosestParent=()=>{var t;const e=(t=this.element.parentElement,t!==null&&t!==void 0?t:this.element.getRootNode().host);return a(this.element.tagName,e)};this.willExit=false;this.didExit=false;this.willEnter=false;this.didEnter=false;this.handleMutation=this.handleMutation.bind(this)}observeChanged(){if(this.observe){this.addMO();this.mo.observe(this.element,{childList:true,attributes:true,attributeFilter:["data-key"]})}else{this.removeMO()}}async componentWillLoad(){var t,e;n();this.ancestor=this.getClosestParent();if(typeof this.observe==="undefined"){this.observe=(e=(t=this.ancestor)===null||t===void 0?void 0:t.observe,e!==null&&e!==void 0?e:true)}Array.from(this.element.children).map(((t,e)=>{h(t,{i:e});t.style.setProperty("animation-play-state","paused");t.dataset.enter=""}))}async componentDidLoad(){var t;this.observeChanged();(t=this.ancestor)===null||t===void 0?void 0:t.registerChild(this.element);if(!this.ancestor){this.enter()}}async componentDidUnload(){var t;this.removeMO();(t=this.ancestor)===null||t===void 0?void 0:t.unregisterChild(this.__presenceKey);this.descendants=[]}async enterNode(t,e=0){delete t.dataset.exit;const i=new CustomEvent("animatePresenceEnter",{bubbles:true,detail:{i:e,hold:c(t)}});t.dispatchEvent(i);t.style.removeProperty("animation-play-state");t.dataset.enter="";h(t,{i:e});await l(t,{afterSelf:async()=>{delete t.dataset.initial;delete t.dataset.enter;t.style.removeProperty("--i")}});return o(t)}async exitNode(t,e="remove",i=0){await d(t);delete t.dataset.willExit;h(t,{i});const s=new CustomEvent("animatePresenceExit",{bubbles:true,detail:{i,hold:c(t)}});t.dispatchEvent(s);t.dataset.exit="";await l(t,{afterSelf:()=>{if(e==="remove"){t.remove()}else if(e==="hide"){t.style.setProperty("visibility","hidden")}}});return Promise.resolve()}async handleEnter(t,e,i){if(!u(t))return;if(f(t,"exit"))return;if(f(t,"willExit")){return this.exitNode(t,"remove",i)}else{return this.enterNode(t,i)}}async handleExit(t,e,i){if(!u(t))return;if(f(t,"exit")||f(t,"willExit")){return}t.dataset.willExit="";h(t,{i});if(u(e.previousSibling)){e.previousSibling.insertAdjacentElement("afterend",t)}else if(u(e.target)){e.target.prepend(t)}}handleMutation(t){let e=0;for(const i of t.reverse()){if(i.addedNodes.length===1){this.handleEnter(i.addedNodes[0],i,t.length-1-e)}if(i.removedNodes.length===1){this.handleExit(i.removedNodes[0],i,e)}e++}}addMO(){if(!this.mo){if("MutationObserver"in window){this.mo=new MutationObserver(this.handleMutation);this.observeChanged()}}}removeMO(){if(this.mo){this.mo.disconnect();this.mo=undefined}}async registerChild(t){const e=t.__presenceKey;this.descendants=[...this.descendants.filter((t=>t.__presenceKey!==e)),t];return}async unregisterChild(t){this.descendants=this.descendants.filter((e=>e.__presenceKey!==t));return}animatePresenceExitCompleteHandler(t){t.stopPropagation()}async exit(){if(this.didExit||this.willExit)return;this.willExit=true;await Promise.all(Array.from(this.element.children).reverse().map(((t,e)=>this.exitNode(t,"hide",e))));this.didExit=true;this.willExit=false;this.animatePresenceExitComplete.emit();return Promise.resolve()}async enter(){this.didExit=false;this.willExit=false;if(this.didEnter||this.willEnter)return;this.willEnter=true;await Promise.all(Array.from(this.element.children).map(((t,e)=>this.enterNode(t,e))));await o(this.element);this.didEnter=true;this.willEnter=false;return Promise.resolve()}render(){return i(s,{style:{display:"contents"}},i("slot",null))}get element(){return r(this)}static get watchers(){return{observe:["observeChanged"]}}};let v=0;export{m as animate_presence};
//# sourceMappingURL=p-ae7eeef0.entry.js.map