/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../../../request.js";import{createTask as s}from"../../../core/asyncUtils.js";import r from"../../../core/Handles.js";import{destroyMaybe as i,abortMaybe as t}from"../../../core/maybe.js";import{watch as n,initial as a}from"../../../core/reactiveUtils.js";import{create as o}from"../../../core/libs/gl-matrix-2/factories/mat3f32.js";import{UpdatingHandles as l}from"../../../core/support/UpdatingHandles.js";import{DisplayObject as h}from"../engine/DisplayObject.js";import{MagnifierTechnique as u}from"../engine/webgl/shaderGraph/techniques/magnifier/MagnifierTechnique.js";import{loadMagnifierResources as m}from"../../magnifier/resources.js";class d extends h{constructor(){super(),this._handles=new r,this._magnifierTechnique=new u,this.updatingHandles=new l,this.visible=!1}destroy(){super.destroy(),this._handles=i(this._handles),this._magnifierTechnique.shutdown(),this._resourcesTask=t(this._resourcesTask)}get backgroundColor(){return this._backgroundColor}set backgroundColor(e){this._backgroundColor=e,this.requestRender()}get magnifier(){return this._magnifier}set magnifier(e){this._magnifier=e,this._handles.removeAll(),this._handles.add([n(()=>e.version,()=>{this.visible=e.visible&&null!=e.position&&e.size>0,this.requestRender()},a),n(()=>[e.maskUrl,e.overlayUrl],()=>this._reloadResources()),n(()=>e.size,()=>{this._magnifierTechnique.shutdown(),this.requestRender()})])}_createTransforms(){return{displayViewScreenMat3:o()}}doRender(e){if(!this._resourcesTask)return void this._reloadResources();if(1!==e.drawPhase||!this._canRender())return;const s=this._magnifier;if(null==s.position)return;this._magnifierTechnique.updateResources(e,this._mask,this._overlay,s);const r=this.backgroundColor,i=r?[r.a*r.r/255,r.a*r.g/255,r.a*r.b/255,r.a]:[1,1,1,1];this._magnifierTechnique.render(e,{background:i,maskEnabled:s.maskEnabled?1:0,overlayEnabled:s.overlayEnabled?1:0})}_canRender(){return this._mask&&this._overlay&&null!=this._magnifier}_reloadResources(){this._resourcesTask&&this._resourcesTask.abort();const r=null!=this._magnifier?this._magnifier.maskUrl:null,i=null!=this._magnifier?this._magnifier.overlayUrl:null;this._resourcesTask=s(async s=>{const t=null==r||null==i?m(s):null,n=null!=r?e(r,{responseType:"image",signal:s}).then(e=>e.data):t.then(e=>e.mask),a=null!=i?e(i,{responseType:"image",signal:s}).then(e=>e.data):t.then(e=>e.overlay),[o,l]=await Promise.all([n,a]);this._mask=o,this._overlay=l,this._magnifierTechnique.shutdown(),this.requestRender()}),this.updatingHandles.consumePromise(this._resourcesTask.promise)}}export{d as default};
