/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../chunks/_rollupPluginBabelHelpers","../../../../chunks/tslib.es6","../../../../core/Accessor","../../../../core/promiseUtils","../../../../core/accessorSupport/decorators/property","../../../../core/arrayUtils","../../../../core/has","../../../../core/accessorSupport/ensureType","../../../../core/accessorSupport/decorators/subclass","../../../../geometry/Extent","../../../../geometry/support/aaBoundingRect","../../../../geometry/support/spatialReferenceUtils","../../../../layers/support/TileInfo","../../viewStateUtils","../../engine/Bitmap","../../tiling/TileInfoView","../../tiling/TileKey"],(function(e,t,o,r,i,a,n,p,s,c,l,u,d,m,g,h,y){"use strict";const f=l.create(),_=[0,0],x=new y(0,0,0,0),S={container:null,fetchSource:null,requestUpdate:null,imageMaxWidth:2048,imageMaxHeight:2048,imageRotationSupported:!1,imageNormalizationSupported:!1,hidpi:!1};let v=function(t){function o(o){var i;return(i=t.call(this,o)||this)._imagePromise=null,i.bitmaps=[],i.hidpi=S.hidpi,i.imageMaxWidth=S.imageMaxWidth,i.imageMaxHeight=S.imageMaxHeight,i.imageRotationSupported=S.imageRotationSupported,i.imageNormalizationSupported=S.imageNormalizationSupported,i.update=r.debounce(function(){var t=e._asyncToGenerator((function*(t,o){if(!t.stationary||i.destroyed)return null;const r=t.state,a=u.getInfo(r.spatialReference),n=i.hidpi?t.pixelRatio:1,p=i.imageNormalizationSupported&&r.worldScreenWidth&&r.worldScreenWidth<r.size[0];p?(_[0]=r.worldScreenWidth,_[1]=r.size[1]):i.imageRotationSupported?(_[0]=r.size[0],_[1]=r.size[1]):m.getOuterSize(_,r);const s=Math.floor(_[0]*n)>i.imageMaxWidth||Math.floor(_[1]*n)>i.imageMaxHeight,c=a&&(r.extent.xmin<a.valid[0]||r.extent.xmax>a.valid[1]),l=!i.imageNormalizationSupported&&c,d=!s&&!l,g=i.imageRotationSupported?r.rotation:0;if(d){const e=p?r.paddedViewState.center:r.center;i._imagePromise=i._singleExport(r,_,e,r.resolution,g,n,o)}else{let e=Math.min(i.imageMaxWidth,i.imageMaxHeight);l&&(e=Math.min(r.worldScreenWidth,e)),i._imagePromise=i._tiledExport(r,e,n,o)}return i._imagePromise.then(function(){var t=e._asyncToGenerator((function*(e){if(i._imagePromise=null,!i.destroyed){i.bitmaps=null!=e?e:[];for(const t of i.container.children)e.includes(t)||t.fadeOut().then((()=>{t.remove()}));for(const t of e)i.container.addChild(t),t.fadeIn()}}));return function(e){return t.apply(this,arguments)}}()).catch((e=>{throw i._imagePromise=null,e}))}));return function(e,o){return t.apply(this,arguments)}}(),5e3),i}e._inheritsLoose(o,t);var i=o.prototype;return i.destroy=function(){this.bitmaps=[]},i.updateExports=function(e){for(const t of this.container.children){if(!t.visible||!t.stage)return;e(t),t.invalidateTexture(),t.requestRender()}},i._export=function(){var t=e._asyncToGenerator((function*(e,t,o,r,i,a){const n=yield this.fetchSource(e,Math.floor(t*i),Math.floor(o*i),{rotation:r,pixelRatio:i,signal:a}),p=new g.Bitmap(n,"additive");return p.x=e.xmin,p.y=e.ymax,p.resolution=e.width/t,p.rotation=r,p.pixelRatio=i,p}));function o(e,o,r,i,a,n){return t.apply(this,arguments)}return o}(),i._singleExport=function(){var t=e._asyncToGenerator((function*(e,t,o,r,i,a,n){m.getBBox(f,o,r,t);const p=new c(f[0],f[1],f[2],f[3],e.spatialReference);return[yield this._export(p,t[0],t[1],i,a,n)]}));function o(e,o,r,i,a,n,p){return t.apply(this,arguments)}return o}(),i._tiledExport=function(e,t,o,r){const i=d.create({size:t,spatialReference:e.spatialReference,scales:[e.scale]}),a=new h(i),n=a.getTileCoverage(e);if(!n)return null;const p=[];return n.forEach(((i,n,s,l)=>{x.set(i,n,s,l),a.getTileBounds(f,x);const u=new c(f[0],f[1],f[2],f[3],e.spatialReference);p.push(this._export(u,t,t,0,o,r))})),Promise.all(p)},e._createClass(o,[{key:"updating",get:function(){return!this.destroyed&&null!==this._imagePromise}}]),o}(o);t.__decorate([i.property()],v.prototype,"_imagePromise",void 0),t.__decorate([i.property()],v.prototype,"bitmaps",void 0),t.__decorate([i.property()],v.prototype,"container",void 0),t.__decorate([i.property()],v.prototype,"fetchSource",void 0),t.__decorate([i.property()],v.prototype,"hidpi",void 0),t.__decorate([i.property()],v.prototype,"imageMaxWidth",void 0),t.__decorate([i.property()],v.prototype,"imageMaxHeight",void 0),t.__decorate([i.property()],v.prototype,"imageRotationSupported",void 0),t.__decorate([i.property()],v.prototype,"imageNormalizationSupported",void 0),t.__decorate([i.property()],v.prototype,"requestUpdate",void 0),t.__decorate([i.property()],v.prototype,"updating",null),v=t.__decorate([s.subclass("esri.views.2d.layers.support.ExportStrategy")],v);return v}));
