/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import{__decorate as t}from"tslib";import e from"../../../../core/Accessor.js";import"../../../../core/has.js";import i from"../../../../core/Logger.js";import{isAbortError as r}from"../../../../core/promiseUtils.js";import{property as s,subclass as a}from"../../../../core/accessorSupport/decorators.js";import{mosaicPixelData as o}from"../../../../layers/raster/functions/pixelUtils.js";import{BitmapContainer as l}from"../../engine/BitmapContainer.js";import{Container as n}from"../../engine/Container.js";import p from"../../engine/ImageryBitmapSource.js";import h from"../support/ExportStrategy.js";import{readFromPixelData as c}from"../../../support/rasterHitTestUtils.js";let g=class extends e{constructor(t){super(t),this.attached=!1,this.container=new n,this.updateRequested=!1,this.pixelHighlights=[],this.type="imagery",this._bitmapView=new l}destroy(){this.attached&&(this.detach(),this.attached=!1),this.updateRequested=!1}get updating(){return!this.attached||this.isUpdating()}update(t){this.updatingHandles.consumePromise(this.strategy.update(t).catch(t=>{r(t)||i.getLogger(this).error(t)}))}async hitTest(t){if(this.layer.isRenderedFormat)return null;let e=null;const{bitmaps:i}=this.strategy;if(1===i.length&&i[0].source)e={extent:i[0].source.extent,pixelBlock:i[0].source.originalPixelBlock};else if(i.length>1){const r=i.map(t=>t.source).find(e=>e.extent&&e.extent.contains(t));r&&(e={extent:r.extent,pixelBlock:r.originalPixelBlock})}return e?.pixelBlock?await c({mapPoint:t,layer:this.layer,pixelData:e}):null}attach(){this.container.addChild(this._bitmapView);const t=this.layer.version>=10,e=this.layer.version>=10.1?this.layer.imageMaxHeight:2048,i=this.layer.version>=10.1?this.layer.imageMaxWidth:2048;this.strategy=new h({container:this._bitmapView,imageNormalizationSupported:t,imageMaxHeight:e,imageMaxWidth:i,fetchSource:this._fetchImage.bind(this),requestUpdate:()=>this.requestUpdate()})}detach(){this.strategy.destroy(),this._bitmapView.removeAllChildren(),this.container.removeAllChildren(),this.updateRequested=!1}redraw(){const t=this.strategy.updateExports(async t=>{const{source:e}=t;if(!e||e instanceof ImageBitmap)return;const i=e.originalPixelBlock??e.pixelBlock,{pixelHighlights:r}=this,s=r.length>0,a=await this.layer.applyRenderer({extent:e.extent,pixelBlock:i,isRawData:s}),o=a.pixelBlock;s&&i&&o&&await this.layer.highlightPixels({pixelBlock:i,renderedPixelBlock:o,highlightOptions:r}),e.filter=t=>this.layer.pixelFilter?this.layer.applyFilter(t):{...a,extent:e.extent}});this.updatingHandles.consumePromise(t),t.catch(t=>{r(t)||i.getLogger(this).error(t)})}requestUpdate(){this.updateRequested||(this.updateRequested=!0,this.view.requestUpdate())}isUpdating(){return this.updateRequested||this.updatingHandles.updating}getPixelData(){if(this.updating)return null;const t=this.strategy.bitmaps;if(1===t.length&&t[0].source)return{extent:t[0].source.extent,pixelBlock:t[0].source.originalPixelBlock};if(t.length>1){const e=this.view.extent,i=t.map(t=>t.source).filter(t=>t.extent&&t.extent.intersects(e)).map(t=>({extent:t.extent,pixelBlock:t.originalPixelBlock})),r=o(i,e);return null!=r?{extent:r.extent,pixelBlock:r.pixelBlock}:null}return null}async _fetchImage(t,e,i,r){(r=r||{}).timeExtent=this.timeExtent;const s=this.pixelHighlights.length>0;r.requestAsImageElement=!s,r.returnImageBitmap=!s,r.requestRawData=s;const a=await this.layer.internalFetchImage(t,e,i,r);if(a.imageBitmap)return a.imageBitmap;const o=await this.layer.applyRenderer({...a.pixelData,isRawData:s},{signal:r.signal}),l=a.pixelData.pixelBlock,n=o.pixelBlock;s&&l&&n&&await this.layer.highlightPixels({pixelBlock:l,renderedPixelBlock:n,highlightOptions:this.pixelHighlights});const h=new p(n,o.extent?.clone(),l);return h.filter=t=>this.layer.applyFilter(t),h}};t([s()],g.prototype,"attached",void 0),t([s()],g.prototype,"container",void 0),t([s()],g.prototype,"layer",void 0),t([s()],g.prototype,"strategy",void 0),t([s()],g.prototype,"timeExtent",void 0),t([s()],g.prototype,"view",void 0),t([s()],g.prototype,"updateRequested",void 0),t([s()],g.prototype,"updatingHandles",void 0),t([s()],g.prototype,"updating",null),t([s()],g.prototype,"pixelHighlights",void 0),t([s()],g.prototype,"type",void 0),g=t([a("esri.views.2d.layers.imagery.ImageryView2D")],g);export{g as default};
