(function(a,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("../../core/plugin/base-plugin")):typeof define=="function"&&define.amd?define(["exports","../../core/plugin/base-plugin"],l):(a=typeof globalThis<"u"?globalThis:a||self,l(a.TbwGridPlugin_reorder={},a.TbwGrid))})(this,(function(a,l){"use strict";function m(s){const r=s.meta??{};return r.lockPosition!==!0&&r.suppressMovable!==!0}function f(s,r,d){if(r===d||r<0||r>=s.length||d<0||d>s.length)return s;const n=[...s],[e]=n.splice(r,1);return n.splice(d,0,e),n}const b='.header-row>.cell[draggable=true]{cursor:grab;position:relative}.header-row>.cell.dragging{opacity:.5;cursor:grabbing}.header-row>.cell.drop-before:before{content:"";position:absolute;left:0;top:0;bottom:0;width:2px;background:var(--tbw-reorder-indicator, var(--tbw-color-accent));z-index:1}.header-row>.cell.drop-after:after{content:"";position:absolute;right:0;top:0;bottom:0;width:2px;background:var(--tbw-reorder-indicator, var(--tbw-color-accent));z-index:1}';class v extends l.BaseGridPlugin{name="reorder";version="1.0.0";get defaultConfig(){return{animation:!0,animationDuration:200}}isDragging=!1;draggedField=null;draggedIndex=null;dropIndex=null;attach(r){super.attach(r),r.addEventListener("column-reorder-request",d=>{const n=d.detail;n?.field&&typeof n.toIndex=="number"&&this.moveColumn(n.field,n.toIndex)},{signal:this.disconnectSignal})}detach(){this.isDragging=!1,this.draggedField=null,this.draggedIndex=null,this.dropIndex=null}afterRender(){const r=this.shadowRoot;if(!r)return;r.querySelectorAll(".header-row > .cell").forEach(n=>{const e=n,i=e.getAttribute("data-field");if(!i)return;const h=this.columns.find(t=>t.field===i),O=!this.grid.queryPlugins({type:l.PLUGIN_QUERIES.CAN_MOVE_COLUMN,context:h}).includes(!1);if(!h||!m(h)||!O){e.draggable=!1;return}e.draggable=!0,!e.getAttribute("data-dragstart-bound")&&(e.setAttribute("data-dragstart-bound","true"),e.addEventListener("dragstart",t=>{const o=this.getColumnOrder().indexOf(i);this.isDragging=!0,this.draggedField=i,this.draggedIndex=o,t.dataTransfer&&(t.dataTransfer.effectAllowed="move",t.dataTransfer.setData("text/plain",i)),e.classList.add("dragging")}),e.addEventListener("dragend",()=>{this.isDragging=!1,this.draggedField=null,this.draggedIndex=null,this.dropIndex=null,r.querySelectorAll(".header-row > .cell").forEach(t=>{t.classList.remove("dragging","drop-target","drop-before","drop-after")})}),e.addEventListener("dragover",t=>{if(t.preventDefault(),!this.isDragging||this.draggedField===i)return;const g=e.getBoundingClientRect(),o=g.left+g.width/2,c=this.getColumnOrder().indexOf(i);this.dropIndex=t.clientX<o?c:c+1,e.classList.add("drop-target"),e.classList.toggle("drop-before",t.clientX<o),e.classList.toggle("drop-after",t.clientX>=o)}),e.addEventListener("dragleave",()=>{e.classList.remove("drop-target","drop-before","drop-after")}),e.addEventListener("drop",t=>{t.preventDefault();const g=this.draggedField,o=this.draggedIndex,u=this.dropIndex;if(!this.isDragging||g===null||o===null||u===null)return;const c=u>o?u-1:u,x=this.getColumnOrder(),p=f(x,o,c),C={field:g,fromIndex:o,toIndex:c,columnOrder:p};this.grid.setColumnOrder(p),this.emit("column-move",C),this.grid.requestStateChange?.()}))})}getColumnOrder(){return this.grid.getColumnOrder()}moveColumn(r,d){const n=this.getColumnOrder(),e=n.indexOf(r);if(e===-1)return;const i=f(n,e,d);this.grid.setColumnOrder(i),this.emit("column-move",{field:r,fromIndex:e,toIndex:d,columnOrder:i}),this.grid.requestStateChange?.()}setColumnOrder(r){this.grid.setColumnOrder(r),this.grid.requestStateChange?.()}resetColumnOrder(){const r=this.columns.map(d=>d.field);this.grid.setColumnOrder(r),this.grid.requestStateChange?.()}styles=b}a.ReorderPlugin=v,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})}));
//# sourceMappingURL=reorder.umd.js.map
