import{r as t,e,f as s,d as o,h as i,H as r}from"./p-5ab6b26f.js";import{a as l,b as n,c as h}from"./p-e2bb9344.js";class a{constructor(s){t(this,s),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,this.ionItemReorder=e(this,"ionItemReorder",7)}disabledChanged(){this.gesture&&this.gesture.setDisabled(this.disabled)}async componentDidLoad(){const t=this.el.closest("ion-content");t&&(await t.componentOnReady(),this.scrollEl=await t.getScrollElement()),this.gesture=(await __sc_import_webmapgl("./p-62bf8ee1.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()}componentDidUnload(){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 e=t.event.target.closest("ion-reorder");if(!e)return!1;const s=d(e,this.el);return!!s&&(t.data=s,!0)}onStart(t){t.event.preventDefault();const e=this.selectedItemEl=t.data,s=this.cachedHeights;s.length=0;const o=this.el,i=o.children;if(!i||0===i.length)return;let r=0;for(let t=0;t<i.length;t++){const e=i[t];s.push(r+=e.offsetHeight),e.$ionIndex=t}const n=o.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+m,this.scrollElBottom=t.bottom-m}else this.scrollElInitial=0,this.scrollElTop=0,this.scrollElBottom=0;this.lastToIndex=c(e),this.selectedItemHeight=e.offsetHeight,this.state=1,e.classList.add(u),l()}onMove(t){const e=this.selectedItemEl;if(!e)return;const s=this.autoscroll(t.currentY),o=this.containerTop-s,i=Math.max(o,Math.min(t.currentY,this.containerBottom-s)),r=s+i-t.startY,l=this.itemIndexForTop(i-o);if(l!==this.lastToIndex){const t=c(e);this.lastToIndex=l,n(),this.reorderMove(t,l)}e.style.transform=`translateY(${r}px)`}onEnd(){const t=this.selectedItemEl;if(this.state=2,!t)return void(this.state=0);const e=this.lastToIndex,s=c(t);e===s?(t.style.transition="transform 200ms ease-in-out",setTimeout(()=>this.completeSync(),200)):this.ionItemReorder.emit({from:s,to:e,complete:this.completeSync.bind(this)}),h()}completeSync(t){const e=this.selectedItemEl;if(e&&2===this.state){const s=this.el.children,o=s.length,i=this.lastToIndex,r=c(e);t&&!0!==t||this.el.insertBefore(e,r<i?s[i+1]:s[i]),Array.isArray(t)&&(t=g(t,r,i));for(let t=0;t<o;t++)s[t].style.transform="";e.style.transition="",e.classList.remove(u),this.selectedItemEl=void 0,this.state=0}return t}itemIndexForTop(t){const e=this.cachedHeights;let s=0;for(s=0;s<e.length&&!(e[s]>t);s++);return s}reorderMove(t,e){const s=this.selectedItemHeight,o=this.el.children;for(let i=0;i<o.length;i++){let r="";i>t&&i<=e?r=`translateY(${-s}px)`:i<t&&i>=e&&(r=`translateY(${s}px)`),o[i].style.transform=r}}autoscroll(t){if(!this.scrollEl)return 0;let e=0;return t<this.scrollElTop?e=-p:t>this.scrollElBottom&&(e=p),0!==e&&this.scrollEl.scrollBy(0,e),this.scrollEl.scrollTop-this.scrollElInitial}hostData(){return{class:{[s(this)]:!0,"reorder-enabled":!this.disabled,"reorder-list-active":0!==this.state}}}get el(){return o(this)}static get watchers(){return{disabled:["disabledChanged"]}}render(){return i(r,this.hostData())}static get style(){return".reorder-list-active>*{-webkit-transition:-webkit-transform .3s;transition:-webkit-transform .3s;transition:transform .3s;transition:transform .3s,-webkit-transform .3s;will-change:transform}.reorder-enabled{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.reorder-enabled ion-reorder{display:block;cursor:-webkit-grab;cursor:grab;pointer-events:all;-ms-touch-action:none;touch-action:none}.reorder-selected,.reorder-selected ion-reorder{cursor:-webkit-grabbing;cursor:grabbing}.reorder-selected{position:relative;-webkit-transition:none!important;transition:none!important;-webkit-box-shadow:0 0 10px rgba(0,0,0,.4);box-shadow:0 0 10px rgba(0,0,0,.4);opacity:.8;z-index:100}.reorder-visible ion-reorder .reorder-icon{-webkit-transform:translateZ(0);transform:translateZ(0)}"}}const c=t=>t.$ionIndex,d=(t,e)=>{let s;for(;t;){if((s=t.parentElement)===e)return t;t=s}},m=60,p=10,u="reorder-selected",g=(t,e,s)=>{const o=t[e];return t.splice(e,1),t.splice(s,0,o),t.slice()};export{a as ion_reorder_group};