/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as t}from"tslib";import e from"../../../../core/Accessor.js";import"../../../../core/has.js";import{debounce as i,throwIfAborted as o,throwIfAbortError as r,eachAlways as a}from"../../../../core/promiseUtils.js";import{property as s,subclass as n}from"../../../../core/accessorSupport/decorators.js";import{toExtent as p,create as m}from"../../../../geometry/support/aaBoundingRect.js";import{getInfo as h}from"../../../../geometry/support/spatialReferenceUtils.js";import d from"../../../../layers/support/TileInfo.js";import{getOuterSize as l,getBBox as c}from"../../viewStateUtils.js";import{Bitmap as u}from"../../engine/Bitmap.js";import g from"../../tiling/TileInfoView.js";import f from"../../tiling/TileKey.js";const x=m(),y=[0,0],S=new f(0,0,0,0),w={imageMaxWidth:2048,imageMaxHeight:2048,imageRotationSupported:!1,imageNormalizationSupported:!1,hidpi:!1};let M=class extends e{constructor(t){super(t),this._imagePromise=null,this.bitmaps=[],this.hidpi=w.hidpi,this.imageMaxWidth=w.imageMaxWidth,this.imageMaxHeight=w.imageMaxHeight,this.imageRotationSupported=w.imageRotationSupported,this.imageNormalizationSupported=w.imageNormalizationSupported,this.update=i(async(t,e)=>{if(o(e),!t.stationary||this.destroyed)return;const i=t.state,a=h(i.spatialReference),s=this.hidpi?t.pixelRatio:1,n=i.worldScreenWidth>0,p=n&&this.imageNormalizationSupported&&i.worldScreenWidth<i.size[0],m=Math.round((this.imageMaxWidth??0)/s),d=Math.round((this.imageMaxHeight??0)/s);p?(y[0]=i.worldScreenWidth,y[1]=i.size[1]):this.imageRotationSupported?(y[0]=i.size[0],y[1]=i.size[1]):l(y,i);const c=Math.floor(y[0])>m||Math.floor(y[1])>d,u=a&&(i.extent.xmin<a.valid[0]||i.extent.xmax>a.valid[1]),g=!this.imageNormalizationSupported&&u,f=!c&&!g,x=this.imageRotationSupported?i.rotation:0,S=this.container.children.slice();if(f){const t=p?i.paddedViewState.center:i.center;this._imagePromise=this._singleExport(i,y,t,i.resolution,x,s,e)}else{let t=Math.min(m,d);n&&(t=Math.min(i.worldScreenWidth,t),t=Math.round(i.worldScreenWidth/Math.ceil(i.worldScreenWidth/t))),this._imagePromise=this._tiledExport(i,t,s,e)}try{const t=await this._imagePromise??[];o(e);const i=[];if(this._imagePromise=null,this.destroyed)return;this.bitmaps=t;for(const e of S)t.includes(e)||i.push(e.fadeOut().then(()=>{e.remove(),e.destroy()}));for(const e of t)i.push(e.fadeIn());await Promise.all(i)}catch(w){this._imagePromise=null,r(w)}},5e3),this.updateExports=i(async t=>{const e=[];for(const i of this.container.children){if(!i.visible||!i.stage)return;e.push(t(i).then(()=>{i.invalidateTexture(),i.requestRender()}))}this._imagePromise=a(e).then(()=>this._imagePromise=null),await this._imagePromise})}destroy(){this.bitmaps.forEach(t=>t.destroy()),this.bitmaps=[]}get updating(){return!this.destroyed&&null!==this._imagePromise}async _export(t,e,i,r,a,s){const n=await this.fetchSource(t,Math.floor(e*a),Math.floor(i*a),{rotation:r,pixelRatio:a,signal:s});o(s);const p=new u(null,!0);return p.x=t.xmin,p.y=t.ymax,p.resolution=t.width/e,p.rotation=r,p.pixelRatio=a,p.opacity=0,this.container.addChild(p),await p.setSourceAsync(n,s),o(s),p}async _singleExport(t,e,i,o,r,a,s){c(x,i,o,e);const n=p(x,t.spatialReference);return[await this._export(n,e[0],e[1],r,a,s)]}_tiledExport(t,e,i,o){const r=d.create({size:e,spatialReference:t.spatialReference,scales:[t.scale]}),a=new g(r),s=a.getTileCoverage(t);if(!s)return null;const n=[];return s.forEach((r,s,m,h)=>{S.set(r,s,m,0),a.getTileBounds(x,S);const d=p(x,t.spatialReference);n.push(this._export(d,e,e,0,i,o).then(t=>(0!==h&&(S.set(r,s,m,h),a.getTileBounds(x,S),t.x=x[0],t.y=x[3]),t)))}),Promise.all(n)}};t([s()],M.prototype,"_imagePromise",void 0),t([s()],M.prototype,"bitmaps",void 0),t([s()],M.prototype,"container",void 0),t([s()],M.prototype,"fetchSource",void 0),t([s()],M.prototype,"hidpi",void 0),t([s()],M.prototype,"imageMaxWidth",void 0),t([s()],M.prototype,"imageMaxHeight",void 0),t([s()],M.prototype,"imageRotationSupported",void 0),t([s()],M.prototype,"imageNormalizationSupported",void 0),t([s()],M.prototype,"requestUpdate",void 0),t([s()],M.prototype,"updating",null),M=t([n("esri.views.2d.layers.support.ExportStrategy")],M);export{M as default};
