import{r as t,i as s,h as r,e as i,H as e}from"./p-9babd345.js";import{g as o}from"./p-fef04ab5.js";import{c as n}from"./p-55ae49e1.js";import{a as h,b as a,c}from"./p-063cd168.js";const d=class{constructor(r){t(this,r),this.ionItemReorder=s(this,"ionItemReorder",7),this.lastToIndex=-1,this.cachedHeights=[],this.scrollElTop=0,this.scrollElBottom=0,this.scrollElInitial=0,this.containerTop=0,this.containerBottom=0,this.state=0,this.disabled=!0}disabledChanged(){this.gesture&&this.gesture.enable(!this.disabled)}async connectedCallback(){const t=this.el.closest("ion-content");t&&(await new Promise((s=>n(t,s))),this.scrollEl=await t.getScrollElement()),this.gesture=(await import("./p-b3e6427d.js")).createGesture({el:this.el,gestureName:"reorder",gesturePriority:110,threshold:0,direction:"y",passive:!1,canStart:t=>this.canStart(t),onStart:t=>this.onStart(t),onMove:t=>this.onMove(t),onEnd:()=>this.onEnd()}),this.disabledChanged()}disconnectedCallback(){this.onEnd(),this.gesture&&(this.gesture.destroy(),this.gesture=void 0)}complete(t){return Promise.resolve(this.completeSync(t))}canStart(t){if(this.selectedItemEl||0!==this.state)return!1;const s=t.event.target.closest("ion-reorder");if(!s)return!1;const r=f(s,this.el);return!!r&&(t.data=r,!0)}onStart(t){t.event.preventDefault();const s=this.selectedItemEl=t.data,r=this.cachedHeights;r.length=0;const i=this.el,e=i.children;if(!e||0===e.length)return;let o=0;for(let t=0;t<e.length;t++){const s=e[t];o+=s.offsetHeight,r.push(o),s.$ionIndex=t}const n=i.getBoundingClientRect();if(this.containerTop=n.top,this.containerBottom=n.bottom,this.scrollEl){const t=this.scrollEl.getBoundingClientRect();this.scrollElInitial=this.scrollEl.scrollTop,this.scrollElTop=t.top+p,this.scrollElBottom=t.bottom-p}else this.scrollElInitial=0,this.scrollElTop=0,this.scrollElBottom=0;this.lastToIndex=l(s),this.selectedItemHeight=s.offsetHeight,this.state=1,s.classList.add(m),h()}onMove(t){const s=this.selectedItemEl;if(!s)return;const r=this.autoscroll(t.currentY),i=this.containerTop-r,e=Math.max(i,Math.min(t.currentY,this.containerBottom-r)),o=r+e-t.startY,n=this.itemIndexForTop(e-i);if(n!==this.lastToIndex){const t=l(s);this.lastToIndex=n,a(),this.reorderMove(t,n)}s.style.transform=`translateY(${o}px)`}onEnd(){const t=this.selectedItemEl;if(this.state=2,!t)return void(this.state=0);const s=this.lastToIndex,r=l(t);s===r?this.completeSync():this.ionItemReorder.emit({from:r,to:s,complete:this.completeSync.bind(this)}),c()}completeSync(t){const s=this.selectedItemEl;if(s&&2===this.state){const r=this.el.children,i=r.length,e=this.lastToIndex,o=l(s);e===o||void 0!==t&&!0!==t||this.el.insertBefore(s,o<e?r[e+1]:r[e]),Array.isArray(t)&&(t=b(t,o,e));for(let t=0;t<i;t++)r[t].style.transform="";s.style.transition="",s.classList.remove(m),this.selectedItemEl=void 0,this.state=0}return t}itemIndexForTop(t){const s=this.cachedHeights;for(let r=0;r<s.length;r++)if(s[r]>t)return r;return s.length-1}reorderMove(t,s){const r=this.selectedItemHeight,i=this.el.children;for(let e=0;e<i.length;e++){let o="";e>t&&e<=s?o=`translateY(${-r}px)`:e<t&&e>=s&&(o=`translateY(${r}px)`),i[e].style.transform=o}}autoscroll(t){if(!this.scrollEl)return 0;let s=0;return t<this.scrollElTop?s=-u:t>this.scrollElBottom&&(s=u),0!==s&&this.scrollEl.scrollBy(0,s),this.scrollEl.scrollTop-this.scrollElInitial}render(){const t=o(this);return r(e,{class:{[t]:!0,"reorder-enabled":!this.disabled,"reorder-list-active":0!==this.state}})}get el(){return i(this)}static get watchers(){return{disabled:["disabledChanged"]}}},l=t=>t.$ionIndex,f=(t,s)=>{let r;for(;t;){if(r=t.parentElement,r===s)return t;t=r}},p=60,u=10,m="reorder-selected",b=(t,s,r)=>{const i=t[s];return t.splice(s,1),t.splice(r,0,i),t.slice()};d.style=".reorder-list-active>*{transition:transform 300ms;will-change:transform}.reorder-enabled{user-select:none}.reorder-enabled ion-reorder{display:block;cursor:grab;pointer-events:all;touch-action:none}.reorder-selected,.reorder-selected ion-reorder{cursor:grabbing}.reorder-selected{position:relative;transition:none !important;box-shadow:0 0 10px rgba(0, 0, 0, 0.4);opacity:0.8;z-index:100}.reorder-visible ion-reorder .reorder-icon{transform:translate3d(0,  0,  0)}";export{d as ion_reorder_group}