/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{throwIfAborted as e,onAbortOrThrow as t}from"../../../../core/promiseUtils.js";import{textureUploadManagerChunkSize as s,textureUploadManagerBudget as o}from"./definitions.js";class r{constructor(e){this._queue=[],this._refreshable=e}destroy(){this._queue=[]}enqueueTextureUpdate(o,r){const u=Promise.withResolvers(),n=o,h=s,i=Math.ceil(n.height/h);e(r);for(let e=0;e<i;e++){const t=e*h,s=e===i-1,a=s?n.height-h*e:h;this._queue.push({type:"chunk",request:o,resolver:u,chunk:e,chunkOffset:t,destHeight:a,chunkIsLast:s,options:r})}return t(r,e=>u.reject(e)),u.promise}upload(){const e=performance.now();let t=0;for(;this._queue.length;){const s=this._queue.shift();if(s){if(null!=s.options.signal&&s.options.signal.aborted)continue;switch(s.type){case"chunk":this._uploadChunk(s);break;case"no-chunk":this._uploadNoChunk(s)}++t;const r=performance.now()-e;if(r+r/t>=o)break}}this._queue.length&&this._refreshable.requestRender()}_uploadChunk(e){const{request:t,resolver:s,chunkOffset:o,chunkIsLast:r,destHeight:u}=e,{data:n,texture:h,width:i}=t;null!=n&&(h.updateData(0,0,o,i,u,n,o),r&&s.resolve())}_uploadNoChunk(e){const{request:t,resolver:s}=e,{data:o,texture:r}=t;r.setData(o),s.resolve()}}export{r as TextureUploadManager};
