@taskctrl/canvas-grid 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const x=require("react/jsx-runtime"),g=require("react"),pn=require("react-dom");class pt{constructor(e){this.scrollX=0,this.scrollY=0,this.canvasWidth=0,this.canvasHeight=0,this.rowHeight=32,this.pinnedWidth=0,this._columnOffsets=[],this._columnWidths=[],this._pinnedOffsets=[],this._pinnedWidths=[],this._scrollableOffsets=[],this._scrollableWidths=[],e&&this.update(e)}update(e){e.scrollX!==void 0&&(this.scrollX=e.scrollX),e.scrollY!==void 0&&(this.scrollY=e.scrollY),e.canvasWidth!==void 0&&(this.canvasWidth=e.canvasWidth),e.canvasHeight!==void 0&&(this.canvasHeight=e.canvasHeight),e.rowHeight!==void 0&&(this.rowHeight=e.rowHeight)}setColumnOffsets(e){this._columnOffsets=e}setColumnWidths(e){this._columnWidths=e}setPinnedOffsets(e,o){this._pinnedOffsets=e,this._pinnedWidths=o}setScrollableOffsets(e,o){this._scrollableOffsets=e,this._scrollableWidths=o}rowToY(e){return e*this.rowHeight-this.scrollY}yToRow(e){return Math.floor((e+this.scrollY)/this.rowHeight)}colToX(e,o){return o===!0?this._pinnedOffsets[e]??0:o===!1?(this._scrollableOffsets[e]??0)-this.scrollX+this.pinnedWidth:(this._columnOffsets[e]??0)-this.scrollX}xToColPinned(e){for(let n=0;n<this._pinnedOffsets.length;n++){const r=this._pinnedOffsets[n],c=this._pinnedWidths[n]??0;if(e>=r&&e<r+c)return{index:n,pinned:!0}}const o=e-this.pinnedWidth+this.scrollX;for(let n=0;n<this._scrollableOffsets.length;n++){const r=this._scrollableOffsets[n],c=this._scrollableWidths[n]??0;if(o>=r&&o<r+c)return{index:n,pinned:!1}}return null}xToCol(e){const o=e+this.scrollX;for(let n=0;n<this._columnOffsets.length;n++){const r=this._columnOffsets[n],c=this._columnWidths[n]??0;if(o>=r&&o<r+c)return n}return-1}}const Vt=["grid","cells","overlay"];class mn{constructor(e){this._dirty={grid:!1,cells:!1,overlay:!1},this._rafId=null,this._drawFn=e}markDirty(e){this._dirty[e]=!0,this._rafId===null&&(this._rafId=requestAnimationFrame(()=>this._flush()))}markAllDirty(){for(const e of Vt)this._dirty[e]=!0;this._rafId===null&&(this._rafId=requestAnimationFrame(()=>this._flush()))}destroy(){this._rafId!==null&&(cancelAnimationFrame(this._rafId),this._rafId=null)}_flush(){this._rafId=null;for(const e of Vt)this._dirty[e]&&(this._dirty[e]=!1,this._drawFn(e))}}class Kt{constructor(e,o){this._rows=e,this._expandedRows=o,this._depthMap=new Map,this._childrenMap=new Map,this._buildMaps(),this._flattenedRows=this._buildFlattenedRows()}_buildMaps(){for(const n of this._rows)this._childrenMap.has(n.id)||this._childrenMap.set(n.id,[]),n.parentId!==void 0&&(this._childrenMap.has(n.parentId)||this._childrenMap.set(n.parentId,[]),this._childrenMap.get(n.parentId).push(n));const o=this._rows.filter(n=>n.parentId===void 0).map(n=>({row:n,depth:0}));for(;o.length>0;){const{row:n,depth:r}=o.pop();this._depthMap.set(n.id,r);const c=this._childrenMap.get(n.id)??[];for(const a of c)o.push({row:a,depth:r+1})}}_buildFlattenedRows(){const e=[],o=this._rows.filter(r=>r.parentId===void 0),n=r=>{if(e.push(r),this._expandedRows.has(r.id)){const c=this._childrenMap.get(r.id)??[];for(const a of c)n(a)}};for(const r of o)n(r);return e}get flattenedRows(){return this._flattenedRows}getDepth(e){return this._depthMap.get(e)??0}hasChildren(e){const o=this._childrenMap.get(e);return o!==void 0&&o.length>0}isExpanded(e){return this._expandedRows.has(e)}toggle(e){const o=new Set(this._expandedRows);return o.has(e)?o.delete(e):o.add(e),o}getVisibleRange(e,o,n){if(n<=0||this._flattenedRows.length===0)return{startIndex:0,endIndex:0};const r=Math.max(0,Math.floor(e/n)),c=Math.min(this._flattenedRows.length-1,Math.ceil((e+o)/n)-1);return{startIndex:r,endIndex:c}}}const wn=30;class Te{constructor(e){this._columns=e}getVisibleColumns(){return this._columns.filter(e=>!e.hidden)}getPinnedColumns(){return this._columns.filter(e=>!e.hidden&&e.pinned==="left")}getScrollableColumns(){return this._columns.filter(e=>!e.hidden&&e.pinned!=="left")}getPinnedWidth(){return this.getPinnedColumns().reduce((e,o)=>e+o.width,0)}getAllColumns(){return[...this._columns]}getTotalWidth(){return this.getVisibleColumns().reduce((e,o)=>e+o.width,0)}getColumnX(e){const o=this.getPinnedColumns(),n=this.getScrollableColumns();let r=0;for(const c of o){if(c.id===e)return r;r+=c.width}for(const c of n){if(c.id===e)return r;r+=c.width}return-1}getColumnAtX(e){let o=0;for(const n of this.getPinnedColumns()){if(e>=o&&e<o+n.width)return n.id;o+=n.width}for(const n of this.getScrollableColumns()){if(e>=o&&e<o+n.width)return n.id;o+=n.width}return null}getVisibleRange(e,o){const n=this.getScrollableColumns();if(n.length===0)return{startIndex:0,endIndex:0};const r=this.getPinnedWidth(),c=o-r;let a=0,f=0,u=n.length-1;for(let M=0;M<n.length;M++)a+n[M].width<=e&&(f=M+1),a<e+c&&(u=M),a+=n[M].width;return f=Math.min(f,n.length-1),u=Math.min(u,n.length-1),{startIndex:f,endIndex:u}}getPinnedRange(){const e=this.getPinnedColumns();return e.length===0?{startIndex:0,endIndex:-1}:{startIndex:0,endIndex:e.length-1}}resize(e,o){const n=this._columns.map(r=>{if(r.id!==e)return r;const c=r.minWidth??wn;return{...r,width:Math.max(c,o)}});return new Te(n)}reorder(e,o){const n=[...this._columns],r=n.findIndex(f=>f.id===e);if(r===-1)return new Te(n);const[c]=n.splice(r,1),a=Math.max(0,Math.min(o,n.length));return n.splice(a,0,c),new Te(n)}hide(e){const o=this._columns.map(n=>n.id===e?{...n,hidden:!0}:n);return new Te(o)}show(e){const o=this._columns.map(n=>n.id===e?{...n,hidden:!1}:n);return new Te(o)}}function Pe(t,e){return`${t}|${e}`}function ft(){return{cells:new Set,rows:new Set,columns:new Set,anchor:void 0}}class Ut{constructor(e){e?this._selection={cells:new Set(e.cells),rows:new Set(e.rows),columns:new Set(e.columns),anchor:e.anchor}:this._selection=ft()}click(e,o,n,r){this._selection=ft(),this._selection.cells.add(Pe(e,o)),this._selection.anchor={rowId:e,colId:o}}ctrlClick(e,o){const n=Pe(e,o);this._selection.cells.has(n)?this._selection.cells.delete(n):this._selection.cells.add(n),this._selection.anchor={rowId:e,colId:o}}shiftClick(e,o,n,r){const c=this._selection.anchor;if(!c){this.click(e,o,n,r);return}const a=n.indexOf(c.rowId),f=n.indexOf(e),u=r.indexOf(c.colId),M=r.indexOf(o),H=Math.min(a,f),O=Math.max(a,f),I=Math.min(u,M),D=Math.max(u,M),X=new Set;for(let B=H;B<=O;B++)for(let L=I;L<=D;L++)X.add(Pe(n[B],r[L]));this._selection.cells=X,this._selection.rows=new Set,this._selection.columns=new Set}selectRow(e,o){for(const n of o)this._selection.cells.add(Pe(e,n));this._selection.rows.add(e),this._selection.anchor={rowId:e,colId:o[0]??""}}selectColumn(e,o){for(const n of o)this._selection.cells.add(Pe(n,e));this._selection.columns.add(e),this._selection.anchor={rowId:o[0]??"",colId:e}}clear(){this._selection=ft()}isSelected(e,o){return this._selection.cells.has(Pe(e,o))}isRowSelected(e){return this._selection.rows.has(e)}isColumnSelected(e){return this._selection.columns.has(e)}getSelection(){return{cells:new Set(this._selection.cells),rows:new Set(this._selection.rows),columns:new Set(this._selection.columns),anchor:this._selection.anchor}}}function ot(t,e,o,n,r){const c=o.yToRow(e),a=n.flattenedRows;if(c<0||c>=a.length)return null;const f=r.getPinnedColumns(),u=r.getPinnedWidth();if(f.length>0){if(t<u){let D=0;for(const X of f){if(t>=D&&t<D+X.width)return{rowId:a[c].id,colId:X.id};D+=X.width}return null}const H=t-u+o.scrollX,O=r.getScrollableColumns();let I=0;for(const D of O){if(H>=I&&H<I+D.width)return{rowId:a[c].id,colId:D.id};I+=D.width}return null}const M=r.getColumnAtX(t+o.scrollX);return M===null?null:{rowId:a[c].id,colId:M}}function bn(t,e,o){const n=window.devicePixelRatio||1;(t.width!==Math.round(e*n)||t.height!==Math.round(o*n))&&(t.width=Math.round(e*n),t.height=Math.round(o*n),t.style.width=`${e}px`,t.style.height=`${o}px`);const c=t.getContext("2d");return c.setTransform(n,0,0,n,0,0),c}function Nt(t,e,o,n,r={}){const{canvasWidth:c,canvasHeight:a,rowHeight:f}=e,{rowStyle:u}=r;t.clearRect(0,0,c,a),t.fillStyle="#f3f4f6",t.fillRect(0,0,c,a);const M=o.getVisibleRange(e.scrollY,a,f),H=o.flattenedRows,O=n.getPinnedColumns(),I=n.getScrollableColumns(),D=n.getPinnedWidth(),X=O.length>0;if(H.length===0||O.length===0&&I.length===0)return;const B=n.getTotalWidth(),L=Math.min(c,B-e.scrollX+(X?e.scrollX:0)),Y=X?Math.min(c,D+n.getScrollableColumns().reduce((S,b)=>S+b.width,0)-e.scrollX):L,Q=H.length*f,z=Math.min(a,Q-e.scrollY);for(let S=M.startIndex;S<=M.endIndex;S++){const b=H[S];if(!b)continue;const W=e.rowToY(S),m=o.getDepth(b.id);let v=S%2===0?"#ffffff":"#f9fafb";if(u){const T=u(b,m);T!=null&&T.backgroundColor&&(v=T.backgroundColor)}t.fillStyle=v,t.fillRect(0,W,Math.max(Y,D),f),t.strokeStyle="#d1d5db",t.lineWidth=.5,t.beginPath(),t.moveTo(0,W+f),t.lineTo(Math.max(Y,D),W+f),t.stroke()}if(z>0){const S=n.getVisibleRange(e.scrollX,c);t.strokeStyle="#d1d5db",t.lineWidth=.5;for(let b=S.startIndex;b<=S.endIndex;b++){const W=I[b];if(!W)continue;const m=e.colToX(b,!1);t.beginPath(),t.moveTo(m+W.width,0),t.lineTo(m+W.width,z),t.stroke()}}if(X&&z>0){t.fillStyle="#ffffff",t.fillRect(0,0,D,a);for(let b=M.startIndex;b<=M.endIndex;b++){const W=H[b];if(!W)continue;const m=e.rowToY(b),v=o.getDepth(W.id);let T=b%2===0?"#ffffff":"#f9fafb";if(u){const k=u(W,v);k!=null&&k.backgroundColor&&(T=k.backgroundColor)}t.fillStyle=T,t.fillRect(0,m,D,f),t.strokeStyle="#d1d5db",t.lineWidth=.5,t.beginPath(),t.moveTo(0,m+f),t.lineTo(D,m+f),t.stroke()}const S=n.getPinnedRange();t.strokeStyle="#d1d5db",t.lineWidth=.5;for(let b=S.startIndex;b<=S.endIndex;b++){const W=O[b];if(!W)continue;const m=e.colToX(b,!0);t.beginPath(),t.moveTo(m+W.width,0),t.lineTo(m+W.width,z),t.stroke()}t.strokeStyle="#d1d5db",t.lineWidth=1,t.beginPath(),t.moveTo(D,0),t.lineTo(D,z),t.stroke()}}function qt(t){function e(a,f,u,M,H,O){t.fillStyle=O,t.beginPath(),t.roundRect(a,f,u,M,H),t.fill()}function o(a,f,u,M){t.fillText(a,f,u,M)}function n(a,f){if(t.measureText(a).width<=f)return a;let u=a;for(;u.length>0;)if(u=u.slice(0,-1),t.measureText(u+"…").width<=f)return u+"…";return"…"}function r(a,f,u,M,H){const X=t.measureText(u).width+16;return e(a,f,X,20,20/2,M),t.fillStyle=H,t.fillText(u,a+8,f+20/2),X}function c(a,f,u,M,H,O){e(a,f,u,M,M/2,"#d1d5db");const I=u*Math.max(0,Math.min(1,H));I>0&&e(a,f,I,M,M/2,O)}return{roundRect:e,fillText:o,truncateText:n,badge:r,progressBar:c}}function Zt(t,e,o,n,r,c){const{canvasWidth:a,canvasHeight:f,rowHeight:u}=e,{cellRenderer:M,getCellData:H,hoveredCell:O}=c;c.clearBackground!==!1&&t.clearRect(0,0,a,f);const I=o.getVisibleRange(e.scrollY,f,u),D=o.flattenedRows,X=n.getPinnedColumns(),B=n.getScrollableColumns(),L=X.length>0;if(D.length===0||X.length===0&&B.length===0)return;const Y=qt(t),Q=(S,b,W,m)=>{const v={x:W,y:m,width:b.width,height:u},T={rowId:S.id,colId:b.id,data:H(S.id,b.id)},k={selected:r.isSelected(S.id,b.id),hovered:O!=null&&O.rowId===S.id&&O.colId===b.id,rowSelected:r.isRowSelected(S.id),colSelected:r.isColumnSelected(b.id),filtered:!0,depth:o.getDepth(S.id),hasChildren:o.hasChildren(S.id),expanded:o.isExpanded(S.id)};t.save(),t.beginPath(),t.rect(W,m,b.width,u),t.clip(),M(t,T,v,k,Y),t.restore()},z=n.getVisibleRange(e.scrollX,a);for(let S=I.startIndex;S<=I.endIndex;S++){const b=D[S];if(!b)continue;const W=e.rowToY(S);for(let m=z.startIndex;m<=z.endIndex;m++){const v=B[m];if(!v)continue;const T=e.colToX(m,L?!1:void 0);Q(b,v,T,W)}}if(L){n.getPinnedWidth();const S=n.getPinnedRange();for(let b=I.startIndex;b<=I.endIndex;b++){const W=D[b];if(!W)continue;const m=e.rowToY(b);t.save(),t.fillStyle="#ffffff",t.restore();for(let v=S.startIndex;v<=S.endIndex;v++){const T=X[v];if(!T)continue;const k=e.colToX(v,!0);Q(W,T,k,m)}}}}function vn(t,e,o,n,r,c={}){const{canvasWidth:a,canvasHeight:f,rowHeight:u}=e,{hoveredCell:M,hoveredRowId:H,selectedCell:O,resizingColumn:I,loading:D}=c;t.clearRect(0,0,a,f);const X=o.getVisibleRange(e.scrollY,f,u),B=o.flattenedRows,L=n.getPinnedColumns(),Y=n.getScrollableColumns(),Q=L.length>0;if(n.getPinnedWidth(),B.length===0||L.length===0&&Y.length===0)return;const z=n.getVisibleRange(e.scrollX,a),S=n.getPinnedRange(),b=m=>{for(let v=z.startIndex;v<=z.endIndex;v++){const T=Y[v];if(!T)continue;const k=Q?e.colToX(v,!1):e.colToX(v);m(T,k)}if(Q)for(let v=S.startIndex;v<=S.endIndex;v++){const T=L[v];if(!T)continue;const k=e.colToX(v,!0);m(T,k)}};if(O){for(let m=X.startIndex;m<=X.endIndex;m++){const v=B[m];if(!v||v.id!==O.rowId)continue;const T=e.rowToY(m);b((k,N)=>{t.fillStyle="rgba(59,130,246,0.06)",t.fillRect(N,T,k.width,u)});break}b((m,v)=>{if(m.id===O.colId)for(let T=X.startIndex;T<=X.endIndex;T++){if(!B[T])continue;const N=e.rowToY(T);t.fillStyle="rgba(59,130,246,0.06)",t.fillRect(v,N,m.width,u)}})}const W=(m,v,T,k,N)=>{const i=m.width,oe=u,_e=r.isSelected(T.id,m.id),ye=r.isRowSelected(T.id),d=r.isColumnSelected(m.id),P=H!=null&&H===T.id,$=M!=null&&M.rowId===T.id&&M.colId===m.id;_e?(t.fillStyle="rgba(59,130,246,0.12)",t.fillRect(v,k,i,oe),t.strokeStyle="#3b82f6",t.lineWidth=2,t.strokeRect(v+1,k+1,i-2,oe-2)):ye||d||$?(t.fillStyle="rgba(59,130,246,0.08)",t.fillRect(v,k,i,oe)):P&&(t.fillStyle="rgba(59,130,246,0.04)",t.fillRect(v,k,i,oe))};for(let m=X.startIndex;m<=X.endIndex;m++){const v=B[m];if(!v)continue;const T=e.rowToY(m);for(let k=z.startIndex;k<=z.endIndex;k++){const N=Y[k];if(!N)continue;const i=Q?e.colToX(k,!1):e.colToX(k);W(N,i,v,T)}}if(Q)for(let m=X.startIndex;m<=X.endIndex;m++){const v=B[m];if(!v)continue;const T=e.rowToY(m);for(let k=S.startIndex;k<=S.endIndex;k++){const N=L[k];if(!N)continue;const i=e.colToX(k,!0);W(N,i,v,T)}}if(I&&(t.save(),t.strokeStyle="#3b82f6",t.lineWidth=1,t.setLineDash([4,4]),t.beginPath(),t.moveTo(I.x,0),t.lineTo(I.x,f),t.stroke(),t.setLineDash([]),t.restore()),D){const v=f-32;t.fillStyle="#f3f4f6",t.fillRect(0,v,a,32),t.strokeStyle="#e5e7eb",t.lineWidth=1,t.beginPath(),t.moveTo(0,v),t.lineTo(a,v),t.stroke(),t.fillStyle="#6b7280",t.font="13px system-ui, sans-serif",t.textAlign="center",t.textBaseline="middle",t.fillText("Loading...",a/2,v+32/2)}}function Cn({treeEngine:t,width:e,rowHeight:o,scrollTop:n,viewportHeight:r,sidebarRenderer:c,onToggle:a,onRowClick:f,hoveredRowId:u,onRowHover:M}){const H=t.flattenedRows,{startIndex:O,endIndex:I}=t.getVisibleRange(n,r,o),D=[];for(let L=O;L<=I;L++){const Y=H[L];if(!Y)continue;const Q=t.getDepth(Y.id),z=t.hasChildren(Y.id),S=t.isExpanded(Y.id),b=L*o;let W;if(c)W=c(Y,Q,S,z);else{const v=Q*16+8;W=x.jsxs("div",{style:{display:"flex",alignItems:"center",height:"100%",paddingLeft:v,overflow:"hidden",whiteSpace:"nowrap",cursor:z?"pointer":"default",userSelect:"none"},onPointerDown:z?T=>{T.stopPropagation(),a(Y.id)}:void 0,children:[z?x.jsx("span",{style:{marginRight:4,flexShrink:0,fontSize:10,color:"#6b7280"},children:S?"▾":"▸"}):x.jsx("span",{style:{width:14,flexShrink:0}}),x.jsx("span",{style:{overflow:"hidden",textOverflow:"ellipsis",fontSize:12},children:String(Y.title??Y.id)})]})}const m=u===Y.id;D.push(x.jsx("div",{role:"rowheader","aria-level":Q+1,"aria-expanded":z?S:void 0,style:{position:"absolute",top:b,left:0,width:"100%",height:o,boxSizing:"border-box",fontSize:13,color:"#374151",borderBottom:"1px solid #e5e7eb",cursor:f?"pointer":"default",backgroundColor:m?"rgba(59,130,246,0.04)":void 0},onPointerDown:f?v=>f(Y.id,v):void 0,onPointerEnter:()=>M==null?void 0:M(Y.id),onPointerLeave:()=>M==null?void 0:M(null),children:W},String(Y.id)))}const X=H.length*o,B=Math.min(r,X-n);return x.jsxs("div",{style:{width:e,height:r,overflow:"hidden",position:"relative",flexShrink:0,backgroundColor:"#ffffff"},children:[x.jsx("div",{style:{position:"absolute",top:0,right:0,width:1,height:Math.max(0,B),backgroundColor:"#e5e7eb",zIndex:1}}),x.jsx("div",{style:{position:"absolute",top:0,left:0,width:"100%",height:X,transform:`translateY(-${n}px)`},children:D})]})}const ut=28,gt=28,xn=4;function yn(t){switch(t){case"vertical":return 120;default:return 32}}function Rn({active:t}){return x.jsx("svg",{width:13,height:13,viewBox:"0 0 24 24",fill:"none",stroke:t?"#2563eb":"#9ca3af",strokeWidth:2,strokeLinecap:"round",strokeLinejoin:"round","aria-hidden":"true",children:x.jsx("polygon",{points:"22 3 2 3 10 12.46 10 19 14 21 14 12.46 22 3"})})}function In({columnEngine:t,columnGroups:e,scrollX:o,orientation:n="horizontal",columnHeaderHeight:r,onColumnClick:c,onColumnResize:a,onColumnReorder:f,onColumnFilterClick:u,columnHeaderRenderer:M}){const H=t.getPinnedColumns(),O=t.getScrollableColumns(),I=t.getVisibleColumns(),D=t.getPinnedWidth(),X=new Map;if(e)for(const d of e)X.set(d.id,d);const B=g.useRef(null),[L,Y]=g.useState(null),Q=g.useRef(null),z=g.useRef(!1),S=g.useRef(null),b=g.useRef(null);Q.current=L;const W=[...H,...O],m=[];if(e&&e.length>0){let d,P=0,$=0,V=!1;for(const G of W){const U=t.getColumnX(G.id),F=G.pinned==="left";if(G.group!==d){if(d!==void 0&&$>0){const le=X.get(d);le&&m.push({group:le,startX:P,width:$,pinned:V})}d=G.group,P=U,$=G.width,V=F}else $+=G.width}if(d!==void 0&&$>0){const G=X.get(d);G&&m.push({group:G,startX:P,width:$,pinned:V})}}const v=m.length>0,T=r??yn(n),k=v?ut+T:T,N=g.useCallback((d,P)=>{P.stopPropagation(),P.preventDefault();const $=I.find(V=>V.id===d);$&&(B.current={colId:d,startX:P.clientX,startWidth:$.width})},[I]);g.useEffect(()=>{const d=$=>{const V=B.current;if(!V)return;const G=$.clientX-V.startX,U=Math.max(V.startWidth+G,30);a==null||a(V.colId,U)},P=()=>{B.current=null};return document.addEventListener("mousemove",d),document.addEventListener("mouseup",P),()=>{document.removeEventListener("mousemove",d),document.removeEventListener("mouseup",P)}},[a]);const i=g.useCallback(d=>{const P=b.current;if(!P)return{dropIndex:0};const $=P.getBoundingClientRect(),V=d-$.left+o;let G=0;for(let U=0;U<I.length;U++){const F=I[U],ge=t.getColumnX(F.id)+F.width/2;V>ge&&(G=U+1)}return{dropIndex:G}},[I,t,o]),oe=g.useCallback((d,P)=>{P.target.dataset.resizeHandle||f&&(P.preventDefault(),z.current=!1,S.current={colId:d,startX:P.clientX,currentX:P.clientX})},[f]);g.useEffect(()=>{if(!f)return;const d=V=>{const G=S.current;if(!G)return;const U=Math.abs(V.clientX-G.startX);if(!z.current&&U>=xn){z.current=!0;const F=I.find(ne=>ne.id===G.colId),le=t.getColumnX(G.colId),{dropIndex:ge}=i(V.clientX);Y({colId:G.colId,startX:G.startX,currentX:V.clientX,dropIndex:ge,ghostLeft:le-o,ghostWidth:(F==null?void 0:F.width)??80,ghostTitle:(F==null?void 0:F.title)??""});return}if(z.current){const{dropIndex:F}=i(V.clientX);Y(le=>le&&{...le,currentX:V.clientX,dropIndex:F})}},P=()=>{z.current&&Q.current&&f(Q.current.colId,Q.current.dropIndex),S.current=null,z.current=!1,Y(null)},$=V=>{V.key==="Escape"&&(S.current=null,z.current=!1,Y(null))};return document.addEventListener("mousemove",d),document.addEventListener("mouseup",P),document.addEventListener("keydown",$),()=>{document.removeEventListener("mousemove",d),document.removeEventListener("mouseup",P),document.removeEventListener("keydown",$)}},[f,I,t,o,i]);const _e=()=>{if(!L)return 0;const{dropIndex:d}=L;if(d>=I.length){const P=I[I.length-1];return t.getColumnX(P.id)+P.width-o}return t.getColumnX(I[d].id)-o},ye=(d,P,$)=>{const V=(L==null?void 0:L.colId)===d.id,U=d.pinned==="left"?"horizontal":n,F=M==null?void 0:M(d.id,d.title,{width:d.width,height:$,orientation:U}),le=d.filterable&&!!u;let ge;return F?ge=F:U==="vertical"?ge=x.jsx("div",{style:{writingMode:"vertical-rl",textOrientation:"mixed",transform:"rotate(180deg)",height:$-16,lineHeight:`${d.width}px`,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:d.title}):ge=x.jsx("span",{style:{overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",flex:1},children:d.title}),x.jsxs("div",{role:"columnheader","aria-label":d.title,style:{position:"absolute",left:P,width:d.width,height:$,boxSizing:"border-box",borderRight:"1px solid #d1d5db",display:"flex",alignItems:U==="horizontal"?"center":"flex-end",justifyContent:U==="horizontal"?"flex-start":"center",paddingLeft:U==="horizontal"?8:0,paddingBottom:U!=="horizontal"?6:0,cursor:f?"grab":c?"pointer":"default",userSelect:"none",fontSize:13,fontWeight:500,color:"#6C737F",opacity:V?.4:1},onMouseDown:f?ne=>oe(d.id,ne):void 0,onPointerDown:c&&!f?ne=>c(d.id,ne):void 0,children:[ge,le&&x.jsx("button",{type:"button","aria-label":"Filter column","data-cg-filter-icon":d.id,style:{position:"absolute",top:2,right:5,zIndex:2,display:"flex",alignItems:"center",justifyContent:"center",padding:1,border:"none",borderRadius:3,background:"transparent",cursor:"pointer",lineHeight:0},onMouseDown:ne=>ne.stopPropagation(),onPointerDown:ne=>ne.stopPropagation(),onClick:ne=>{ne.stopPropagation(),u==null||u(d.id,ne.currentTarget.getBoundingClientRect())},children:x.jsx(Rn,{active:d.filterActive})}),a&&x.jsx("div",{"data-resize-handle":"true",style:{position:"absolute",right:0,top:0,width:4,height:"100%",cursor:"col-resize",zIndex:1},onMouseDown:ne=>N(d.id,ne)})]},d.id)};return x.jsxs("div",{ref:b,style:{flex:1,height:k,overflow:"hidden",position:"relative",backgroundColor:"#f9fafb"},children:[x.jsxs("div",{style:{position:"absolute",top:0,left:0,transform:`translateX(-${o}px)`,height:k},children:[x.jsx("div",{style:{position:"absolute",top:0,height:k},children:O.map(d=>{const P=t.getColumnX(d.id);return ye(d,P,k)})}),v&&x.jsx("div",{style:{position:"relative",height:ut,zIndex:1},children:m.filter(d=>!d.pinned).map(d=>x.jsx("div",{style:{position:"absolute",left:d.startX,width:d.width,height:ut,borderBottom:`1px solid ${d.group.color??"#3b82f6"}`,display:"flex",alignItems:"center",paddingLeft:8,boxSizing:"border-box",fontSize:11,fontWeight:600,color:"#6b7280",overflow:"hidden",whiteSpace:"nowrap",backgroundColor:"#f9fafb"},children:d.group.title},d.group.id))})]}),H.length>0&&x.jsxs("div",{style:{position:"absolute",top:0,left:0,width:D,height:k,backgroundColor:"#f9fafb",zIndex:2,display:"flex",flexDirection:"column"},children:[v&&x.jsx("div",{style:{position:"absolute",top:0,left:0,width:D,height:k-gt},children:m.filter(d=>d.pinned).map(d=>x.jsx("div",{style:{position:"absolute",left:d.startX,width:d.width,height:"100%",borderBottom:"1px solid #d1d5db",borderRight:"1px solid #d1d5db",display:"flex",alignItems:"center",justifyContent:"center",boxSizing:"border-box",fontSize:11,fontWeight:600,color:"#6b7280",overflow:"hidden",whiteSpace:"nowrap",backgroundColor:"#f9fafb"},children:d.group.title},d.group.id))}),x.jsx("div",{style:{position:"absolute",bottom:0,left:0,width:D,height:gt,borderTop:"1px solid #d1d5db"},children:H.map(d=>{const P=t.getColumnX(d.id);return ye(d,P,gt)})}),x.jsx("div",{style:{position:"absolute",top:0,right:0,width:1,height:"100%",backgroundColor:"#d1d5db",zIndex:3}})]}),x.jsx("div",{style:{position:"absolute",bottom:0,left:0,right:0,height:1,backgroundColor:"#d1d5db",zIndex:5}}),L&&x.jsx("div",{style:{position:"absolute",top:0,left:_e(),width:2,height:k,backgroundColor:"#3b82f6",pointerEvents:"none",zIndex:10}}),L&&x.jsx("div",{style:{position:"fixed",top:0,left:L.currentX-L.ghostWidth/2,width:L.ghostWidth,height:32,backgroundColor:"#dbeafe",border:"1px solid #3b82f6",borderRadius:4,display:"flex",alignItems:"center",paddingLeft:8,boxSizing:"border-box",fontSize:12,fontWeight:600,color:"#1d4ed8",opacity:.85,pointerEvents:"none",zIndex:9999,whiteSpace:"nowrap",overflow:"hidden"},children:L.ghostTitle})]})}function Sn({columnEngine:t,onColumnVisibilityChange:e}){const[o,n]=g.useState(!1),r=g.useRef(null),c=t.getAllColumns();return g.useEffect(()=>{if(!o)return;const a=u=>{r.current&&!r.current.contains(u.target)&&n(!1)},f=u=>{u.key==="Escape"&&n(!1)};return document.addEventListener("mousedown",a),document.addEventListener("keydown",f),()=>{document.removeEventListener("mousedown",a),document.removeEventListener("keydown",f)}},[o]),x.jsxs("div",{ref:r,style:{position:"relative",display:"inline-block"},children:[x.jsx("button",{onClick:()=>n(a=>!a),style:{display:"flex",alignItems:"center",gap:4,padding:"0 8px",height:28,fontSize:12,fontWeight:600,color:"#374151",backgroundColor:o?"#e5e7eb":"#f9fafb",border:"1px solid #d1d5db",borderRadius:4,cursor:"pointer",whiteSpace:"nowrap"},title:"Toggle column visibility",children:"Columns"}),o&&x.jsx("div",{style:{position:"absolute",top:"100%",left:0,marginTop:4,minWidth:160,backgroundColor:"#ffffff",border:"1px solid #d1d5db",borderRadius:6,boxShadow:"0 4px 12px rgba(0,0,0,0.12)",zIndex:1e3,padding:"4px 0"},children:c.map(a=>{const f=!a.hidden;return x.jsxs("label",{style:{display:"flex",alignItems:"center",gap:8,padding:"6px 12px",cursor:"pointer",fontSize:13,color:"#374151",userSelect:"none"},onMouseEnter:u=>{u.currentTarget.style.backgroundColor="#f3f4f6"},onMouseLeave:u=>{u.currentTarget.style.backgroundColor=""},children:[x.jsx("input",{type:"checkbox",checked:f,onChange:u=>{e(a.id,u.target.checked)},style:{cursor:"pointer",width:14,height:14}}),x.jsx("span",{style:{overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:a.title})]},a.id)})})]})}function kn({content:t,position:e}){return pn.createPortal(x.jsx("div",{style:{position:"fixed",left:e.left,top:e.top,zIndex:9999,pointerEvents:"none"},children:t}),document.body)}function Mn({message:t}){return x.jsx("div",{role:"status","aria-live":"polite","aria-atomic":"true",style:{position:"absolute",width:1,height:1,padding:0,margin:-1,overflow:"hidden",clip:"rect(0,0,0,0)",whiteSpace:"nowrap",borderWidth:0},children:t})}const Ke=28,_n=32,Tn=120;function Wn(t){var Le,Oe;const{treeEngine:e,columnEngine:o,columnGroups:n,rowHeight:r,sidebarWidth:c,gridLayerConfig:a,cellLayerConfig:f,headerOrientation:u="horizontal",scale:M=2}=t,H=n!=null&&n.length>0,O=t.columnHeaderHeight??(u==="vertical"?Tn:_n),I=H?Ke+O:O,D=o.getVisibleColumns(),X=e.flattenedRows,B=Math.max(0,((Le=t.rowRange)==null?void 0:Le.start)??0),L=Math.min(X.length,((Oe=t.rowRange)==null?void 0:Oe.end)??X.length),Y=Math.max(0,L-B),Q=B*r,z=o.getTotalWidth(),S=Y*r,b=c+z,W=I+S,m=16384,T=Math.min(m/b,m/W,Math.sqrt(256e6/(b*W)));let k=Math.min(M,T);(!Number.isFinite(k)||k<=0)&&(k=1);const N=document.createElement("canvas");N.width=Math.round(b*k),N.height=Math.round(W*k),N.style.width=`${b}px`,N.style.height=`${W}px`;const i=N.getContext("2d");i.setTransform(k,0,0,k,0,0),i.fillStyle="#ffffff",i.fillRect(0,0,b,W);const oe=new pt({scrollX:0,scrollY:Q,canvasWidth:z,canvasHeight:S,rowHeight:r}),_e=[],ye=[];let d=0;for(const j of D)_e.push(d),ye.push(j.width),d+=j.width;oe.setColumnOffsets(_e),oe.setColumnWidths(ye);const P=o.getPinnedColumns(),$=o.getScrollableColumns(),V=[],G=[];let U=0;for(const j of P)V.push(U),G.push(j.width),U+=j.width;const F=[],le=[];let ge=0;for(const j of $)F.push(ge),le.push(j.width),ge+=j.width;if(oe.pinnedWidth=o.getPinnedWidth(),oe.setPinnedOffsets(V,G),oe.setScrollableOffsets(F,le),i.save(),i.translate(c,I),Nt(i,oe,e,o,a),Zt(i,oe,e,o,t.selectionEngine,{...f,clearBackground:!1}),i.restore(),c>0){i.save(),i.translate(0,I);const j="#ffffff";i.fillStyle=j,i.fillRect(0,0,c,S),i.strokeStyle="#e5e7eb",i.lineWidth=.5,i.beginPath(),i.moveTo(c,0),i.lineTo(c,S),i.stroke(),i.font="13px system-ui, sans-serif",i.textBaseline="middle",i.fillStyle="#374151";for(let se=B;se<L;se++){const Z=X[se];if(!Z)continue;const be=e.getDepth(Z.id),ee=(se-B)*r,ie=be*16+8,Re=String(Z.title??Z.id),pe=c-ie-8;i.fillStyle=se%2===0?"#ffffff":"#f9fafb",i.fillRect(0,ee,c,r),i.strokeStyle="#e5e7eb",i.lineWidth=.5,i.beginPath(),i.moveTo(0,ee+r),i.lineTo(c,ee+r),i.stroke(),i.fillStyle="#374151";let ae=Re;if(i.measureText(ae).width>pe&&pe>0){for(;ae.length>0&&i.measureText(ae+"…").width>pe;)ae=ae.slice(0,-1);ae=ae+"…"}i.fillText(ae,ie,ee+r/2)}i.restore()}if(i.save(),i.translate(0,0),i.fillStyle="#f9fafb",i.fillRect(0,0,b,I),i.fillStyle="#d1d5db",i.fillRect(0,0,b,1),i.strokeStyle="#d1d5db",i.lineWidth=1,i.beginPath(),i.moveTo(0,I),i.lineTo(b,I),i.stroke(),i.fillStyle="#f9fafb",i.fillRect(0,0,c,I),i.strokeStyle="#d1d5db",i.lineWidth=1,i.beginPath(),i.moveTo(c,0),i.lineTo(c,I),i.stroke(),H&&n){const j=new Map;for(const ie of n)j.set(ie.id,ie);let se,Z=0,be=0;const ee=(ie,Re,pe)=>{if(ie===void 0||pe<=0)return;const ae=j.get(ie);if(!ae)return;const He=c+Re;i.strokeStyle="#d1d5db",i.lineWidth=1,i.beginPath(),i.moveTo(He,Ke),i.lineTo(He+pe,Ke),i.stroke(),i.font="600 11px system-ui, sans-serif",i.fillStyle="#6b7280",i.textBaseline="middle",i.textAlign="left",i.fillText(ae.title,He+8,Ke/2)};for(const ie of D){const Re=o.getColumnX(ie.id);ie.group!==se?(ee(se,Z,be),se=ie.group,Z=Re,be=ie.width):be+=ie.width}ee(se,Z,be)}const ne=H?Ke:0,Ue=(j,se)=>{if(se<=0||i.measureText(j).width<=se)return j;let Z=j;for(;Z.length>0&&i.measureText(Z+"…").width>se;)Z=Z.slice(0,-1);return Z+"…"};for(const j of D){const se=o.getColumnX(j.id),Z=c+se,be=u==="vertical"&&j.pinned!=="left";if(i.strokeStyle="#d1d5db",i.lineWidth=1,i.beginPath(),i.moveTo(Z+j.width,ne),i.lineTo(Z+j.width,I),i.stroke(),!!j.title)if(i.fillStyle="#6b7280",i.textBaseline="middle",be){const ee=O-12;i.save(),i.translate(Z+j.width/2+4,I-6),i.rotate(-Math.PI/2),i.font="11px system-ui, sans-serif",i.textAlign="left",i.fillText(Ue(j.title,ee),0,0,ee),i.restore()}else{i.font="600 12px system-ui, sans-serif",i.textAlign="left";const ee=j.width-16,ie=u==="vertical"?I-14:ne+O/2;i.fillText(Ue(j.title,ee),Z+8,ie,ee)}}return i.restore(),i.fillStyle="#d1d5db",i.fillRect(0,0,b,1),i.fillRect(0,W-1,b,1),i.fillRect(0,0,1,W),i.fillRect(b-1,0,1,W),N}const Bt=180,Gt=32,Hn=28,$t=200;function Ft(t,e,o){const n=o??(t==="vertical"?120:32);return e?Hn+n:n}const Dn=g.memo(g.forwardRef(function(e,o){const{rows:n,columns:r,columnGroups:c,getCellData:a,cellRenderer:f,sidebarRenderer:u,sidebarWidth:M=Bt,rowHeight:H=Gt,headerOrientation:O="horizontal",columnHeaderHeight:I,maxHeight:D,rowStyle:X,selection:B,onSelectionChange:L,onCellClick:Y,onCellDoubleClick:Q,onCellContextMenu:z,onCellHover:S,onColumnHeaderClick:b,onTreeToggle:W,defaultExpandedRows:m,onExpandChange:v,onLoadMore:T,hasMore:k,loading:N,hoverContent:i,hoverPosition:oe,showColumnVisibilityMenu:_e,ariaLabel:ye="Data grid"}=e,[d,P]=g.useState(()=>new Set(m??[])),[$,V]=g.useState(0),[G,U]=g.useState(0),[F,le]=g.useState({width:0,height:0}),[ge,ne]=g.useState(!1),[Ue,Le]=g.useState(!1),[Oe,j]=g.useState(r),[se,Z]=g.useState(null),[be,ee]=g.useState(""),[ie,Re]=g.useState(null),pe=g.useRef(null),ae=g.useRef(null);g.useEffect(()=>{j(r)},[r]);const He=g.useRef(null),Ie=g.useRef([]),he=g.useRef(new pt({rowHeight:H})),q=g.useRef(null),A=g.useRef(e),me=g.useRef(null),Ne=g.useRef(null),qe=g.useRef(!1);A.current=e;const De=g.useMemo(()=>new Kt(n,d),[n,d]),fe=g.useMemo(()=>new Te(Oe),[Oe]),mt=g.useMemo(()=>new Ut(B),[B]);me.current={treeEngine:De,columnEngine:fe,selectionEngine:mt};const st=fe.getPinnedColumns().length>0,je=st?0:M,wt=g.useCallback(s=>{const h=me.current;if(!h)return;const{treeEngine:y,columnEngine:_,selectionEngine:C}=h,w=he.current,l=s==="grid"?0:s==="cells"?1:2,p=Ie.current[l];if(!p)return;const R=bn(p,w.canvasWidth,w.canvasHeight);s==="grid"?Nt(R,w,y,_,{rowStyle:A.current.rowStyle}):s==="cells"?Zt(R,w,y,_,C,{cellRenderer:A.current.cellRenderer,getCellData:A.current.getCellData,hoveredCell:Ne.current}):vn(R,w,y,_,C,{hoveredCell:Ne.current,hoveredRowId:pe.current,selectedCell:ae.current,resizingColumn:se??void 0,loading:A.current.loading})},[se]);g.useEffect(()=>{const s=fe.getPinnedColumns(),h=fe.getScrollableColumns(),y=fe.getPinnedWidth(),_=[],C=[];let w=0;for(const ce of s)_.push(w),C.push(ce.width),w+=ce.width;const l=[],p=[];let R=0;for(const ce of h)l.push(R),p.push(ce.width),R+=ce.width;const E=he.current;E.pinnedWidth=y,E.setPinnedOffsets(_,C),E.setScrollableOffsets(l,p);const K=fe.getVisibleColumns(),J=[],ue=[];let ve=0;for(const ce of K)J.push(ve),ue.push(ce.width),ve+=ce.width;E.setColumnOffsets(J),E.setColumnWidths(ue),E.update({rowHeight:H})},[fe,H]),g.useEffect(()=>{const s=new mn(wt);return q.current=s,s.markAllDirty(),()=>{s.destroy(),q.current=null}},[wt]),g.useEffect(()=>{const s=He.current;if(!s)return;const h=new ResizeObserver(y=>{var _,C;for(const w of y){const{width:l,height:p}=w.contentRect,R=l-je,E=Ft(A.current.headerOrientation??"horizontal",(((_=A.current.columnGroups)==null?void 0:_.length)??0)>0,A.current.columnHeaderHeight),K=p-E;he.current.update({canvasWidth:Math.max(0,R),canvasHeight:Math.max(0,K)}),le({width:Math.max(0,R),height:Math.max(0,K)}),(C=q.current)==null||C.markAllDirty()}});return h.observe(s),()=>h.disconnect()},[je]),g.useEffect(()=>{var s;(s=q.current)==null||s.markAllDirty()},[De,fe,mt]),g.useEffect(()=>{var s;(s=q.current)==null||s.markDirty("overlay")},[N]);const it=g.useCallback(s=>{var ue;s.preventDefault();const h=he.current,y=me.current;if(!y)return;const _=y.treeEngine.flattenedRows.length*h.rowHeight,C=y.columnEngine.getTotalWidth(),w=Math.max(0,_-h.canvasHeight),l=C-y.columnEngine.getPinnedWidth(),p=h.canvasWidth-y.columnEngine.getPinnedWidth(),R=Math.max(0,l-p),E=Math.max(0,Math.min(w,h.scrollY+s.deltaY)),K=Math.max(0,Math.min(R,h.scrollX+s.deltaX));h.update({scrollY:E,scrollX:K}),V(E),U(K),(ue=q.current)==null||ue.markAllDirty();const J=A.current;J.hasMore&&J.onLoadMore&&!J.loading&&w-E<$t?qe.current||(qe.current=!0,J.onLoadMore()):w-E>=$t&&(qe.current=!1)},[]);g.useEffect(()=>{const s=Ie.current[2];if(s)return s.addEventListener("wheel",it,{passive:!1}),()=>s.removeEventListener("wheel",it)},[it]),g.useEffect(()=>{N||(qe.current=!1)},[N]);const Jt=g.useCallback(s=>{var p,R,E;const h=me.current;if(!h)return;const y=Ie.current[2];if(!y)return;const _=y.getBoundingClientRect(),C=s.clientX-_.left,w=s.clientY-_.top,l=ot(C,w,he.current,h.treeEngine,h.columnEngine);Ne.current=l,pe.current=(l==null?void 0:l.rowId)??null,Re((l==null?void 0:l.rowId)??null),(p=q.current)==null||p.markDirty("overlay"),(E=(R=A.current).onCellHover)==null||E.call(R,(l==null?void 0:l.rowId)??null,(l==null?void 0:l.colId)??null,s.nativeEvent)},[]),lt=g.useCallback(s=>{P(h=>{var _,C;const y=new Set(h);return y.has(s)?(y.delete(s),ee(`Row ${s} collapsed`)):(y.add(s),ee(`Row ${s} expanded`)),(C=(_=A.current).onExpandChange)==null||C.call(_,[...y]),y})},[]),Qt=g.useCallback(s=>{var Ye,Ve,Be,Ge,$e;const h=me.current;if(!h)return;const y=Ie.current[2];if(!y)return;const _=y.getBoundingClientRect(),C=s.clientX-_.left,w=s.clientY-_.top,l=ot(C,w,he.current,h.treeEngine,h.columnEngine);if(!l)return;const{treeEngine:p,columnEngine:R,selectionEngine:E}=h,K=R.getPinnedColumns();if(K.length>0){const Ce=K[0].id;if(p.hasChildren(l.rowId)&&l.colId===Ce){A.current.onTreeToggle?A.current.onTreeToggle(l.rowId):lt(l.rowId);return}}const J=p.flattenedRows.map(Ce=>Ce.id),ue=R.getVisibleColumns().map(Ce=>Ce.id);s.ctrlKey||s.metaKey?E.ctrlClick(l.rowId,l.colId):s.shiftKey?E.shiftClick(l.rowId,l.colId,J,ue):E.click(l.rowId,l.colId,J,ue),ae.current={rowId:l.rowId,colId:l.colId},(Ve=(Ye=A.current).onSelectionChange)==null||Ve.call(Ye,E.getSelection()),(Ge=(Be=A.current).onCellClick)==null||Ge.call(Be,l.rowId,l.colId,s.nativeEvent),($e=q.current)==null||$e.markDirty("overlay");const ve=String(l.rowId),ce=R.getVisibleColumns().find(Ce=>Ce.id===l.colId),et=(ce==null?void 0:ce.title)??l.colId;ee(`Row ${ve}, Column ${et} selected`)},[lt]),en=g.useCallback(s=>{var p,R;const h=me.current;if(!h)return;const y=Ie.current[2];if(!y)return;const _=y.getBoundingClientRect(),C=s.clientX-_.left,w=s.clientY-_.top,l=ot(C,w,he.current,h.treeEngine,h.columnEngine);l&&((R=(p=A.current).onCellDoubleClick)==null||R.call(p,l.rowId,l.colId,s.nativeEvent))},[]),tn=g.useCallback(s=>{var p,R;s.preventDefault();const h=me.current;if(!h)return;const y=Ie.current[2];if(!y)return;const _=y.getBoundingClientRect(),C=s.clientX-_.left,w=s.clientY-_.top,l=ot(C,w,he.current,h.treeEngine,h.columnEngine);l&&((R=(p=A.current).onCellContextMenu)==null||R.call(p,l.rowId,l.colId,s.nativeEvent))},[]),nn=g.useCallback(()=>{var s,h,y;Ne.current=null,pe.current=null,Re(null),(s=q.current)==null||s.markDirty("overlay"),(y=(h=A.current).onCellHover)==null||y.call(h,null,null,new PointerEvent("pointerleave"))},[]),on=g.useCallback((s,h)=>{var y,_;(_=(y=A.current).onColumnHeaderClick)==null||_.call(y,s,h.nativeEvent)},[]),sn=g.useCallback((s,h)=>{var _,C,w,l;j(p=>p.map(E=>{if(E.id!==s)return E;const K=E.minWidth??30;return{...E,width:Math.max(K,h)}}));const y=(_=me.current)==null?void 0:_.columnEngine;if(y){const p=y.getColumnX(s),R=y.getVisibleColumns().find(E=>E.id===s);if(R){const K=R.pinned==="left"?p+Math.max(R.minWidth??30,h):p+Math.max(R.minWidth??30,h)-he.current.scrollX;Z({colId:s,x:K})}}(w=(C=A.current).onColumnResize)==null||w.call(C,s,h),(l=q.current)==null||l.markAllDirty()},[]);g.useEffect(()=>{const s=()=>{Z(null)};return document.addEventListener("mouseup",s),()=>document.removeEventListener("mouseup",s)},[]);const ln=g.useCallback((s,h)=>{var y,_,C;j(w=>{const l=[...w],p=l.findIndex(K=>K.id===s);if(p===-1)return w;const[R]=l.splice(p,1),E=Math.max(0,Math.min(h,l.length));return l.splice(E,0,R),l}),(_=(y=A.current).onColumnReorder)==null||_.call(y,s,h),(C=q.current)==null||C.markAllDirty()},[]),rn=g.useCallback((s,h)=>{var y,_,C;j(w=>w.map(l=>l.id===s?{...l,hidden:!h}:l)),(_=(y=A.current).onColumnVisibilityChange)==null||_.call(y,s,h),(C=q.current)==null||C.markAllDirty()},[]),bt=g.useCallback((s,h,y,_)=>{h.preventDefault(),h.stopPropagation(),Le(!0);const C=s==="y"?h.clientY:h.clientX,w=s==="y"?he.current.scrollY:he.current.scrollX,l=R=>{var ve;const E=(s==="y"?R.clientY:R.clientX)-C,K=y>0?E/y*_:0,J=Math.max(0,Math.min(_,w+K)),ue=he.current;s==="y"?(ue.update({scrollY:J}),V(J)):(ue.update({scrollX:J}),U(J)),(ve=q.current)==null||ve.markAllDirty()},p=()=>{Le(!1),document.removeEventListener("mousemove",l),document.removeEventListener("mouseup",p)};document.addEventListener("mousemove",l),document.addEventListener("mouseup",p)},[]),cn=g.useCallback(s=>{var ue,ve,ce,et,Ye,Ve,Be,Ge,$e,Ce,ht,kt,Mt,_t,Tt,Wt,Ht,Dt,Xt,Et,Pt,Lt,Ot,jt;const h=me.current;if(!h)return;const{treeEngine:y,columnEngine:_,selectionEngine:C}=h,w=he.current,l=y.flattenedRows.map(te=>te.id),p=_.getVisibleColumns().map(te=>te.id);if(l.length===0||p.length===0)return;const R=C.getSelection().anchor,E=s.ctrlKey||s.metaKey,K=(te,re)=>{const de=l.length*w.rowHeight,Se=_.getTotalWidth(),ke=Math.max(0,de-w.canvasHeight),tt=Math.max(0,Se-w.canvasWidth),We=te*w.rowHeight,Ee=We+w.rowHeight;let we=w.scrollY;We<w.scrollY?we=We:Ee>w.scrollY+w.canvasHeight&&(we=Ee-w.canvasHeight),we=Math.max(0,Math.min(ke,we));const Me=_.getVisibleColumns()[re],Fe=Me?_.getColumnX(Me.id):0,nt=Fe+((Me==null?void 0:Me.width)??0);let xe=w.scrollX;Fe<w.scrollX?xe=Fe:nt>w.scrollX+w.canvasWidth&&(xe=nt-w.canvasWidth),xe=Math.max(0,Math.min(tt,xe)),(we!==w.scrollY||xe!==w.scrollX)&&(w.update({scrollY:we,scrollX:xe}),V(we),U(xe))},J=(te,re,de)=>{var At,zt,Yt;let Se=R?l.indexOf(R.rowId):-1,ke=R?p.indexOf(R.colId):-1;Se===-1&&(Se=0),ke===-1&&(ke=0);const tt=Math.max(0,Math.min(l.length-1,Se+te)),We=Math.max(0,Math.min(p.length-1,ke+re)),Ee=l[tt],we=p[We];de?C.shiftClick(Ee,we,l,p):C.click(Ee,we,l,p),(zt=(At=A.current).onSelectionChange)==null||zt.call(At,C.getSelection()),(Yt=q.current)==null||Yt.markDirty("overlay"),K(tt,We);const Me=_.getVisibleColumns()[We],Fe=String(Ee),nt=(Me==null?void 0:Me.title)??String(we),xe=C.getSelection().cells.size;xe>1?ee(`${xe} cells selected`):ee(`Row ${Fe}, Column ${nt} selected`)};switch(s.key){case"ArrowUp":s.preventDefault(),J(-1,0,s.shiftKey);break;case"ArrowDown":s.preventDefault(),J(1,0,s.shiftKey);break;case"ArrowLeft":s.preventDefault(),J(0,-1,s.shiftKey);break;case"ArrowRight":s.preventDefault(),J(0,1,s.shiftKey);break;case"Tab":{if(s.preventDefault(),s.shiftKey){const te=R?l.indexOf(R.rowId):0;if((R?p.indexOf(R.colId):0)>0)J(0,-1,!1);else{const de=Math.max(0,(te===-1?0:te)-1),Se=l[de],ke=p[p.length-1];C.click(Se,ke,l,p),(Ye=(et=A.current).onSelectionChange)==null||Ye.call(et,C.getSelection()),(Ve=q.current)==null||Ve.markDirty("overlay"),K(de,p.length-1)}}else{const te=R?l.indexOf(R.rowId):0;if((R?p.indexOf(R.colId):-1)<p.length-1)J(0,1,!1);else{const de=Math.min(l.length-1,(te===-1?0:te)+1),Se=l[de],ke=p[0];C.click(Se,ke,l,p),(ve=(ue=A.current).onSelectionChange)==null||ve.call(ue,C.getSelection()),(ce=q.current)==null||ce.markDirty("overlay"),K(de,0)}}break}case"Escape":s.preventDefault(),C.clear(),(Ge=(Be=A.current).onSelectionChange)==null||Ge.call(Be,C.getSelection()),($e=q.current)==null||$e.markDirty("overlay"),ee("Selection cleared");break;case"a":case"A":if(E){s.preventDefault();for(const te of l)for(const re of p)C.ctrlClick(te,re);(ht=(Ce=A.current).onSelectionChange)==null||ht.call(Ce,C.getSelection()),(kt=q.current)==null||kt.markDirty("overlay"),ee(`${l.length*p.length} cells selected`)}break;case"Home":{s.preventDefault();const te=R?l.indexOf(R.rowId):0;if(E)C.click(l[0],p[0],l,p),(_t=(Mt=A.current).onSelectionChange)==null||_t.call(Mt,C.getSelection()),(Tt=q.current)==null||Tt.markDirty("overlay"),K(0,0);else{const re=te===-1?0:te;C.click(l[re],p[0],l,p),(Ht=(Wt=A.current).onSelectionChange)==null||Ht.call(Wt,C.getSelection()),(Dt=q.current)==null||Dt.markDirty("overlay"),K(re,0)}break}case"End":{s.preventDefault();const te=R?l.indexOf(R.rowId):0;if(E){const re=l.length-1,de=p.length-1;C.click(l[re],p[de],l,p),(Et=(Xt=A.current).onSelectionChange)==null||Et.call(Xt,C.getSelection()),(Pt=q.current)==null||Pt.markDirty("overlay"),K(re,de)}else{const re=te===-1?0:te,de=p.length-1;C.click(l[re],p[de],l,p),(Ot=(Lt=A.current).onSelectionChange)==null||Ot.call(Lt,C.getSelection()),(jt=q.current)==null||jt.markDirty("overlay"),K(re,de)}break}}},[]);g.useImperativeHandle(o,()=>({captureToCanvas(s){const h=me.current;if(!h)throw new Error("CanvasGrid: engines not initialized");const{treeEngine:y,columnEngine:_,selectionEngine:C}=h,w=A.current,l=w.rowHeight??Gt,p=w.sidebarWidth??Bt;return Wn({treeEngine:y,columnEngine:_,selectionEngine:C,columnGroups:w.columnGroups,rowHeight:l,sidebarWidth:p,headerOrientation:w.headerOrientation??"horizontal",columnHeaderHeight:w.columnHeaderHeight,gridLayerConfig:{rowStyle:w.rowStyle},cellLayerConfig:{cellRenderer:w.cellRenderer,getCellData:w.getCellData,hoveredCell:null},scale:(s==null?void 0:s.scale)??2,rowRange:s==null?void 0:s.rowRange})},getRowCount(){var s;return((s=me.current)==null?void 0:s.treeEngine.flattenedRows.length)??0}}),[]);const dn=c!=null&&c.length>0,vt=Ft(O??"horizontal",dn,I),an=je+fe.getTotalWidth()+2,hn=vt+De.flattenedRows.length*H+2,Xe=12,Ct=24,Ze=ge||Ue,xt=De.flattenedRows.length*H,fn=fe.getTotalWidth(),rt=fe.getPinnedWidth(),Je=Math.max(0,xt-F.height),yt=fn-rt,ct=Math.max(0,F.width-rt),Qe=Math.max(0,yt-ct),Rt=2,It=Je>Rt&&F.height>0,St=Qe>Rt&&F.width>0,Ae=F.height-(St?Xe:0),dt=Math.min(Ae,Math.max(Ct,Ae*(F.height/xt))),un=Je>0?$/Je*(Ae-dt):0,ze=ct-(It?Xe:0),at=Math.min(ze,Math.max(Ct,ze*(ct/yt))),gn=Qe>0?G/Qe*(ze-at):0;return x.jsxs("div",{ref:He,tabIndex:0,role:"grid","aria-label":ye,"aria-rowcount":De.flattenedRows.length,"aria-colcount":fe.getVisibleColumns().length,onKeyDown:cn,style:{display:"flex",flexDirection:"column",width:an,height:hn,maxWidth:"100%",maxHeight:D??"100%",overflow:"hidden",fontFamily:"system-ui, sans-serif",outline:"none",border:"1px solid #d1d5db",borderRadius:4},children:[x.jsxs("div",{style:{display:"flex",flexDirection:"row",height:vt,flexShrink:0},children:[!st&&x.jsx("div",{style:{width:je,flexShrink:0,borderRight:"1px solid #e5e7eb",borderBottom:"1px solid #e5e7eb",backgroundColor:"#f9fafb",display:"flex",flexDirection:"column",overflow:"hidden"},children:e.sidebarHeaderRenderer?x.jsx("div",{style:{width:"100%",flex:1},children:e.sidebarHeaderRenderer()}):_e?x.jsx("div",{style:{display:"flex",alignItems:"center",justifyContent:"center",flex:1},children:x.jsx(Sn,{columnEngine:fe,onColumnVisibilityChange:rn})}):null}),x.jsx(In,{columnEngine:fe,columnGroups:c,scrollX:G,orientation:O,columnHeaderHeight:I,onColumnClick:b?on:void 0,onColumnResize:e.onColumnResize!==void 0?sn:void 0,onColumnReorder:e.onColumnReorder!==void 0?ln:void 0,onColumnFilterClick:e.onColumnFilterClick,columnHeaderRenderer:e.columnHeaderRenderer})]}),x.jsxs("div",{style:{display:"flex",flexDirection:"row",flex:1,overflow:"hidden"},children:[!st&&x.jsx(Cn,{treeEngine:De,width:je,rowHeight:H,scrollTop:$,viewportHeight:he.current.canvasHeight||600,sidebarRenderer:u,onToggle:lt,onRowClick:e.onRowHeaderClick?(s,h)=>e.onRowHeaderClick(s,h.nativeEvent):void 0,hoveredRowId:ie,onRowHover:s=>{var h;pe.current=s,Re(s),(h=q.current)==null||h.markDirty("overlay")}}),x.jsxs("div",{style:{flex:1,position:"relative",overflow:"hidden"},onMouseEnter:()=>ne(!0),onMouseLeave:()=>ne(!1),children:[x.jsx("canvas",{ref:s=>{s&&(Ie.current[0]=s)},style:{position:"absolute",top:0,left:0,zIndex:0}}),x.jsx("canvas",{ref:s=>{s&&(Ie.current[1]=s)},style:{position:"absolute",top:0,left:0,zIndex:1}}),x.jsx("canvas",{ref:s=>{s&&(Ie.current[2]=s)},style:{position:"absolute",top:0,left:0,zIndex:2},onPointerMove:Jt,onPointerDown:Qt,onDoubleClick:en,onContextMenu:tn,onPointerLeave:nn}),It&&x.jsx("div",{style:{position:"absolute",top:0,right:0,width:Xe,height:Ae,zIndex:3,opacity:Ze?1:0,transition:"opacity 150ms ease",pointerEvents:Ze?"auto":"none"},children:x.jsx("div",{onMouseDown:s=>bt("y",s,Ae-dt,Je),style:{position:"absolute",top:un,left:2,width:Xe-4,height:dt,borderRadius:8,backgroundColor:"rgba(0, 0, 0, 0.3)",cursor:"pointer"}})}),St&&x.jsx("div",{style:{position:"absolute",bottom:0,left:rt,height:Xe,width:ze,zIndex:3,opacity:Ze?1:0,transition:"opacity 150ms ease",pointerEvents:Ze?"auto":"none"},children:x.jsx("div",{onMouseDown:s=>bt("x",s,ze-at,Qe),style:{position:"absolute",left:gn,top:2,height:Xe-4,width:at,borderRadius:8,backgroundColor:"rgba(0, 0, 0, 0.3)",cursor:"pointer"}})})]})]}),i!=null&&oe!=null&&x.jsx(kn,{content:i,position:oe}),x.jsx(Mn,{message:be})]})}));exports.CanvasGrid=Dn;exports.ColumnEngine=Te;exports.SelectionEngine=Ut;exports.TreeEngine=Kt;exports.ViewState=pt;exports.createDrawHelpers=qt;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("react/jsx-runtime"),g=require("react"),bn=require("react-dom");class pt{constructor(e){this.scrollX=0,this.scrollY=0,this.canvasWidth=0,this.canvasHeight=0,this.rowHeight=32,this.pinnedWidth=0,this._columnOffsets=[],this._columnWidths=[],this._pinnedOffsets=[],this._pinnedWidths=[],this._scrollableOffsets=[],this._scrollableWidths=[],e&&this.update(e)}update(e){e.scrollX!==void 0&&(this.scrollX=e.scrollX),e.scrollY!==void 0&&(this.scrollY=e.scrollY),e.canvasWidth!==void 0&&(this.canvasWidth=e.canvasWidth),e.canvasHeight!==void 0&&(this.canvasHeight=e.canvasHeight),e.rowHeight!==void 0&&(this.rowHeight=e.rowHeight)}setColumnOffsets(e){this._columnOffsets=e}setColumnWidths(e){this._columnWidths=e}setPinnedOffsets(e,o){this._pinnedOffsets=e,this._pinnedWidths=o}setScrollableOffsets(e,o){this._scrollableOffsets=e,this._scrollableWidths=o}rowToY(e){return e*this.rowHeight-this.scrollY}yToRow(e){return Math.floor((e+this.scrollY)/this.rowHeight)}colToX(e,o){return o===!0?this._pinnedOffsets[e]??0:o===!1?(this._scrollableOffsets[e]??0)-this.scrollX+this.pinnedWidth:(this._columnOffsets[e]??0)-this.scrollX}xToColPinned(e){for(let n=0;n<this._pinnedOffsets.length;n++){const r=this._pinnedOffsets[n],c=this._pinnedWidths[n]??0;if(e>=r&&e<r+c)return{index:n,pinned:!0}}const o=e-this.pinnedWidth+this.scrollX;for(let n=0;n<this._scrollableOffsets.length;n++){const r=this._scrollableOffsets[n],c=this._scrollableWidths[n]??0;if(o>=r&&o<r+c)return{index:n,pinned:!1}}return null}xToCol(e){const o=e+this.scrollX;for(let n=0;n<this._columnOffsets.length;n++){const r=this._columnOffsets[n],c=this._columnWidths[n]??0;if(o>=r&&o<r+c)return n}return-1}}const $t=["grid","cells","overlay"];class vn{constructor(e){this._dirty={grid:!1,cells:!1,overlay:!1},this._rafId=null,this._drawFn=e}markDirty(e){this._dirty[e]=!0,this._rafId===null&&(this._rafId=requestAnimationFrame(()=>this._flush()))}markAllDirty(){for(const e of $t)this._dirty[e]=!0;this._rafId===null&&(this._rafId=requestAnimationFrame(()=>this._flush()))}destroy(){this._rafId!==null&&(cancelAnimationFrame(this._rafId),this._rafId=null)}_flush(){this._rafId=null;for(const e of $t)this._dirty[e]&&(this._dirty[e]=!1,this._drawFn(e))}}class Nt{constructor(e,o){this._rows=e,this._expandedRows=o,this._depthMap=new Map,this._childrenMap=new Map,this._buildMaps(),this._flattenedRows=this._buildFlattenedRows()}_buildMaps(){for(const n of this._rows)this._childrenMap.has(n.id)||this._childrenMap.set(n.id,[]),n.parentId!==void 0&&(this._childrenMap.has(n.parentId)||this._childrenMap.set(n.parentId,[]),this._childrenMap.get(n.parentId).push(n));const o=this._rows.filter(n=>n.parentId===void 0).map(n=>({row:n,depth:0}));for(;o.length>0;){const{row:n,depth:r}=o.pop();this._depthMap.set(n.id,r);const c=this._childrenMap.get(n.id)??[];for(const a of c)o.push({row:a,depth:r+1})}}_buildFlattenedRows(){const e=[],o=this._rows.filter(r=>r.parentId===void 0),n=r=>{if(e.push(r),this._expandedRows.has(r.id)){const c=this._childrenMap.get(r.id)??[];for(const a of c)n(a)}};for(const r of o)n(r);return e}get flattenedRows(){return this._flattenedRows}getDepth(e){return this._depthMap.get(e)??0}hasChildren(e){const o=this._childrenMap.get(e);return o!==void 0&&o.length>0}isExpanded(e){return this._expandedRows.has(e)}toggle(e){const o=new Set(this._expandedRows);return o.has(e)?o.delete(e):o.add(e),o}getVisibleRange(e,o,n){if(n<=0||this._flattenedRows.length===0)return{startIndex:0,endIndex:0};const r=Math.max(0,Math.floor(e/n)),c=Math.min(this._flattenedRows.length-1,Math.ceil((e+o)/n)-1);return{startIndex:r,endIndex:c}}}const Cn=30;class De{constructor(e){this._columns=e}getVisibleColumns(){return this._columns.filter(e=>!e.hidden)}getPinnedColumns(){return this._columns.filter(e=>!e.hidden&&e.pinned==="left")}getScrollableColumns(){return this._columns.filter(e=>!e.hidden&&e.pinned!=="left")}getPinnedWidth(){return this.getPinnedColumns().reduce((e,o)=>e+o.width,0)}getAllColumns(){return[...this._columns]}getTotalWidth(){return this.getVisibleColumns().reduce((e,o)=>e+o.width,0)}getColumnX(e){const o=this.getPinnedColumns(),n=this.getScrollableColumns();let r=0;for(const c of o){if(c.id===e)return r;r+=c.width}for(const c of n){if(c.id===e)return r;r+=c.width}return-1}getColumnAtX(e){let o=0;for(const n of this.getPinnedColumns()){if(e>=o&&e<o+n.width)return n.id;o+=n.width}for(const n of this.getScrollableColumns()){if(e>=o&&e<o+n.width)return n.id;o+=n.width}return null}getVisibleRange(e,o){const n=this.getScrollableColumns();if(n.length===0)return{startIndex:0,endIndex:0};const r=this.getPinnedWidth(),c=o-r;let a=0,h=0,p=n.length-1;for(let I=0;I<n.length;I++)a+n[I].width<=e&&(h=I+1),a<e+c&&(p=I),a+=n[I].width;return h=Math.min(h,n.length-1),p=Math.min(p,n.length-1),{startIndex:h,endIndex:p}}getPinnedRange(){const e=this.getPinnedColumns();return e.length===0?{startIndex:0,endIndex:-1}:{startIndex:0,endIndex:e.length-1}}resize(e,o){const n=this._columns.map(r=>{if(r.id!==e)return r;const c=r.minWidth??Cn;return{...r,width:Math.max(c,o)}});return new De(n)}reorder(e,o){const n=[...this._columns],r=n.findIndex(h=>h.id===e);if(r===-1)return new De(n);const[c]=n.splice(r,1),a=Math.max(0,Math.min(o,n.length));return n.splice(a,0,c),new De(n)}hide(e){const o=this._columns.map(n=>n.id===e?{...n,hidden:!0}:n);return new De(o)}show(e){const o=this._columns.map(n=>n.id===e?{...n,hidden:!1}:n);return new De(o)}}function ze(t,e){return`${t}|${e}`}function ut(){return{cells:new Set,rows:new Set,columns:new Set,anchor:void 0}}class Zt{constructor(e){e?this._selection={cells:new Set(e.cells),rows:new Set(e.rows),columns:new Set(e.columns),anchor:e.anchor}:this._selection=ut()}click(e,o,n,r){this._selection=ut(),this._selection.cells.add(ze(e,o)),this._selection.anchor={rowId:e,colId:o}}ctrlClick(e,o){const n=ze(e,o);this._selection.cells.has(n)?this._selection.cells.delete(n):this._selection.cells.add(n),this._selection.anchor={rowId:e,colId:o}}shiftClick(e,o,n,r){const c=this._selection.anchor;if(!c){this.click(e,o,n,r);return}const a=n.indexOf(c.rowId),h=n.indexOf(e),p=r.indexOf(c.colId),I=r.indexOf(o),D=Math.min(a,h),j=Math.max(a,h),W=Math.min(p,I),_=Math.max(p,I),X=new Set;for(let B=D;B<=j;B++)for(let G=W;G<=_;G++)X.add(ze(n[B],r[G]));this._selection.cells=X,this._selection.rows=new Set,this._selection.columns=new Set}selectRow(e,o){for(const n of o)this._selection.cells.add(ze(e,n));this._selection.rows.add(e),this._selection.anchor={rowId:e,colId:o[0]??""}}selectColumn(e,o){for(const n of o)this._selection.cells.add(ze(n,e));this._selection.columns.add(e),this._selection.anchor={rowId:o[0]??"",colId:e}}clear(){this._selection=ut()}isSelected(e,o){return this._selection.cells.has(ze(e,o))}isRowSelected(e){return this._selection.rows.has(e)}isColumnSelected(e){return this._selection.columns.has(e)}getSelection(){return{cells:new Set(this._selection.cells),rows:new Set(this._selection.rows),columns:new Set(this._selection.columns),anchor:this._selection.anchor}}}function ot(t,e,o,n,r){const c=o.yToRow(e),a=n.flattenedRows;if(c<0||c>=a.length)return null;const h=r.getPinnedColumns(),p=r.getPinnedWidth();if(h.length>0){if(t<p){let _=0;for(const X of h){if(t>=_&&t<_+X.width)return{rowId:a[c].id,colId:X.id};_+=X.width}return null}const D=t-p+o.scrollX,j=r.getScrollableColumns();let W=0;for(const _ of j){if(D>=W&&D<W+_.width)return{rowId:a[c].id,colId:_.id};W+=_.width}return null}const I=r.getColumnAtX(t+o.scrollX);return I===null?null:{rowId:a[c].id,colId:I}}function xn(t,e,o){const n=window.devicePixelRatio||1;(t.width!==Math.round(e*n)||t.height!==Math.round(o*n))&&(t.width=Math.round(e*n),t.height=Math.round(o*n),t.style.width=`${e}px`,t.style.height=`${o}px`);const c=t.getContext("2d");return c.setTransform(n,0,0,n,0,0),c}function Jt(t,e,o,n,r={}){const{canvasWidth:c,canvasHeight:a,rowHeight:h}=e,{rowStyle:p}=r;t.clearRect(0,0,c,a),t.fillStyle="#f3f4f6",t.fillRect(0,0,c,a);const I=o.getVisibleRange(e.scrollY,a,h),D=o.flattenedRows,j=n.getPinnedColumns(),W=n.getScrollableColumns(),_=n.getPinnedWidth(),X=j.length>0;if(D.length===0||j.length===0&&W.length===0)return;const B=n.getTotalWidth(),G=Math.min(c,B-e.scrollX+(X?e.scrollX:0)),P=X?Math.min(c,_+n.getScrollableColumns().reduce((E,u)=>E+u.width,0)-e.scrollX):G,Z=D.length*h,F=Math.min(a,Z-e.scrollY);for(let E=I.startIndex;E<=I.endIndex;E++){const u=D[E];if(!u)continue;const S=e.rowToY(E),z=o.getDepth(u.id);let x=E%2===0?"#ffffff":"#f9fafb";if(p){const w=p(u,z);w!=null&&w.backgroundColor&&(x=w.backgroundColor)}t.fillStyle=x,t.fillRect(0,S,Math.max(P,_),h),t.strokeStyle="#d1d5db",t.lineWidth=.5,t.beginPath(),t.moveTo(0,S+h),t.lineTo(Math.max(P,_),S+h),t.stroke()}if(F>0){const E=n.getVisibleRange(e.scrollX,c);t.strokeStyle="#d1d5db",t.lineWidth=.5;for(let u=E.startIndex;u<=E.endIndex;u++){const S=W[u];if(!S)continue;const z=e.colToX(u,!1);t.beginPath(),t.moveTo(z+S.width,0),t.lineTo(z+S.width,F),t.stroke()}}if(X&&F>0){t.fillStyle="#ffffff",t.fillRect(0,0,_,a);for(let u=I.startIndex;u<=I.endIndex;u++){const S=D[u];if(!S)continue;const z=e.rowToY(u),x=o.getDepth(S.id);let w=u%2===0?"#ffffff":"#f9fafb";if(p){const T=p(S,x);T!=null&&T.backgroundColor&&(w=T.backgroundColor)}t.fillStyle=w,t.fillRect(0,z,_,h),t.strokeStyle="#d1d5db",t.lineWidth=.5,t.beginPath(),t.moveTo(0,z+h),t.lineTo(_,z+h),t.stroke()}const E=n.getPinnedRange();t.strokeStyle="#d1d5db",t.lineWidth=.5;for(let u=E.startIndex;u<=E.endIndex;u++){const S=j[u];if(!S)continue;const z=e.colToX(u,!0);t.beginPath(),t.moveTo(z+S.width,0),t.lineTo(z+S.width,F),t.stroke()}t.strokeStyle="#d1d5db",t.lineWidth=1,t.beginPath(),t.moveTo(_,0),t.lineTo(_,F),t.stroke()}}function Qt(t){function e(a,h,p,I,D,j){t.fillStyle=j,t.beginPath(),t.roundRect(a,h,p,I,D),t.fill()}function o(a,h,p,I){t.fillText(a,h,p,I)}function n(a,h){if(t.measureText(a).width<=h)return a;let p=a;for(;p.length>0;)if(p=p.slice(0,-1),t.measureText(p+"…").width<=h)return p+"…";return"…"}function r(a,h,p,I,D){const X=t.measureText(p).width+16;return e(a,h,X,20,20/2,I),t.fillStyle=D,t.fillText(p,a+8,h+20/2),X}function c(a,h,p,I,D,j){e(a,h,p,I,I/2,"#d1d5db");const W=p*Math.max(0,Math.min(1,D));W>0&&e(a,h,W,I,I/2,j)}return{roundRect:e,fillText:o,truncateText:n,badge:r,progressBar:c}}function en(t,e,o,n,r,c){const{canvasWidth:a,canvasHeight:h,rowHeight:p}=e,{cellRenderer:I,getCellData:D,hoveredCell:j}=c;c.clearBackground!==!1&&t.clearRect(0,0,a,h);const W=o.getVisibleRange(e.scrollY,h,p),_=o.flattenedRows,X=n.getPinnedColumns(),B=n.getScrollableColumns(),G=X.length>0;if(_.length===0||X.length===0&&B.length===0)return;const P=Qt(t),Z=(u,S,z,x)=>{const w={x:z,y:x,width:S.width,height:p},T={rowId:u.id,colId:S.id,data:D(u.id,S.id)},M={selected:r.isSelected(u.id,S.id),hovered:j!=null&&j.rowId===u.id&&j.colId===S.id,rowSelected:r.isRowSelected(u.id),colSelected:r.isColumnSelected(S.id),filtered:!0,depth:o.getDepth(u.id),hasChildren:o.hasChildren(u.id),expanded:o.isExpanded(u.id)};t.save(),t.beginPath(),t.rect(z,x,S.width,p),t.clip(),I(t,T,w,M,P),t.restore()},F=G?n.getPinnedWidth():0,E=n.getVisibleRange(e.scrollX,a);t.save(),G&&(t.beginPath(),t.rect(F,0,Math.max(0,a-F),h),t.clip());for(let u=W.startIndex;u<=W.endIndex;u++){const S=_[u];if(!S)continue;const z=e.rowToY(u);for(let x=E.startIndex;x<=E.endIndex;x++){const w=B[x];if(!w)continue;const T=e.colToX(x,G?!1:void 0);Z(S,w,T,z)}}if(t.restore(),G){const u=n.getPinnedRange();for(let S=W.startIndex;S<=W.endIndex;S++){const z=_[S];if(!z)continue;const x=e.rowToY(S);for(let w=u.startIndex;w<=u.endIndex;w++){const T=X[w];if(!T)continue;const M=e.colToX(w,!0);Z(z,T,M,x)}}}}function yn(t,e,o,n,r,c={}){const{canvasWidth:a,canvasHeight:h,rowHeight:p}=e,{hoveredCell:I,hoveredRowId:D,selectedCell:j,resizingColumn:W,loading:_}=c;t.clearRect(0,0,a,h);const X=o.getVisibleRange(e.scrollY,h,p),B=o.flattenedRows,G=n.getPinnedColumns(),P=n.getScrollableColumns(),Z=G.length>0,F=n.getPinnedWidth();if(B.length===0||G.length===0&&P.length===0)return;const E=n.getVisibleRange(e.scrollX,a),u=n.getPinnedRange(),S=x=>{for(let w=E.startIndex;w<=E.endIndex;w++){const T=P[w];if(!T)continue;const M=Z?e.colToX(w,!1):e.colToX(w);x(T,M)}if(Z)for(let w=u.startIndex;w<=u.endIndex;w++){const T=G[w];if(!T)continue;const M=e.colToX(w,!0);x(T,M)}};if(j){for(let x=X.startIndex;x<=X.endIndex;x++){const w=B[x];if(!w||w.id!==j.rowId)continue;const T=e.rowToY(x);S((M,i)=>{t.fillStyle="rgba(59,130,246,0.06)",t.fillRect(i,T,M.width,p)});break}S((x,w)=>{if(x.id===j.colId)for(let T=X.startIndex;T<=X.endIndex;T++){if(!B[T])continue;const i=e.rowToY(T);t.fillStyle="rgba(59,130,246,0.06)",t.fillRect(w,i,x.width,p)}})}const z=(x,w,T,M,i)=>{const q=x.width,ue=p,He=r.isSelected(T.id,x.id),ve=r.isRowSelected(T.id),d=r.isColumnSelected(x.id),L=D!=null&&D===T.id,$=I!=null&&I.colId===x.id,Y=I!=null&&I.rowId===T.id&&I.colId===x.id;He?(t.fillStyle="rgba(59,130,246,0.12)",t.fillRect(w,M,q,ue),t.strokeStyle="#3b82f6",t.lineWidth=2,t.strokeRect(w+1,M+1,q-2,ue-2)):ve||d||Y?(t.fillStyle="rgba(59,130,246,0.08)",t.fillRect(w,M,q,ue)):(L||$)&&(t.fillStyle="rgba(59,130,246,0.04)",t.fillRect(w,M,q,ue))};t.save(),Z&&(t.beginPath(),t.rect(F,0,Math.max(0,a-F),h),t.clip());for(let x=X.startIndex;x<=X.endIndex;x++){const w=B[x];if(!w)continue;const T=e.rowToY(x);for(let M=E.startIndex;M<=E.endIndex;M++){const i=P[M];if(!i)continue;const q=Z?e.colToX(M,!1):e.colToX(M);z(i,q,w,T)}}if(t.restore(),Z)for(let x=X.startIndex;x<=X.endIndex;x++){const w=B[x];if(!w)continue;const T=e.rowToY(x);for(let M=u.startIndex;M<=u.endIndex;M++){const i=G[M];if(!i)continue;const q=e.colToX(M,!0);z(i,q,w,T)}}if(W&&(t.save(),t.strokeStyle="#3b82f6",t.lineWidth=1,t.setLineDash([4,4]),t.beginPath(),t.moveTo(W.x,0),t.lineTo(W.x,h),t.stroke(),t.setLineDash([]),t.restore()),_){const w=h-32;t.fillStyle="#f3f4f6",t.fillRect(0,w,a,32),t.strokeStyle="#e5e7eb",t.lineWidth=1,t.beginPath(),t.moveTo(0,w),t.lineTo(a,w),t.stroke(),t.fillStyle="#6b7280",t.font="13px system-ui, sans-serif",t.textAlign="center",t.textBaseline="middle",t.fillText("Loading...",a/2,w+32/2)}}function Rn({treeEngine:t,width:e,rowHeight:o,scrollTop:n,viewportHeight:r,sidebarRenderer:c,onToggle:a,onRowClick:h,hoveredRowId:p,onRowHover:I}){const D=t.flattenedRows,{startIndex:j,endIndex:W}=t.getVisibleRange(n,r,o),_=[];for(let G=j;G<=W;G++){const P=D[G];if(!P)continue;const Z=t.getDepth(P.id),F=t.hasChildren(P.id),E=t.isExpanded(P.id),u=G*o;let S;if(c)S=c(P,Z,E,F);else{const x=Z*16+8;S=C.jsxs("div",{style:{display:"flex",alignItems:"center",height:"100%",paddingLeft:x,overflow:"hidden",whiteSpace:"nowrap",cursor:F?"pointer":"default",userSelect:"none"},onPointerDown:F?w=>{w.stopPropagation(),a(P.id)}:void 0,children:[F?C.jsx("span",{style:{marginRight:4,flexShrink:0,fontSize:10,color:"#6b7280"},children:E?"▾":"▸"}):C.jsx("span",{style:{width:14,flexShrink:0}}),C.jsx("span",{style:{overflow:"hidden",textOverflow:"ellipsis",fontSize:12},children:String(P.title??P.id)})]})}const z=p===P.id;_.push(C.jsx("div",{role:"rowheader","aria-level":Z+1,"aria-expanded":F?E:void 0,style:{position:"absolute",top:u,left:0,width:"100%",height:o,boxSizing:"border-box",fontSize:13,color:"#374151",borderBottom:"1px solid #e5e7eb",cursor:h?"pointer":"default",backgroundColor:z?"rgba(59,130,246,0.04)":void 0},onPointerDown:h?x=>h(P.id,x):void 0,onPointerEnter:()=>I==null?void 0:I(P.id),onPointerLeave:()=>I==null?void 0:I(null),children:S},String(P.id)))}const X=D.length*o,B=Math.min(r,X-n);return C.jsxs("div",{style:{width:e,height:r,overflow:"hidden",position:"relative",flexShrink:0,backgroundColor:"#ffffff"},children:[C.jsx("div",{style:{position:"absolute",top:0,right:0,width:1,height:Math.max(0,B),backgroundColor:"#e5e7eb",zIndex:1}}),C.jsx("div",{style:{position:"absolute",top:0,left:0,width:"100%",height:X,transform:`translateY(-${n}px)`},children:_})]})}const st=28,gt=28,Sn=4,In=18,kn="rgba(59,130,246,0.12)";function Mn(t){switch(t){case"vertical":return 120;default:return 32}}function _n({active:t}){return C.jsx("svg",{width:14,height:14,viewBox:"0 -960 960 960",fill:t?"#269bf7":"#9ca3af","aria-hidden":"true",children:C.jsx("path",{d:"M429.76-228.04q-14.48 0-24.27-9.7-9.79-9.69-9.79-24.23 0-14.53 9.82-24.37 9.82-9.83 24.24-9.83h100.72q14.37 0 24.1 10.01 9.72 10.01 9.72 24.55 0 14.53-9.82 24.05-9.82 9.52-24.24 9.52H429.76ZM268.57-446.17q-14.51 0-24.17-9.7-9.66-9.69-9.66-24.25 0-14.55 9.66-24.25t24.17-9.7H691.2q14.47 0 24.27 9.88 9.79 9.87 9.79 24.42 0 14.56-9.79 24.08-9.8 9.52-24.27 9.52H268.57Zm-120.72-217.9q-14.51 0-24.17-9.83-9.66-9.83-9.66-24.37 0-14.53 9.82-24.23 9.82-9.7 24.25-9.7h664.06q14.48 0 24.27 9.88 9.8 9.87 9.8 24.4 0 14.54-9.96 24.2-9.95 9.65-24.35 9.65H147.85Z"})})}function Tn({columnEngine:t,columnGroups:e,scrollX:o,orientation:n="horizontal",columnHeaderHeight:r,onColumnClick:c,onColumnResize:a,onColumnReorder:h,onColumnFilterClick:p,selectedColId:I,columnHeaderRenderer:D}){const j=t.getPinnedColumns(),W=t.getScrollableColumns(),_=t.getVisibleColumns(),X=t.getPinnedWidth(),B=new Map;if(e)for(const d of e)B.set(d.id,d);const G=g.useRef(null),[P,Z]=g.useState(null),F=g.useRef(null),E=g.useRef(!1),u=g.useRef(null),S=g.useRef(null);F.current=P;const z=[...j,...W],x=[];if(e&&e.length>0){let d,L=0,$=0,Y=!1;for(const V of z){const ee=t.getColumnX(V.id),K=V.pinned==="left";if(V.group!==d){if(d!==void 0&&$>0){const le=B.get(d);le&&x.push({group:le,startX:L,width:$,pinned:Y})}d=V.group,L=ee,$=V.width,Y=K}else $+=V.width}if(d!==void 0&&$>0){const V=B.get(d);V&&x.push({group:V,startX:L,width:$,pinned:Y})}}const w=x.length>0,T=r??Mn(n),M=w?st+T:T,i=g.useCallback((d,L)=>{L.stopPropagation(),L.preventDefault();const $=_.find(Y=>Y.id===d);$&&(G.current={colId:d,startX:L.clientX,startWidth:$.width})},[_]);g.useEffect(()=>{const d=$=>{const Y=G.current;if(!Y)return;const V=$.clientX-Y.startX,ee=Math.max(Y.startWidth+V,30);a==null||a(Y.colId,ee)},L=()=>{G.current=null};return document.addEventListener("mousemove",d),document.addEventListener("mouseup",L),()=>{document.removeEventListener("mousemove",d),document.removeEventListener("mouseup",L)}},[a]);const q=g.useCallback(d=>{const L=S.current;if(!L)return{dropIndex:0};const $=L.getBoundingClientRect(),Y=d-$.left+o;let V=0;for(let ee=0;ee<_.length;ee++){const K=_[ee],me=t.getColumnX(K.id)+K.width/2;Y>me&&(V=ee+1)}return{dropIndex:V}},[_,t,o]),ue=g.useCallback((d,L)=>{L.target.dataset.resizeHandle||h&&(L.preventDefault(),E.current=!1,u.current={colId:d,startX:L.clientX,currentX:L.clientX})},[h]);g.useEffect(()=>{if(!h)return;const d=Y=>{const V=u.current;if(!V)return;const ee=Math.abs(Y.clientX-V.startX);if(!E.current&&ee>=Sn){E.current=!0;const K=_.find(Ie=>Ie.id===V.colId),le=t.getColumnX(V.colId),{dropIndex:me}=q(Y.clientX);Z({colId:V.colId,startX:V.startX,currentX:Y.clientX,dropIndex:me,ghostLeft:le-o,ghostWidth:(K==null?void 0:K.width)??80,ghostTitle:(K==null?void 0:K.title)??""});return}if(E.current){const{dropIndex:K}=q(Y.clientX);Z(le=>le&&{...le,currentX:Y.clientX,dropIndex:K})}},L=()=>{E.current&&F.current&&h(F.current.colId,F.current.dropIndex),u.current=null,E.current=!1,Z(null)},$=Y=>{Y.key==="Escape"&&(u.current=null,E.current=!1,Z(null))};return document.addEventListener("mousemove",d),document.addEventListener("mouseup",L),document.addEventListener("keydown",$),()=>{document.removeEventListener("mousemove",d),document.removeEventListener("mouseup",L),document.removeEventListener("keydown",$)}},[h,_,t,o,q]);const He=()=>{if(!P)return 0;const{dropIndex:d}=P;if(d>=_.length){const L=_[_.length-1];return t.getColumnX(L.id)+L.width-o}return t.getColumnX(_[d].id)-o},ve=(d,L,$,Y=0)=>{const V=(P==null?void 0:P.colId)===d.id,K=d.pinned==="left"?"horizontal":n,le=d.filterable&&!!p,me=le&&K==="vertical"?In:0,Ie=Math.max(0,$-Y-me),We=D==null?void 0:D(d.id,d.title,{width:d.width,height:Ie,orientation:K});let Ce;return We?Ce=We:K==="vertical"?Ce=C.jsx("div",{style:{writingMode:"vertical-rl",textOrientation:"mixed",transform:"rotate(180deg)",height:Ie-16,lineHeight:`${d.width}px`,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:d.title}):Ce=C.jsx("span",{style:{overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",flex:1},children:d.title}),C.jsxs("div",{role:"columnheader","aria-label":d.title,style:{position:"absolute",left:L,width:d.width,height:$,boxSizing:"border-box",borderRight:"1px solid #d1d5db",backgroundColor:I!=null&&d.id===I?kn:void 0,display:"flex",alignItems:K==="horizontal"?"center":"flex-end",justifyContent:K==="horizontal"?"flex-start":"center",paddingLeft:K==="horizontal"?8:0,paddingBottom:K!=="horizontal"?6:0,cursor:h?"grab":c?"pointer":"default",userSelect:"none",fontSize:13,fontWeight:500,color:"#6C737F",opacity:V?.4:1},onMouseDown:h?H=>ue(d.id,H):void 0,onPointerDown:c&&!h?H=>c(d.id,H):void 0,children:[Ce,le&&C.jsx("button",{type:"button","aria-label":"Filter column","data-cg-filter-icon":d.id,style:{position:"absolute",top:Y+2,right:5,zIndex:2,display:"flex",alignItems:"center",justifyContent:"center",padding:1,border:"none",borderRadius:3,background:"transparent",cursor:"pointer",lineHeight:0},onMouseDown:H=>H.stopPropagation(),onPointerDown:H=>H.stopPropagation(),onClick:H=>{H.stopPropagation(),p==null||p(d.id,H.currentTarget.getBoundingClientRect())},children:C.jsx(_n,{active:d.filterActive})}),a&&C.jsx("div",{"data-resize-handle":"true",style:{position:"absolute",right:0,top:0,width:4,height:"100%",cursor:"col-resize",zIndex:1},onMouseDown:H=>i(d.id,H)})]},d.id)};return C.jsxs("div",{ref:S,style:{flex:1,height:M,overflow:"hidden",position:"relative",backgroundColor:"#f9fafb"},children:[C.jsxs("div",{style:{position:"absolute",top:0,left:0,transform:`translateX(-${o}px)`,height:M},children:[C.jsx("div",{style:{position:"absolute",top:0,height:M},children:W.map(d=>{const L=t.getColumnX(d.id);return ve(d,L,M,w?st:0)})}),w&&C.jsx("div",{style:{position:"relative",height:st,zIndex:1},children:x.filter(d=>!d.pinned).map(d=>C.jsx("div",{style:{position:"absolute",left:d.startX,width:d.width,height:st,borderBottom:`1px solid ${d.group.color??"#d1d5db"}`,borderRight:"1px solid #d1d5db",display:"flex",alignItems:"center",justifyContent:"center",padding:"0 8px",boxSizing:"border-box",fontSize:14,fontWeight:600,color:"#6b7280",overflow:"hidden",whiteSpace:"nowrap",backgroundColor:"#f9fafb"},children:d.group.title},d.group.id))})]}),j.length>0&&C.jsxs("div",{style:{position:"absolute",top:0,left:0,width:X,height:M,backgroundColor:"#f9fafb",zIndex:2,display:"flex",flexDirection:"column"},children:[w&&C.jsx("div",{style:{position:"absolute",top:0,left:0,width:X,height:M-gt},children:x.filter(d=>d.pinned).map(d=>C.jsx("div",{style:{position:"absolute",left:d.startX,width:d.width,height:"100%",borderBottom:"1px solid #d1d5db",borderRight:"1px solid #d1d5db",display:"flex",alignItems:"center",justifyContent:"center",boxSizing:"border-box",fontSize:14,fontWeight:600,color:"#6b7280",overflow:"hidden",whiteSpace:"nowrap",backgroundColor:"#f9fafb"},children:d.group.title},d.group.id))}),C.jsx("div",{style:{position:"absolute",bottom:0,left:0,width:X,height:gt,borderTop:"1px solid #d1d5db"},children:j.map(d=>{const L=t.getColumnX(d.id);return ve(d,L,gt)})}),C.jsx("div",{style:{position:"absolute",top:0,right:0,width:1,height:"100%",backgroundColor:"#d1d5db",zIndex:3}})]}),C.jsx("div",{style:{position:"absolute",bottom:0,left:0,right:0,height:1,backgroundColor:"#d1d5db",zIndex:5}}),P&&C.jsx("div",{style:{position:"absolute",top:0,left:He(),width:2,height:M,backgroundColor:"#3b82f6",pointerEvents:"none",zIndex:10}}),P&&C.jsx("div",{style:{position:"fixed",top:0,left:P.currentX-P.ghostWidth/2,width:P.ghostWidth,height:32,backgroundColor:"#dbeafe",border:"1px solid #3b82f6",borderRadius:4,display:"flex",alignItems:"center",paddingLeft:8,boxSizing:"border-box",fontSize:12,fontWeight:600,color:"#1d4ed8",opacity:.85,pointerEvents:"none",zIndex:9999,whiteSpace:"nowrap",overflow:"hidden"},children:P.ghostTitle})]})}function Hn({columnEngine:t,onColumnVisibilityChange:e}){const[o,n]=g.useState(!1),r=g.useRef(null),c=t.getAllColumns();return g.useEffect(()=>{if(!o)return;const a=p=>{r.current&&!r.current.contains(p.target)&&n(!1)},h=p=>{p.key==="Escape"&&n(!1)};return document.addEventListener("mousedown",a),document.addEventListener("keydown",h),()=>{document.removeEventListener("mousedown",a),document.removeEventListener("keydown",h)}},[o]),C.jsxs("div",{ref:r,style:{position:"relative",display:"inline-block"},children:[C.jsx("button",{onClick:()=>n(a=>!a),style:{display:"flex",alignItems:"center",gap:4,padding:"0 8px",height:28,fontSize:12,fontWeight:600,color:"#374151",backgroundColor:o?"#e5e7eb":"#f9fafb",border:"1px solid #d1d5db",borderRadius:4,cursor:"pointer",whiteSpace:"nowrap"},title:"Toggle column visibility",children:"Columns"}),o&&C.jsx("div",{style:{position:"absolute",top:"100%",left:0,marginTop:4,minWidth:160,backgroundColor:"#ffffff",border:"1px solid #d1d5db",borderRadius:6,boxShadow:"0 4px 12px rgba(0,0,0,0.12)",zIndex:1e3,padding:"4px 0"},children:c.map(a=>{const h=!a.hidden;return C.jsxs("label",{style:{display:"flex",alignItems:"center",gap:8,padding:"6px 12px",cursor:"pointer",fontSize:13,color:"#374151",userSelect:"none"},onMouseEnter:p=>{p.currentTarget.style.backgroundColor="#f3f4f6"},onMouseLeave:p=>{p.currentTarget.style.backgroundColor=""},children:[C.jsx("input",{type:"checkbox",checked:h,onChange:p=>{e(a.id,p.target.checked)},style:{cursor:"pointer",width:14,height:14}}),C.jsx("span",{style:{overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:a.title})]},a.id)})})]})}function Wn({content:t,position:e}){return bn.createPortal(C.jsx("div",{style:{position:"fixed",left:e.left,top:e.top,zIndex:9999,pointerEvents:"none"},children:t}),document.body)}function Dn({message:t}){return C.jsx("div",{role:"status","aria-live":"polite","aria-atomic":"true",style:{position:"absolute",width:1,height:1,padding:0,margin:-1,overflow:"hidden",clip:"rect(0,0,0,0)",whiteSpace:"nowrap",borderWidth:0},children:t})}const Ne=28,En=32,Xn=120;function Pn(t){var We,Ce;const{treeEngine:e,columnEngine:o,columnGroups:n,rowHeight:r,sidebarWidth:c,gridLayerConfig:a,cellLayerConfig:h,headerOrientation:p="horizontal",scale:I=2}=t,D=n!=null&&n.length>0,j=t.columnHeaderHeight??(p==="vertical"?Xn:En),W=D?Ne+j:j,_=o.getVisibleColumns(),X=e.flattenedRows,B=Math.max(0,((We=t.rowRange)==null?void 0:We.start)??0),G=Math.min(X.length,((Ce=t.rowRange)==null?void 0:Ce.end)??X.length),P=Math.max(0,G-B),Z=B*r,F=o.getTotalWidth(),E=P*r,u=c+F,S=W+E,z=16384,w=Math.min(z/u,z/S,Math.sqrt(256e6/(u*S)));let T=Math.min(I,w);(!Number.isFinite(T)||T<=0)&&(T=1);const M=document.createElement("canvas");M.width=Math.round(u*T),M.height=Math.round(S*T),M.style.width=`${u}px`,M.style.height=`${S}px`;const i=M.getContext("2d");i.setTransform(T,0,0,T,0,0),i.fillStyle="#ffffff",i.fillRect(0,0,u,S);const q=new pt({scrollX:0,scrollY:Z,canvasWidth:F,canvasHeight:E,rowHeight:r}),ue=[],He=[];let ve=0;for(const H of _)ue.push(ve),He.push(H.width),ve+=H.width;q.setColumnOffsets(ue),q.setColumnWidths(He);const d=o.getPinnedColumns(),L=o.getScrollableColumns(),$=[],Y=[];let V=0;for(const H of d)$.push(V),Y.push(H.width),V+=H.width;const ee=[],K=[];let le=0;for(const H of L)ee.push(le),K.push(H.width),le+=H.width;if(q.pinnedWidth=o.getPinnedWidth(),q.setPinnedOffsets($,Y),q.setScrollableOffsets(ee,K),i.save(),i.translate(c,W),Jt(i,q,e,o,a),en(i,q,e,o,t.selectionEngine,{...h,clearBackground:!1}),i.restore(),c>0){i.save(),i.translate(0,W);const H="#ffffff";i.fillStyle=H,i.fillRect(0,0,c,E),i.strokeStyle="#e5e7eb",i.lineWidth=.5,i.beginPath(),i.moveTo(c,0),i.lineTo(c,E),i.stroke(),i.font="13px system-ui, sans-serif",i.textBaseline="middle",i.fillStyle="#374151";for(let oe=B;oe<G;oe++){const J=X[oe];if(!J)continue;const xe=e.getDepth(J.id),te=(oe-B)*r,se=xe*16+8,ye=String(J.title??J.id),pe=c-se-8;i.fillStyle=oe%2===0?"#ffffff":"#f9fafb",i.fillRect(0,te,c,r),i.strokeStyle="#e5e7eb",i.lineWidth=.5,i.beginPath(),i.moveTo(0,te+r),i.lineTo(c,te+r),i.stroke(),i.fillStyle="#374151";let de=ye;if(i.measureText(de).width>pe&&pe>0){for(;de.length>0&&i.measureText(de+"…").width>pe;)de=de.slice(0,-1);de=de+"…"}i.fillText(de,se,te+r/2)}i.restore()}if(i.save(),i.translate(0,0),i.fillStyle="#f9fafb",i.fillRect(0,0,u,W),i.fillStyle="#d1d5db",i.fillRect(0,0,u,1),i.strokeStyle="#d1d5db",i.lineWidth=1,i.beginPath(),i.moveTo(0,W),i.lineTo(u,W),i.stroke(),i.fillStyle="#f9fafb",i.fillRect(0,0,c,W),i.strokeStyle="#d1d5db",i.lineWidth=1,i.beginPath(),i.moveTo(c,0),i.lineTo(c,W),i.stroke(),D&&n){const H=new Map;for(const se of n)H.set(se.id,se);let oe,J=0,xe=0;const te=(se,ye,pe)=>{if(se===void 0||pe<=0)return;const de=H.get(se);if(!de)return;const Pe=c+ye;i.strokeStyle="#d1d5db",i.lineWidth=1,i.beginPath(),i.moveTo(Pe,Ne),i.lineTo(Pe+pe,Ne),i.stroke(),i.font="600 11px system-ui, sans-serif",i.fillStyle="#6b7280",i.textBaseline="middle",i.textAlign="left",i.fillText(de.title,Pe+8,Ne/2)};for(const se of _){const ye=o.getColumnX(se.id);se.group!==oe?(te(oe,J,xe),oe=se.group,J=ye,xe=se.width):xe+=se.width}te(oe,J,xe)}const me=D?Ne:0,Ie=(H,oe)=>{if(oe<=0||i.measureText(H).width<=oe)return H;let J=H;for(;J.length>0&&i.measureText(J+"…").width>oe;)J=J.slice(0,-1);return J+"…"};for(const H of _){const oe=o.getColumnX(H.id),J=c+oe,xe=p==="vertical"&&H.pinned!=="left";if(i.strokeStyle="#d1d5db",i.lineWidth=1,i.beginPath(),i.moveTo(J+H.width,me),i.lineTo(J+H.width,W),i.stroke(),!!H.title)if(i.fillStyle="#6b7280",i.textBaseline="middle",xe){const te=j-12;i.save(),i.translate(J+H.width/2+4,W-6),i.rotate(-Math.PI/2),i.font="11px system-ui, sans-serif",i.textAlign="left",i.fillText(Ie(H.title,te),0,0,te),i.restore()}else{i.font="600 12px system-ui, sans-serif",i.textAlign="left";const te=H.width-16,se=p==="vertical"?W-14:me+j/2;i.fillText(Ie(H.title,te),J+8,se,te)}}return i.restore(),i.fillStyle="#d1d5db",i.fillRect(0,0,u,1),i.fillRect(0,S-1,u,1),i.fillRect(0,0,1,S),i.fillRect(u-1,0,1,S),M}const Ft=180,Kt=32,Ln=28,Ut=200,On=150;function qt(t,e,o){const n=o??(t==="vertical"?120:32);return e?Ln+n:n}const jn=g.memo(g.forwardRef(function(e,o){var Mt,_t;const{rows:n,columns:r,columnGroups:c,getCellData:a,cellRenderer:h,sidebarRenderer:p,sidebarWidth:I=Ft,rowHeight:D=Kt,headerOrientation:j="horizontal",columnHeaderHeight:W,maxHeight:_,rowStyle:X,selection:B,onSelectionChange:G,onCellClick:P,onCellDoubleClick:Z,onCellContextMenu:F,onCellHover:E,onColumnHeaderClick:u,onTreeToggle:S,defaultExpandedRows:z,onExpandChange:x,onLoadMore:w,hasMore:T,loading:M,hoverContent:i,hoverPosition:q,showColumnVisibilityMenu:ue,ariaLabel:He="Data grid"}=e,[ve,d]=g.useState(()=>new Set(z??[])),[L,$]=g.useState(0),[Y,V]=g.useState(0),[ee,K]=g.useState({width:0,height:0}),[le,me]=g.useState(!1),[Ie,We]=g.useState(!1),[Ce,H]=g.useState(r),[oe,J]=g.useState(null),[xe,te]=g.useState(""),[se,ye]=g.useState(null),pe=g.useRef(null),de=g.useRef(null);g.useEffect(()=>{H(r)},[r]);const Pe=g.useRef(null),ke=g.useRef([]),ae=g.useRef(new pt({rowHeight:D})),N=g.useRef(null),A=g.useRef(e),we=g.useRef(null),Le=g.useRef(null),Ze=g.useRef(!1),mt=g.useRef(0);A.current=e;const Oe=g.useMemo(()=>new Nt(n,ve),[n,ve]),he=g.useMemo(()=>new De(Ce),[Ce]),wt=g.useMemo(()=>new Zt(B),[B]);we.current={treeEngine:Oe,columnEngine:he,selectionEngine:wt};const it=he.getPinnedColumns().length>0,Ye=it?0:I,bt=g.useCallback(s=>{const f=we.current;if(!f)return;const{treeEngine:y,columnEngine:k,selectionEngine:v}=f,b=ae.current,l=s==="grid"?0:s==="cells"?1:2,m=ke.current[l];if(!m)return;const R=xn(m,b.canvasWidth,b.canvasHeight);s==="grid"?Jt(R,b,y,k,{rowStyle:A.current.rowStyle}):s==="cells"?en(R,b,y,k,v,{cellRenderer:A.current.cellRenderer,getCellData:A.current.getCellData,hoveredCell:Le.current}):yn(R,b,y,k,v,{hoveredCell:Le.current,hoveredRowId:pe.current,selectedCell:de.current,resizingColumn:oe??void 0,loading:A.current.loading})},[oe]);g.useEffect(()=>{const s=he.getPinnedColumns(),f=he.getScrollableColumns(),y=he.getPinnedWidth(),k=[],v=[];let b=0;for(const ie of s)k.push(b),v.push(ie.width),b+=ie.width;const l=[],m=[];let R=0;for(const ie of f)l.push(R),m.push(ie.width),R+=ie.width;const O=ae.current;O.pinnedWidth=y,O.setPinnedOffsets(k,v),O.setScrollableOffsets(l,m);const U=he.getVisibleColumns(),Q=[],fe=[];let ge=0;for(const ie of U)Q.push(ge),fe.push(ie.width),ge+=ie.width;O.setColumnOffsets(Q),O.setColumnWidths(fe),O.update({rowHeight:D})},[he,D]),g.useEffect(()=>{const s=new vn(bt);return N.current=s,s.markAllDirty(),()=>{s.destroy(),N.current=null}},[bt]),g.useEffect(()=>{const s=Pe.current;if(!s)return;const f=new ResizeObserver(y=>{var k,v;for(const b of y){const{width:l,height:m}=b.contentRect,R=l-Ye,O=qt(A.current.headerOrientation??"horizontal",(((k=A.current.columnGroups)==null?void 0:k.length)??0)>0,A.current.columnHeaderHeight),U=m-O;ae.current.update({canvasWidth:Math.max(0,R),canvasHeight:Math.max(0,U)}),K({width:Math.max(0,R),height:Math.max(0,U)}),(v=N.current)==null||v.markAllDirty()}});return f.observe(s),()=>f.disconnect()},[Ye]),g.useEffect(()=>{var s;(s=N.current)==null||s.markAllDirty()},[Oe,he,wt]),g.useEffect(()=>{var s;(s=N.current)==null||s.markDirty("overlay")},[M]);const lt=g.useCallback(s=>{var fe,ge,ie,Ee;s.preventDefault();const f=ae.current,y=we.current;if(!y)return;const k=y.treeEngine.flattenedRows.length*f.rowHeight,v=y.columnEngine.getTotalWidth(),b=Math.max(0,k-f.canvasHeight),l=v-y.columnEngine.getPinnedWidth(),m=f.canvasWidth-y.columnEngine.getPinnedWidth(),R=Math.max(0,l-m),O=Math.max(0,Math.min(b,f.scrollY+s.deltaY)),U=Math.max(0,Math.min(R,f.scrollX+s.deltaX));f.update({scrollY:O,scrollX:U}),$(O),V(U),(fe=N.current)==null||fe.markAllDirty(),mt.current=s.timeStamp,Le.current&&(Le.current=null,pe.current=null,ye(null),(ge=N.current)==null||ge.markDirty("overlay"),(Ee=(ie=A.current).onCellHover)==null||Ee.call(ie,null,null,new PointerEvent("pointerleave")));const Q=A.current;Q.hasMore&&Q.onLoadMore&&!Q.loading&&b-O<Ut?Ze.current||(Ze.current=!0,Q.onLoadMore()):b-O>=Ut&&(Ze.current=!1)},[]);g.useEffect(()=>{const s=ke.current[2];if(s)return s.addEventListener("wheel",lt,{passive:!1}),()=>s.removeEventListener("wheel",lt)},[lt]),g.useEffect(()=>{M||(Ze.current=!1)},[M]);const tn=g.useCallback(s=>{var m,R,O;const f=we.current;if(!f)return;const y=ke.current[2];if(!y||s.timeStamp-mt.current<On)return;const k=y.getBoundingClientRect(),v=s.clientX-k.left,b=s.clientY-k.top,l=ot(v,b,ae.current,f.treeEngine,f.columnEngine);Le.current=l,pe.current=(l==null?void 0:l.rowId)??null,ye((l==null?void 0:l.rowId)??null),(m=N.current)==null||m.markDirty("overlay"),(O=(R=A.current).onCellHover)==null||O.call(R,(l==null?void 0:l.rowId)??null,(l==null?void 0:l.colId)??null,s.nativeEvent)},[]),rt=g.useCallback(s=>{d(f=>{var k,v;const y=new Set(f);return y.has(s)?(y.delete(s),te(`Row ${s} collapsed`)):(y.add(s),te(`Row ${s} expanded`)),(v=(k=A.current).onExpandChange)==null||v.call(k,[...y]),y})},[]),nn=g.useCallback(s=>{var Ge,$e,Fe,Ke,Ue;const f=we.current;if(!f)return;const y=ke.current[2];if(!y)return;const k=y.getBoundingClientRect(),v=s.clientX-k.left,b=s.clientY-k.top,l=ot(v,b,ae.current,f.treeEngine,f.columnEngine);if(!l)return;const{treeEngine:m,columnEngine:R,selectionEngine:O}=f,U=R.getPinnedColumns();if(U.length>0){const Re=U[0].id;if(m.hasChildren(l.rowId)&&l.colId===Re){A.current.onTreeToggle?A.current.onTreeToggle(l.rowId):rt(l.rowId);return}}const Q=m.flattenedRows.map(Re=>Re.id),fe=R.getVisibleColumns().map(Re=>Re.id);s.ctrlKey||s.metaKey?O.ctrlClick(l.rowId,l.colId):s.shiftKey?O.shiftClick(l.rowId,l.colId,Q,fe):O.click(l.rowId,l.colId,Q,fe),de.current={rowId:l.rowId,colId:l.colId},($e=(Ge=A.current).onSelectionChange)==null||$e.call(Ge,O.getSelection()),(Ke=(Fe=A.current).onCellClick)==null||Ke.call(Fe,l.rowId,l.colId,s.nativeEvent),(Ue=N.current)==null||Ue.markDirty("overlay");const ge=String(l.rowId),ie=R.getVisibleColumns().find(Re=>Re.id===l.colId),Ee=(ie==null?void 0:ie.title)??l.colId;te(`Row ${ge}, Column ${Ee} selected`)},[rt]),on=g.useCallback(s=>{var m,R;const f=we.current;if(!f)return;const y=ke.current[2];if(!y)return;const k=y.getBoundingClientRect(),v=s.clientX-k.left,b=s.clientY-k.top,l=ot(v,b,ae.current,f.treeEngine,f.columnEngine);l&&((R=(m=A.current).onCellDoubleClick)==null||R.call(m,l.rowId,l.colId,s.nativeEvent))},[]),sn=g.useCallback(s=>{var m,R;s.preventDefault();const f=we.current;if(!f)return;const y=ke.current[2];if(!y)return;const k=y.getBoundingClientRect(),v=s.clientX-k.left,b=s.clientY-k.top,l=ot(v,b,ae.current,f.treeEngine,f.columnEngine);l&&((R=(m=A.current).onCellContextMenu)==null||R.call(m,l.rowId,l.colId,s.nativeEvent))},[]),ln=g.useCallback(()=>{var s,f,y;Le.current=null,pe.current=null,ye(null),(s=N.current)==null||s.markDirty("overlay"),(y=(f=A.current).onCellHover)==null||y.call(f,null,null,new PointerEvent("pointerleave"))},[]),rn=g.useCallback((s,f)=>{var y,k;(k=(y=A.current).onColumnHeaderClick)==null||k.call(y,s,f.nativeEvent)},[]),cn=g.useCallback((s,f)=>{var k,v,b,l;H(m=>m.map(O=>{if(O.id!==s)return O;const U=O.minWidth??30;return{...O,width:Math.max(U,f)}}));const y=(k=we.current)==null?void 0:k.columnEngine;if(y){const m=y.getColumnX(s),R=y.getVisibleColumns().find(O=>O.id===s);if(R){const U=R.pinned==="left"?m+Math.max(R.minWidth??30,f):m+Math.max(R.minWidth??30,f)-ae.current.scrollX;J({colId:s,x:U})}}(b=(v=A.current).onColumnResize)==null||b.call(v,s,f),(l=N.current)==null||l.markAllDirty()},[]);g.useEffect(()=>{const s=()=>{J(null)};return document.addEventListener("mouseup",s),()=>document.removeEventListener("mouseup",s)},[]);const dn=g.useCallback((s,f)=>{var y,k,v;H(b=>{const l=[...b],m=l.findIndex(U=>U.id===s);if(m===-1)return b;const[R]=l.splice(m,1),O=Math.max(0,Math.min(f,l.length));return l.splice(O,0,R),l}),(k=(y=A.current).onColumnReorder)==null||k.call(y,s,f),(v=N.current)==null||v.markAllDirty()},[]),an=g.useCallback((s,f)=>{var y,k,v;H(b=>b.map(l=>l.id===s?{...l,hidden:!f}:l)),(k=(y=A.current).onColumnVisibilityChange)==null||k.call(y,s,f),(v=N.current)==null||v.markAllDirty()},[]),vt=g.useCallback((s,f,y,k)=>{f.preventDefault(),f.stopPropagation(),We(!0);const v=s==="y"?f.clientY:f.clientX,b=s==="y"?ae.current.scrollY:ae.current.scrollX,l=R=>{var ge;const O=(s==="y"?R.clientY:R.clientX)-v,U=y>0?O/y*k:0,Q=Math.max(0,Math.min(k,b+U)),fe=ae.current;s==="y"?(fe.update({scrollY:Q}),$(Q)):(fe.update({scrollX:Q}),V(Q)),(ge=N.current)==null||ge.markAllDirty()},m=()=>{We(!1),document.removeEventListener("mousemove",l),document.removeEventListener("mouseup",m)};document.addEventListener("mousemove",l),document.addEventListener("mouseup",m)},[]),hn=g.useCallback(s=>{var fe,ge,ie,Ee,Ge,$e,Fe,Ke,Ue,Re,ft,Tt,Ht,Wt,Dt,Et,Xt,Pt,Lt,Ot,jt,At,zt,Yt;const f=we.current;if(!f)return;const{treeEngine:y,columnEngine:k,selectionEngine:v}=f,b=ae.current,l=y.flattenedRows.map(ne=>ne.id),m=k.getVisibleColumns().map(ne=>ne.id);if(l.length===0||m.length===0)return;const R=v.getSelection().anchor,O=s.ctrlKey||s.metaKey,U=(ne,re)=>{const ce=l.length*b.rowHeight,Me=k.getTotalWidth(),_e=Math.max(0,ce-b.canvasHeight),tt=Math.max(0,Me-b.canvasWidth),Xe=ne*b.rowHeight,Ae=Xe+b.rowHeight;let be=b.scrollY;Xe<b.scrollY?be=Xe:Ae>b.scrollY+b.canvasHeight&&(be=Ae-b.canvasHeight),be=Math.max(0,Math.min(_e,be));const Te=k.getVisibleColumns()[re],qe=Te?k.getColumnX(Te.id):0,nt=qe+((Te==null?void 0:Te.width)??0);let Se=b.scrollX;qe<b.scrollX?Se=qe:nt>b.scrollX+b.canvasWidth&&(Se=nt-b.canvasWidth),Se=Math.max(0,Math.min(tt,Se)),(be!==b.scrollY||Se!==b.scrollX)&&(b.update({scrollY:be,scrollX:Se}),$(be),V(Se))},Q=(ne,re,ce)=>{var Vt,Bt,Gt;let Me=R?l.indexOf(R.rowId):-1,_e=R?m.indexOf(R.colId):-1;Me===-1&&(Me=0),_e===-1&&(_e=0);const tt=Math.max(0,Math.min(l.length-1,Me+ne)),Xe=Math.max(0,Math.min(m.length-1,_e+re)),Ae=l[tt],be=m[Xe];ce?v.shiftClick(Ae,be,l,m):v.click(Ae,be,l,m),(Bt=(Vt=A.current).onSelectionChange)==null||Bt.call(Vt,v.getSelection()),(Gt=N.current)==null||Gt.markDirty("overlay"),U(tt,Xe);const Te=k.getVisibleColumns()[Xe],qe=String(Ae),nt=(Te==null?void 0:Te.title)??String(be),Se=v.getSelection().cells.size;Se>1?te(`${Se} cells selected`):te(`Row ${qe}, Column ${nt} selected`)};switch(s.key){case"ArrowUp":s.preventDefault(),Q(-1,0,s.shiftKey);break;case"ArrowDown":s.preventDefault(),Q(1,0,s.shiftKey);break;case"ArrowLeft":s.preventDefault(),Q(0,-1,s.shiftKey);break;case"ArrowRight":s.preventDefault(),Q(0,1,s.shiftKey);break;case"Tab":{if(s.preventDefault(),s.shiftKey){const ne=R?l.indexOf(R.rowId):0;if((R?m.indexOf(R.colId):0)>0)Q(0,-1,!1);else{const ce=Math.max(0,(ne===-1?0:ne)-1),Me=l[ce],_e=m[m.length-1];v.click(Me,_e,l,m),(Ge=(Ee=A.current).onSelectionChange)==null||Ge.call(Ee,v.getSelection()),($e=N.current)==null||$e.markDirty("overlay"),U(ce,m.length-1)}}else{const ne=R?l.indexOf(R.rowId):0;if((R?m.indexOf(R.colId):-1)<m.length-1)Q(0,1,!1);else{const ce=Math.min(l.length-1,(ne===-1?0:ne)+1),Me=l[ce],_e=m[0];v.click(Me,_e,l,m),(ge=(fe=A.current).onSelectionChange)==null||ge.call(fe,v.getSelection()),(ie=N.current)==null||ie.markDirty("overlay"),U(ce,0)}}break}case"Escape":s.preventDefault(),v.clear(),(Ke=(Fe=A.current).onSelectionChange)==null||Ke.call(Fe,v.getSelection()),(Ue=N.current)==null||Ue.markDirty("overlay"),te("Selection cleared");break;case"a":case"A":if(O){s.preventDefault();for(const ne of l)for(const re of m)v.ctrlClick(ne,re);(ft=(Re=A.current).onSelectionChange)==null||ft.call(Re,v.getSelection()),(Tt=N.current)==null||Tt.markDirty("overlay"),te(`${l.length*m.length} cells selected`)}break;case"Home":{s.preventDefault();const ne=R?l.indexOf(R.rowId):0;if(O)v.click(l[0],m[0],l,m),(Wt=(Ht=A.current).onSelectionChange)==null||Wt.call(Ht,v.getSelection()),(Dt=N.current)==null||Dt.markDirty("overlay"),U(0,0);else{const re=ne===-1?0:ne;v.click(l[re],m[0],l,m),(Xt=(Et=A.current).onSelectionChange)==null||Xt.call(Et,v.getSelection()),(Pt=N.current)==null||Pt.markDirty("overlay"),U(re,0)}break}case"End":{s.preventDefault();const ne=R?l.indexOf(R.rowId):0;if(O){const re=l.length-1,ce=m.length-1;v.click(l[re],m[ce],l,m),(Ot=(Lt=A.current).onSelectionChange)==null||Ot.call(Lt,v.getSelection()),(jt=N.current)==null||jt.markDirty("overlay"),U(re,ce)}else{const re=ne===-1?0:ne,ce=m.length-1;v.click(l[re],m[ce],l,m),(zt=(At=A.current).onSelectionChange)==null||zt.call(At,v.getSelection()),(Yt=N.current)==null||Yt.markDirty("overlay"),U(re,ce)}break}}},[]);g.useImperativeHandle(o,()=>({captureToCanvas(s){const f=we.current;if(!f)throw new Error("CanvasGrid: engines not initialized");const{treeEngine:y,columnEngine:k,selectionEngine:v}=f,b=A.current,l=b.rowHeight??Kt,m=b.sidebarWidth??Ft;return Pn({treeEngine:y,columnEngine:k,selectionEngine:v,columnGroups:b.columnGroups,rowHeight:l,sidebarWidth:m,headerOrientation:b.headerOrientation??"horizontal",columnHeaderHeight:b.columnHeaderHeight,gridLayerConfig:{rowStyle:b.rowStyle},cellLayerConfig:{cellRenderer:b.cellRenderer,getCellData:b.getCellData,hoveredCell:null},scale:(s==null?void 0:s.scale)??2,rowRange:s==null?void 0:s.rowRange})},getRowCount(){var s;return((s=we.current)==null?void 0:s.treeEngine.flattenedRows.length)??0}}),[]);const fn=c!=null&&c.length>0,Ct=qt(j??"horizontal",fn,W),un=Ye+he.getTotalWidth()+2,gn=Ct+Oe.flattenedRows.length*D+2,je=12,xt=24,Je=le||Ie,yt=Oe.flattenedRows.length*D,pn=he.getTotalWidth(),ct=he.getPinnedWidth(),Qe=Math.max(0,yt-ee.height),Rt=pn-ct,dt=Math.max(0,ee.width-ct),et=Math.max(0,Rt-dt),St=2,It=Qe>St&&ee.height>0,kt=et>St&&ee.width>0,Ve=ee.height-(kt?je:0),at=Math.min(Ve,Math.max(xt,Ve*(ee.height/yt))),mn=Qe>0?L/Qe*(Ve-at):0,Be=dt-(It?je:0),ht=Math.min(Be,Math.max(xt,Be*(dt/Rt))),wn=et>0?Y/et*(Be-ht):0;return C.jsxs("div",{ref:Pe,tabIndex:0,role:"grid","aria-label":He,"aria-rowcount":Oe.flattenedRows.length,"aria-colcount":he.getVisibleColumns().length,onKeyDown:hn,style:{display:"flex",flexDirection:"column",width:un,height:gn,maxWidth:"100%",maxHeight:_??"100%",overflow:"hidden",fontFamily:"system-ui, sans-serif",outline:"none",border:"1px solid #d1d5db",borderRadius:4},children:[C.jsxs("div",{style:{display:"flex",flexDirection:"row",height:Ct,flexShrink:0},children:[!it&&C.jsx("div",{style:{width:Ye,flexShrink:0,borderRight:"1px solid #e5e7eb",borderBottom:"1px solid #e5e7eb",backgroundColor:"#f9fafb",display:"flex",flexDirection:"column",overflow:"hidden"},children:e.sidebarHeaderRenderer?C.jsx("div",{style:{width:"100%",flex:1},children:e.sidebarHeaderRenderer()}):ue?C.jsx("div",{style:{display:"flex",alignItems:"center",justifyContent:"center",flex:1},children:C.jsx(Hn,{columnEngine:he,onColumnVisibilityChange:an})}):null}),C.jsx(Tn,{columnEngine:he,columnGroups:c,scrollX:Y,orientation:j,columnHeaderHeight:W,onColumnClick:u?rn:void 0,onColumnResize:e.onColumnResize!==void 0?cn:void 0,onColumnReorder:e.onColumnReorder!==void 0?dn:void 0,onColumnFilterClick:e.onColumnFilterClick,selectedColId:((_t=(Mt=e.selection)==null?void 0:Mt.anchor)==null?void 0:_t.colId)??null,columnHeaderRenderer:e.columnHeaderRenderer})]}),C.jsxs("div",{style:{display:"flex",flexDirection:"row",flex:1,overflow:"hidden"},children:[!it&&C.jsx(Rn,{treeEngine:Oe,width:Ye,rowHeight:D,scrollTop:L,viewportHeight:ae.current.canvasHeight||600,sidebarRenderer:p,onToggle:rt,onRowClick:e.onRowHeaderClick?(s,f)=>e.onRowHeaderClick(s,f.nativeEvent):void 0,hoveredRowId:se,onRowHover:s=>{var f;pe.current=s,ye(s),(f=N.current)==null||f.markDirty("overlay")}}),C.jsxs("div",{style:{flex:1,position:"relative",overflow:"hidden"},onMouseEnter:()=>me(!0),onMouseLeave:()=>me(!1),children:[C.jsx("canvas",{ref:s=>{s&&(ke.current[0]=s)},style:{position:"absolute",top:0,left:0,zIndex:0}}),C.jsx("canvas",{ref:s=>{s&&(ke.current[1]=s)},style:{position:"absolute",top:0,left:0,zIndex:1}}),C.jsx("canvas",{ref:s=>{s&&(ke.current[2]=s)},style:{position:"absolute",top:0,left:0,zIndex:2},onPointerMove:tn,onPointerDown:nn,onDoubleClick:on,onContextMenu:sn,onPointerLeave:ln}),It&&C.jsx("div",{style:{position:"absolute",top:0,right:0,width:je,height:Ve,zIndex:3,opacity:Je?1:0,transition:"opacity 150ms ease",pointerEvents:Je?"auto":"none"},children:C.jsx("div",{onMouseDown:s=>vt("y",s,Ve-at,Qe),style:{position:"absolute",top:mn,left:2,width:je-4,height:at,borderRadius:8,backgroundColor:"rgba(0, 0, 0, 0.3)",cursor:"pointer"}})}),kt&&C.jsx("div",{style:{position:"absolute",bottom:0,left:ct,height:je,width:Be,zIndex:3,opacity:Je?1:0,transition:"opacity 150ms ease",pointerEvents:Je?"auto":"none"},children:C.jsx("div",{onMouseDown:s=>vt("x",s,Be-ht,et),style:{position:"absolute",left:wn,top:2,height:je-4,width:ht,borderRadius:8,backgroundColor:"rgba(0, 0, 0, 0.3)",cursor:"pointer"}})})]})]}),i!=null&&q!=null&&C.jsx(Wn,{content:i,position:q}),C.jsx(Dn,{message:xe})]})}));exports.CanvasGrid=jn;exports.ColumnEngine=De;exports.SelectionEngine=Zt;exports.TreeEngine=Nt;exports.ViewState=pt;exports.createDrawHelpers=Qt;