(function(d,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):(d=typeof globalThis<"u"?globalThis:d||self,l(d.TbwGridPlugin_multiSort={},d.TbwGrid))})(this,(function(d,l){"use strict";function b(r,t,i){return t.length?[...r].sort((n,e)=>{for(const o of t){const c=i.find(u=>u.field===o.field)?.sortComparator??S,m=n[o.field],p=e[o.field],s=c(m,p,n,e);if(s!==0)return o.direction==="asc"?s:-s}return 0}):[...r]}function S(r,t){return r==null&&t==null?0:r==null?1:t==null?-1:typeof r=="number"&&typeof t=="number"?r-t:r instanceof Date&&t instanceof Date?r.getTime()-t.getTime():typeof r=="boolean"&&typeof t=="boolean"?r===t?0:r?-1:1:String(r).localeCompare(String(t))}function y(r,t,i,n){const e=r.find(o=>o.field===t);return i?e?e.direction==="asc"?r.map(o=>o.field===t?{...o,direction:"desc"}:o):r.filter(o=>o.field!==t):r.length<n?[...r,{field:t,direction:"asc"}]:r:e?.direction==="asc"?[{field:t,direction:"desc"}]:e?.direction==="desc"?[]:[{field:t,direction:"asc"}]}function g(r,t){const i=r.findIndex(n=>n.field===t);return i>=0?i+1:void 0}function h(r,t){return r.find(i=>i.field===t)?.direction}const M='@layer tbw-plugins{.header-cell[data-sort=asc]:after{content:"↑";margin-left:var(--tbw-spacing-xs, .25em);opacity:.8}.header-cell[data-sort=desc]:after{content:"↓";margin-left:var(--tbw-spacing-xs, .25em);opacity:.8}.sort-indicator{margin-left:var(--tbw-spacing-xs, .25em);opacity:.8}.sort-index{font-size:var(--tbw-font-size-2xs, .7em);background:var(--tbw-multi-sort-badge-bg, var(--tbw-color-panel-bg));color:var(--tbw-multi-sort-badge-color, var(--tbw-color-fg));border-radius:50%;width:var(--tbw-multi-sort-badge-size, 1em);height:var(--tbw-multi-sort-badge-size, 1em);display:inline-flex;align-items:center;justify-content:center;margin-left:var(--tbw-spacing-xs, .125em);font-weight:600}}';class w extends l.BaseGridPlugin{name="multiSort";styles=M;get defaultConfig(){return{maxSortColumns:3,showSortIndex:!0}}sortModel=[];detach(){this.sortModel=[]}processRows(t){return this.sortModel.length===0?[...t]:b([...t],this.sortModel,[...this.columns])}onHeaderClick(t){if(!this.columns.find(o=>o.field===t.field)?.sortable)return!1;const n=t.originalEvent.shiftKey,e=this.config.maxSortColumns??3;return this.sortModel=y(this.sortModel,t.field,n,e),this.emit("sort-change",{sortModel:[...this.sortModel]}),this.requestRender(),!0}afterRender(){const t=this.gridElement;if(!t)return;const i=this.config.showSortIndex!==!1;t.querySelectorAll(".header-row .cell[data-field]").forEach(e=>{const o=e.getAttribute("data-field");if(!o)return;const f=g(this.sortModel,o),c=h(this.sortModel,o);if(e.querySelector(".sort-index")?.remove(),c){e.querySelector('[part~="sort-indicator"], .sort-indicator')?.remove(),e.setAttribute("data-sort",c);const s=document.createElement("span");s.className="sort-indicator",this.setIcon(s,this.resolveIcon(c==="asc"?"sortAsc":"sortDesc"));const u=e.querySelector(".tbw-filter-btn"),v=e.querySelector(".resize-handle"),x=u??v;if(x?e.insertBefore(s,x):e.appendChild(s),i&&this.sortModel.length>1&&f!==void 0){const a=document.createElement("span");a.className="sort-index",a.textContent=String(f),s.nextSibling?e.insertBefore(a,s.nextSibling):e.appendChild(a)}}else e.removeAttribute("data-sort")})}getSortModel(){return[...this.sortModel]}setSortModel(t){this.sortModel=[...t],this.emit("sort-change",{sortModel:[...t]}),this.requestRender()}clearSort(){this.sortModel=[],this.emit("sort-change",{sortModel:[]}),this.requestRender()}getSortIndex(t){return g(this.sortModel,t)}getSortDirection(t){return h(this.sortModel,t)}getColumnState(t){const i=this.sortModel.findIndex(e=>e.field===t);return i===-1?void 0:{sort:{direction:this.sortModel[i].direction,priority:i}}}applyColumnState(t,i){if(!i.sort){this.sortModel=this.sortModel.filter(o=>o.field!==t);return}const n=this.sortModel.findIndex(o=>o.field===t),e={field:t,direction:i.sort.direction};n!==-1?this.sortModel[n]=e:this.sortModel.splice(i.sort.priority,0,e)}}d.MultiSortPlugin=w,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
//# sourceMappingURL=multi-sort.umd.js.map
