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