(function(d,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("../../core/plugin/base-plugin")):typeof define=="function"&&define.amd?define(["exports","../../core/plugin/base-plugin"],c):(d=typeof globalThis<"u"?globalThis:d||self,c(d.TbwGridPlugin_groupingColumns={},d.TbwGrid))})(this,(function(d,c){"use strict";function a(l){if(!l.length)return[];const t=new Map,e=[],r=(i,n)=>{if(!n.length)return;const s=e[e.length-1];if(s&&s.implicit&&s.firstIndex+s.columns.length===i){s.columns.push(...n);return}e.push({id:"__implicit__"+i,label:void 0,columns:n,firstIndex:i,implicit:!0})};let u=[],o=0;return l.forEach((i,n)=>{const s=i.group;if(!s){u.length===0&&(o=n),u.push(i);return}u.length&&(r(o,u.slice()),u=[]);const p=typeof s=="string"?s:s.id;let g=t.get(p);g||(g={id:p,label:typeof s=="string"?void 0:s.label,columns:[],firstIndex:n},t.set(p,g),e.push(g)),g.columns.push(i)}),u.length&&r(o,u),e.length===1&&e[0].implicit&&e[0].columns.length===l.length?[]:e}function f(l,t,e){if(!t.length||!l)return;const r=new Map;for(const o of t)for(const i of o.columns)i?.field&&r.set(i.field,o.id);const u=Array.from(l.querySelectorAll(".cell[data-field]"));u.forEach(o=>{const i=o.getAttribute("data-field")||"",n=r.get(i);n&&(o.classList.add("grouped"),o.getAttribute("data-group")||o.setAttribute("data-group",n))});for(const o of t){const i=o.columns[o.columns.length-1],n=u.find(s=>s.getAttribute("data-field")===i.field);n&&n.classList.add("group-end")}}function h(l,t){if(l.length===0)return null;const e=document.createElement("div");e.className="header-group-row",e.setAttribute("role","row");for(const r of l){const u=r.firstIndex!=null?r.firstIndex:t.findIndex(s=>r.columns.includes(s)),o=String(r.id).startsWith("__implicit__"),i=o?"":r.label||r.id,n=document.createElement("div");n.className="cell header-group-cell",o&&n.classList.add("implicit-group"),n.setAttribute("data-group",String(r.id)),n.style.gridColumn=`${u+1} / span ${r.columns.length}`,n.textContent=i,e.appendChild(n)}return e}function m(l){return l.some(t=>t.group!=null)}const b=".header-group-row{display:grid;grid-auto-flow:column;background:var(--tbw-grouping-columns-header-bg, var(--tbw-color-header-bg));border-bottom:1px solid var(--tbw-grouping-columns-border, var(--tbw-color-border))}.header-group-cell{display:flex;align-items:center;justify-content:center;padding:4px 8px;font-weight:600;font-size:.9em;text-transform:uppercase;letter-spacing:.5px;border-right:1px solid var(--tbw-grouping-columns-border, var(--tbw-color-border))}.header-group-cell:last-child{border-right:none}.header-row .cell.grouped{border-top:none}.header-row .cell.group-end{border-right:2px solid var(--tbw-grouping-columns-separator, var(--tbw-color-border-strong))}";class w extends c.BaseGridPlugin{name="groupingColumns";version="1.0.0";get defaultConfig(){return{showGroupBorders:!0}}groups=[];isActive=!1;detach(){this.groups=[],this.isActive=!1}static detect(t,e){const r=e?.columns;return Array.isArray(r)?m(r):!1}processColumns(t){const e=a(t);return e.length===0?(this.isActive=!1,this.groups=[],[...t]):(this.isActive=!0,this.groups=e,[...t])}afterRender(){if(!this.isActive||this.groups.length===0){const i=this.shadowRoot?.querySelector(".header")?.querySelector(".header-group-row");i&&i.remove();return}const t=this.shadowRoot?.querySelector(".header");if(!t)return;const e=t.querySelector(".header-group-row");e&&e.remove();const r=h(this.groups,this.columns);if(r){const o=t.querySelector(".header-row");o?t.insertBefore(r,o):t.appendChild(r)}const u=t.querySelector(".header-row");u&&f(u,this.groups,this.columns)}isGroupingActive(){return this.isActive}getGroups(){return this.groups}getGroupColumns(t){const e=this.groups.find(r=>r.id===t);return e?e.columns:[]}refresh(){this.requestRender()}styles=b}d.GroupingColumnsPlugin=w,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
//# sourceMappingURL=grouping-columns.umd.js.map
