(function(n,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("../../core/plugin/base-plugin")):typeof define=="function"&&define.amd?define(["exports","../../core/plugin/base-plugin"],r):(n=typeof globalThis<"u"?globalThis:n||self,r(n.TbwGridPlugin_serverSide={},n.TbwGrid))})(this,(function(n,r){"use strict";function a(s,e){return Math.floor(s/e)}function u(s,e){return{start:s*e,end:(s+1)*e}}function h(s,e,t){const o=a(s,t),l=a(e-1,t),i=[];for(let c=o;c<=l;c++)i.push(c);return i}async function d(s,e,t,o){const l=u(e,t);return s.getRows({startRow:l.start,endRow:l.end,sortModel:o.sortModel,filterModel:o.filterModel})}function k(s,e,t){const o=a(s,e),l=t.get(o);if(!l)return;const i=s%e;return l[i]}const f=100;class g extends r.BaseGridPlugin{name="serverSide";get defaultConfig(){return{pageSize:100,cacheBlockSize:100,maxConcurrentRequests:2}}dataSource=null;totalRowCount=0;loadedBlocks=new Map;loadingBlocks=new Set;lastRequestId=0;scrollDebounceTimer;detach(){this.dataSource=null,this.totalRowCount=0,this.loadedBlocks.clear(),this.loadingBlocks.clear(),this.lastRequestId=0,this.scrollDebounceTimer&&(clearTimeout(this.scrollDebounceTimer),this.scrollDebounceTimer=void 0)}loadRequiredBlocks(){if(!this.dataSource)return;const e=this.grid,t=this.config.cacheBlockSize??100,o={startRow:e._virtualization.start,endRow:e._virtualization.end},l=h(o.startRow,o.endRow,t);for(const i of l)if(!(this.loadedBlocks.has(i)||this.loadingBlocks.has(i))){if(this.loadingBlocks.size>=(this.config.maxConcurrentRequests??2))break;this.loadingBlocks.add(i),d(this.dataSource,i,t,{}).then(c=>{this.loadedBlocks.set(i,c.rows),this.totalRowCount=c.totalRowCount,this.loadingBlocks.delete(i),this.requestRender(),this.loadRequiredBlocks()}).catch(()=>{this.loadingBlocks.delete(i)})}}processRows(e){if(!this.dataSource)return[...e];const t=[];for(let o=0;o<this.totalRowCount;o++){const l=k(o,this.config.cacheBlockSize??100,this.loadedBlocks);t.push(l??{__loading:!0,__index:o})}return t}onScroll(e){this.dataSource&&(this.loadRequiredBlocks(),this.scrollDebounceTimer&&clearTimeout(this.scrollDebounceTimer),this.scrollDebounceTimer=setTimeout(()=>{this.loadRequiredBlocks()},f))}setDataSource(e){this.dataSource=e,this.loadedBlocks.clear(),this.loadingBlocks.clear();const t=this.config.cacheBlockSize??100;d(e,0,t,{}).then(o=>{this.loadedBlocks.set(0,o.rows),this.totalRowCount=o.totalRowCount,this.requestRender()})}refresh(){this.dataSource&&(this.loadedBlocks.clear(),this.loadingBlocks.clear(),this.requestRender())}purgeCache(){this.loadedBlocks.clear()}getTotalRowCount(){return this.totalRowCount}isRowLoaded(e){const t=this.config.cacheBlockSize??100,o=a(e,t);return this.loadedBlocks.has(o)}getLoadedBlockCount(){return this.loadedBlocks.size}}n.ServerSidePlugin=g,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
//# sourceMappingURL=server-side.umd.js.map
