(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 m(e,t,i){return t.length?[...e].sort((n,o)=>{for(const r of t){const c=i.find(a=>a.field===r.field)?.sortComparator??x,g=n[r.field],p=o[r.field],s=c(g,p,n,o);if(s!==0)return r.direction==="asc"?s:-s}return 0}):[...e]}function x(e,t){return e==null&&t==null?0:e==null?1:t==null?-1:typeof e=="number"&&typeof t=="number"?e-t:e instanceof Date&&t instanceof Date?e.getTime()-t.getTime():typeof e=="boolean"&&typeof t=="boolean"?e===t?0:e?-1:1:String(e).localeCompare(String(t))}function M(e,t,i,n){const o=e.find(r=>r.field===t);return i?o?o.direction==="asc"?e.map(r=>r.field===t?{...r,direction:"desc"}:r):e.filter(r=>r.field!==t):e.length<n?[...e,{field:t,direction:"asc"}]:e:o?.direction==="asc"?[{field:t,direction:"desc"}]:o?.direction==="desc"?[]:[{field:t,direction:"asc"}]}function f(e,t){const i=e.findIndex(n=>n.field===t);return i>=0?i+1:void 0}function h(e,t){return e.find(i=>i.field===t)?.direction}const y='.header-cell[data-sort=asc]:after{content:"↑";margin-left:4px;opacity:.8}.header-cell[data-sort=desc]:after{content:"↓";margin-left:4px;opacity:.8}.sort-index{font-size:10px;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:14px;height:14px;display:inline-flex;align-items:center;justify-content:center;margin-left:2px;font-weight:600}';class S extends l.BaseGridPlugin{name="multiSort";version="1.0.0";get defaultConfig(){return{maxSortColumns:3,showSortIndex:!0}}sortModel=[];detach(){this.sortModel=[]}processRows(t){return this.sortModel.length===0?[...t]:m([...t],this.sortModel,[...this.columns])}onHeaderClick(t){if(!this.columns.find(r=>r.field===t.field)?.sortable)return!1;const n=t.originalEvent.shiftKey,o=this.config.maxSortColumns??3;return this.sortModel=M(this.sortModel,t.field,n,o),this.emit("sort-change",{sortModel:[...this.sortModel]}),this.requestRender(),!0}afterRender(){const t=this.shadowRoot;if(!t)return;const i=this.config.showSortIndex!==!1;t.querySelectorAll(".header-row .cell[data-field]").forEach(o=>{const r=o.getAttribute("data-field");if(!r)return;const u=f(this.sortModel,r),c=h(this.sortModel,r);if(o.querySelector(".sort-index")?.remove(),c){o.querySelector('[part~="sort-indicator"], .sort-indicator')?.remove(),o.setAttribute("data-sort",c);const s=document.createElement("span");if(s.className="sort-indicator",s.style.marginLeft="4px",s.style.opacity="0.8",this.setIcon(s,this.resolveIcon(c==="asc"?"sortAsc":"sortDesc")),o.appendChild(s),i&&this.sortModel.length>1&&u!==void 0){const a=document.createElement("span");a.className="sort-index",a.textContent=String(u),o.appendChild(a)}}else o.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 f(this.sortModel,t)}getSortDirection(t){return h(this.sortModel,t)}getColumnState(t){const i=this.sortModel.findIndex(o=>o.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(r=>r.field!==t);return}const n=this.sortModel.findIndex(r=>r.field===t),o={field:t,direction:i.sort.direction};n!==-1?this.sortModel[n]=o:this.sortModel.splice(i.sort.priority,0,o)}styles=y}d.MultiSortPlugin=S,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
//# sourceMappingURL=multi-sort.umd.js.map
