/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import"../../../../core/has.js";import{createResolver as e,throwIfAborted as t,onAbortOrThrow as s}from"../../../../core/promiseUtils.js";import{textureUploadManagerChunkSize as o,textureUploadManagerBudget as r}from"./definitions.js";class u{constructor(e){this._queue=[],this._refreshable=e}destroy(){this._queue=[]}enqueueTextureUpdate(r,u){const n=e(),h=r,i=o,a=Math.ceil(h.height/i);t(u);for(let e=0;e<a;e++){const t=e*i,s=e===a-1,o=s?h.height-i*e:i;this._queue.push({type:"chunk",request:r,resolver:n,chunk:e,chunkOffset:t,destHeight:o,chunkIsLast:s,options:u})}return s(u,e=>n.reject(e)),n.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 o=performance.now()-e;if(o+o/t>=r)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{u as TextureUploadManager};
