(()=>{"use strict";class t extends CustomEvent{constructor(t,e,n){super(n,{cancelable:!0});const{_pointerState:i}=t;this.originalEvent=e,this.draggingElement=i.targetElement,this.dragStartPos={x:i.startPosX,y:i.startPosY},this.dragStartTimestamp=i.startTimestamp}}class e extends t{constructor(t,e){super(t,e,"tDragStartLite")}}class n extends t{constructor(t,e){super(t,e,"tDragMoveLite")}}class i extends t{constructor(t,e,n){super(t,e,n||"tDragEndLite")}}class s extends i{constructor(t,e){super(t,e,"tDropLite"),this.relativeTargets={prevEl:t._draggingElement.previousElementSibling||null,nextEl:t._draggingElement.nextElementSibling||null}}}window.TSortableLite=class{constructor(t,e){if(!(t instanceof Element))throw new TypeError("TSortable container is not an Element");this._container=t;const n=(e="[object Object]"===Object.prototype.toString.call(e)?e:{}).dragImage||{},i=e.ghost||{},s=e.autoScroll||{};this._config={containerClassName:e.containerClassName||"tsort",dragImage:{className:n.className||"tsort__drag-image",opacity:!isNaN(n.opacity)&&n.opacity>=0&&n.opacity<=1?n.opacity:1,zIndex:isNaN(n.zIndex)?1e4:n.zIndex,appendTo:n.appendTo instanceof Element?n.appendTo:document.body},selector:e.selector||".tsort__item",cancelSelector:e.cancelSelector||".tsort__item_cancel",handleSelector:e.handleSelector||".tsort__item",ghost:{className:i.className||"tsort__ghost"},autoScroll:{isEnabled:void 0===s.isEnabled||!!s.isEnabled,speedIndex:!isNaN(s.speedIndex)&&s.speedIndex>0?s.speedIndex:1,offset:!isNaN(s.offset)&&s.offset>0?s.offset:60},toleranceDistance:!isNaN(e.toleranceDistance)&&e.toleranceDistance>=0?e.toleranceDistance:6,delay:!isNaN(e.delay)&&e.delay>=0?e.delay:50,touchDelay:!isNaN(e.touchDelay)&&e.touchDelay>=0?e.touchDelay:300,touchDistance:!isNaN(e.touchDistance)&&e.touchDistance>=0?e.touchDistance:20,axis:["x","y","both"].includes(e.axis)?e.axis:"both",isAutoUpdateEnabled:!!e.isAutoUpdateEnabled},this._config.autoScroll.container=s.container instanceof Element?s.container:this._getAutoScrollContainer(),this._sortableElements=Array.from(this._container.querySelectorAll(this._config.selector)),this._onStart="function"==typeof e.onStart?e.onStart:function(){},this._onMove="function"==typeof e.onMove?e.onMove:function(){},this._onEnd="function"==typeof e.onEnd?e.onEnd:function(){},this._onDrop="function"==typeof e.onDrop?e.onDrop:function(){},this._handlePointerDown=this._handlePointerDown.bind(this),this._handlePointerUp=this._handlePointerUp.bind(this),this._handlePointerMove=this._handlePointerMove.bind(this),this._handleDragStart=this._handleDragStart.bind(this),this._handleDrop=this._handleDrop.bind(this),this._handleDragMove=this._handleDragMove.bind(this),this._handleDragEnd=this._handleDragEnd.bind(this),this._preventDefaultDrag=this._preventDefaultDrag.bind(this),this._preventContextMenu=this._preventContextMenu.bind(this),this._handleTouchStart=this._handleTouchStart.bind(this),this._handleTouchMove=this._handleTouchMove.bind(this),this._handleTouchEnd=this._handleTouchEnd.bind(this),this._pointerState={isButtonPressed:!1,startPosX:null,startPosY:null,startTimestamp:null,targetElement:null},this._isDropped=!1,this._isDisabled=!1,this._isDragStarted=!1,this._dragImage=null,this._draggingElement=null,this._autoScrollSpeed=0,this._autoScrollAccumulator=0,this._draggingElIndex=null,this._prevSwappedEl=null,this._isTouchDevice=!window.matchMedia("(hover: hover)").matches}_getAutoScrollContainer(){let t=this._container;const e=["auto","scroll","overlay"];for(;t&&t!==document.documentElement;){const n=window.getComputedStyle(t).getPropertyValue("overflow-y");if(e.includes(n))break;t=t.parentElement}return t||document.documentElement}_getDragImage(t){const e=this._config.dragImage,n=t.cloneNode(!0),i=t.getBoundingClientRect();n.classList.add(e.className),n.style.opacity=e.opacity,n.style.zIndex=e.zIndex,n.style.position="fixed",n.style.width=i.width+"px",n.style.height=i.height+"px",n.style.top=i.top+"px",n.style.left=i.left+"px",e.appendTo.appendChild(n);const s=n.querySelectorAll("select"),o=t.querySelectorAll("select");s.forEach(((t,e)=>{const n=o[e];n&&(t.value=n.value)})),this._dragImage=n}_disableSelection(){document.body.style.setProperty("user-select","none"),document.body.style.setProperty("-webkit-user-select","none"),document.documentElement.style.cursor="default",this._config.autoScroll.container.style.setProperty("overflow-anchor","none")}_enableSelection(){document.body.style.removeProperty("user-select"),document.body.style.removeProperty("-webkit-user-select"),document.documentElement.style.removeProperty("cursor"),this._config.autoScroll.container.style.removeProperty("overflow-anchor")}_setAutoScrollSpeed(t){if(this._autoScrollSpeed=t,this._scrollAnimationFrame&&!t)return cancelAnimationFrame(this._scrollAnimationFrame),this._scrollAnimationFrame=null,void(this._autoScrollAccumulator=0);if(!t)return;const e=this._config.autoScroll.container;let n;const i=t=>{if(this._scrollAnimationFrame&&!this._autoScrollSpeed)return cancelAnimationFrame(this._scrollAnimationFrame),this._scrollAnimationFrame=null,void(this._autoScrollAccumulator=0);void 0===n&&(n=t);const s=(t-n)/1e3*this._autoScrollSpeed;this._autoScrollAccumulator+=s,(this._autoScrollSpeed>0&&this._autoScrollAccumulator>=1||this._autoScrollSpeed<0&&this._autoScrollAccumulator<=-1)&&(e.scrollTop+=Math.floor(this._autoScrollAccumulator),this._autoScrollAccumulator=0),n=t,this._scrollAnimationFrame=requestAnimationFrame(i)};this._scrollAnimationFrame=requestAnimationFrame(i)}_enableAutoUpdate(){const t=this._container;this._observer=new MutationObserver((t=>{const e=[];t.forEach((t=>{e.push(...Array.from(t.addedNodes),...Array.from(t.removedNodes))}));for(let t=0;t{t.style.setProperty("user-select","none"),t.style.setProperty("-webkit-touch-callout","none")})),this._config.isAutoUpdateEnabled&&this._enableAutoUpdate()}disable(){this._isDisabled=!0}enable(){this._isDisabled=!1}destroy(){this._container.classList.remove(this._config.containerClassName),this._container.style.removeProperty("position"),this._isTouchDevice?(document.removeEventListener("touchstart",this._handleTouchStart),document.removeEventListener("touchmove",this._handleTouchMove,{passive:!1}),document.removeEventListener("touchend",this._handleTouchEnd),window.removeEventListener("contextmenu",this._preventContextMenu,{passive:!1,capture:!0})):(document.removeEventListener("mousedown",this._handlePointerDown),document.removeEventListener("mousemove",this._handlePointerMove),document.removeEventListener("mouseup",this._handlePointerUp)),this._container.removeEventListener("tDragStartLite",this._handleDragStart),this._container.removeEventListener("tDragMoveLite",this._handleDragMove),this._container.removeEventListener("tDragEndLite",this._handleDragEnd),this._container.removeEventListener("tDropLite",this._handleDrop),document.removeEventListener("dragstart",this._preventDefaultDrag,{passive:!1,capture:!0}),this._isTouchDevice&&this._sortableElements.forEach((t=>{t.style.removeProperty("user-select"),t.style.removeProperty("-webkit-touch-callout")}))}update(){this._sortableElements=Array.from(this._container.querySelectorAll(this._config.selector)),this._isTouchDevice&&this._sortableElements.forEach((t=>{t.style.setProperty("user-select","none"),t.style.setProperty("-webkit-touch-callout","none")}))}_handlePointerDown(t){this._isDisabled||this._isDragStarted||this._pointerState.isButtonPressed||1!==t.buttons||!t.target.closest(this._config.handleSelector)||!this._container.contains(t.target)||t.target.closest(this._config.selector).matches(this._config.cancelSelector)||(this._disableSelection(),this._pointerState.isButtonPressed=!0,this._pointerState.startPosX=t.clientX,this._pointerState.startPosY=t.clientY,this._pointerState.startTimestamp=performance.now(),this._pointerState.targetElement=t.target.closest(this._config.selector),document.addEventListener("mousemove",this._handlePointerMove),document.addEventListener("mouseup",this._handlePointerUp))}_handlePointerMove(t){if(this._isDisabled)return;1!==t.buttons&&(this._pointerState.isButtonPressed=!1);const i=this._pointerState.startPosX||0,s=this._pointerState.startPosY||0,o=t.clientX,a=t.clientY,r=Math.sqrt(Math.pow(o-i,2)+Math.pow(a-s,2)),l=this._pointerState.startTimestamp||0,h=performance.now()-l;if(this._isDragStarted&&1===t.buttons){const e=new n(this,t);this._container.dispatchEvent(e)}else if(!this._isDragStarted&&1===t.buttons&&this._pointerState.isButtonPressed&&r>=this._config.toleranceDistance&&h>=this._config.delay){this._isDragStarted=!0,this._draggingElIndex=this._sortableElements.indexOf(this._pointerState.targetElement);const n=new e(this,t);this._container.dispatchEvent(n)}else;}_handlePointerUp(t){if(!this._isDisabled){if(this._enableSelection(),this._isDragStarted){if(this._draggingElIndex!==this._sortableElements.indexOf(this._pointerState.targetElement)){const e=new s(this,t);this._container.dispatchEvent(e),this._isDropped=!0}const e=new i(this,t);this._container.dispatchEvent(e)}this._pointerState.isButtonPressed=!1,this._pointerState.startPosX=null,this._pointerState.startPosY=null,this._pointerState.startTimestamp=null,this._pointerState.targetElement=null,this._isDragStarted=!1,this._draggingElIndex=null,document.removeEventListener("mousemove",this._handlePointerMove),document.removeEventListener("mouseup",this._handlePointerUp)}}_handleTouchStart(t){this._isDisabled||this._isDragStarted||this._pointerState.isButtonPressed||1!==t.touches.length||!t.target.closest(this._config.handleSelector)||!this._container.contains(t.target)||t.target.closest(this._config.selector).matches(this._config.cancelSelector)||(this._disableSelection(),this._pointerState.isButtonPressed=!0,this._pointerState.startPosX=t.touches[0].clientX,this._pointerState.startPosY=t.touches[0].clientY,this._pointerState.startTimestamp=performance.now(),this._pointerState.targetElement=t.target.closest(this._config.selector),document.addEventListener("touchmove",this._handleTouchMove,{passive:!1}),document.addEventListener("touchend",this._handleTouchEnd))}_handleTouchMove(t){if(!this._isDisabled)if(this._isDragStarted){t.cancelable&&t.preventDefault();const e=new n(this,t);this._container.dispatchEvent(e)}else if(this._pointerState.isButtonPressed){const n=performance.now(),i=this._pointerState.startPosX,s=this._pointerState.startPosY,o=t.changedTouches[0].clientX,a=t.changedTouches[0].clientY,r=Math.sqrt(Math.pow(o-i,2)+Math.pow(a-s,2)),l=n-this._pointerState.startTimestamp;if(t.cancelable&&r=this._config.touchDelay){t.preventDefault(),this._draggingElIndex=this._sortableElements.indexOf(this._pointerState.targetElement),this._isDragStarted=!0;const n=new e(this,t);this._container.dispatchEvent(n)}}}_handleTouchEnd(t){if(!this._isDisabled){if(this._enableSelection(),this._isDragStarted){if(this._draggingElIndex!==this._sortableElements.indexOf(this._pointerState.targetElement)){const e=new s(this,t);this._container.dispatchEvent(e),this._isDropped=!0}const e=new i(this,t);this._container.dispatchEvent(e)}this._pointerState.isButtonPressed=!1,this._pointerState.startPosX=null,this._pointerState.startPosY=null,this._pointerState.startTimestamp=null,this._pointerState.targetElement=null,this._isDragStarted=!1,this._draggingElIndex=null,document.removeEventListener("touchmove",this._handleTouchMove,{passive:!1}),document.removeEventListener("touchend",this._handleTouchEnd)}}_handleDragStart(t){this._onStart.call(null,t),this._isTouchDevice&&"function"==typeof window.navigator.vibrate&&window.navigator.vibrate(10),this._draggingElement=t.draggingElement,this._getDragImage(this._draggingElement);const e=this._config.ghost.className;this._draggingElement.classList.add(e),this._draggingElement.style.pointerEvents="none",this._draggingElement.style.visibility="hidden"}_handleDragMove(t){this._onMove.call(null,t);const e=t.draggingElement,n=this._config.axis,i=this._pointerState.startPosX,s=this._pointerState.startPosY;let o,a;t.originalEvent.type.includes("touch")?(o=t.originalEvent.changedTouches[0].clientX,a=t.originalEvent.changedTouches[0].clientY):(o=t.originalEvent.clientX,a=t.originalEvent.clientY),"both"===n?this._dragImage.style.translate=`${o-i}px ${a-s}px`:"x"===n?this._dragImage.style.translate=o-i+"px 0":"y"===n&&(this._dragImage.style.translate=`0 ${a-s}px`);const r=this._sortableElements.filter((t=>t!==e)),l=r.map((t=>t.getBoundingClientRect()));let h=null;for(let t=0;t=e.left&&o<=e.left+e.width&&a>=e.top&&a<=e.top+e.height||"x"===n&&o>=e.left&&o<=e.left+e.width||"y"===n&&a>=e.top&&a<=e.top+e.height){h=r[t];break}}if(h&&this._prevSwappedEl!==h){let t;t=this._sortableElements.indexOf(h)>this._sortableElements.indexOf(e)?"afterend":"beforebegin",h.insertAdjacentElement(t,e),this._prevSwappedEl=h,this._isTouchDevice&&"function"==typeof window.navigator.vibrate&&window.navigator.vibrate(10),this._config.isAutoUpdateEnabled||this.update()}else h||(this._prevSwappedEl=null);const c=this._config.autoScroll,d=c.container;if(!c.isEnabled)return;const _=d.getBoundingClientRect(),u=Math.max(_.top,0),p=Math.min(_.bottom,document.documentElement.clientHeight),g=a-u-c.offset,m=p-a-c.offset,v=c.speedIndex;g<0?this._setAutoScrollSpeed(v*Math.pow(g,3)/5e3):m<0?this._setAutoScrollSpeed(-1*v*(Math.pow(m,3)/5e3)):this._setAutoScrollSpeed(0)}_handleDrop(t){window.setTimeout(this._onDrop.bind(null,t),0),this._isTouchDevice&&"function"==typeof window.navigator.vibrate&&window.navigator.vibrate(10)}_handleDragEnd(t){this._onEnd.call(null,t),this._setAutoScrollSpeed(0),this._dragImage.remove(),this._dragImage=null,this._draggingElement=null,this._isDropped=!1,this._isDragStarted=!1,this._prevSwappedEl=null;const e=this._config.ghost.className,n=t.draggingElement;n.style.removeProperty("pointer-events"),n.style.removeProperty("visibility"),n.classList.remove(e)}}})();