/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{throwIfAborted as t}from"../../../../../core/promiseUtils.js";import{timeExtentsEqual as r,setUniforms as a}from"../utils.js";import{AFlowResources as e}from"./AFlowResources.js";import{PrimitiveType as s,DataType as o}from"../../../../webgl/enums.js";import i from"../../../../webgl/Texture.js";import{TextureDescriptor as n}from"../../../../webgl/TextureDescriptor.js";import{VertexArrayObject as m}from"../../../../webgl/VertexArrayObject.js";import{fromLayout as l}from"../../../../webgl/VertexAttributeLocations.js";import{VertexBuffer as p}from"../../../../webgl/VertexBuffer.js";import{VertexElementDescriptor as c}from"../../../../webgl/VertexElementDescriptor.js";class h{constructor(t){this._params=t,this.supportsContinuation=!1,this.slideoutDuration=0,this.animated=!1}isCompatible(t){if(!(t instanceof h))return!1;if(!r(this._params.timeExtent,t._params.timeExtent))return!1;let a=!0;return a=a&&this._params.loadImagery===t._params.loadImagery,a=a&&this._params.color.kind===t._params.color.kind,a=a&&this._params.opacity.kind===t._params.opacity.kind,a}async load(r,a,e){const{extent:s,size:o}=r;t(e);const i=await this._params.loadImagery(s,o[0],o[1],this._params.timeExtent,e);return new d(i,r,[],{color:this._params.color,opacity:this._params.opacity})}render(t,r,e){const{context:o}=t,{program:i}=e;o.setFaceCullingEnabled(!1),o.setBlendingEnabled(!0),o.setBlendFunction(1,771),o.useProgram(i),i.setUniformMatrix3fv("u_dvsMat3",r.dvsMat3),o.bindTexture(e.texture,0),i.setUniform1i("u_texture",0),i.setUniform1f("u_Min",e.min),i.setUniform1f("u_Max",e.max),a(i,"color","vec4",this._params.color),a(i,"opacity","float",this._params.opacity),o.bindVAO(e.vertexArray),o.drawArrays(s.TRIANGLE_STRIP,0,4)}}const f=[new c("a_position",2,o.UNSIGNED_SHORT,0,8),new c("a_texcoord",2,o.UNSIGNED_SHORT,4,8)],u={vsPath:"raster/flow/imagery",fsPath:"raster/flow/imagery",locations:l(f)};class d extends e{constructor(t,r,a,e){super(r,a),this._flowData=t,this._values=e}attach(t){const{context:r}=t,{width:a,height:e}=this._flowData,s=new p(r,f,new Uint16Array([0,0,0,1,a,0,1,1,0,e,0,0,a,e,1,0])),o=new m(r,s),l=[];"ramp"===this._values.color.kind&&l.push("vvColor"),"ramp"===this._values.opacity.kind&&l.push("vvOpacity");const c=t.getProgram(u,l);let h=1e6,d=-1e6;for(let i=0;i<e;i++)for(let t=0;t<a;t++)if(0!==this._flowData.mask[i*a+t]){const r=this._flowData.data[2*(i*a+t)],e=this._flowData.data[2*(i*a+t)+1],s=Math.sqrt(r*r+e*e);h=Math.min(h,s),d=Math.max(d,s)}const _=new Uint8Array(4*a*e);for(let i=0;i<e;i++)for(let t=0;t<a;t++)if(0!==this._flowData.mask[i*a+t]){const r=this._flowData.data[2*(i*a+t)],e=this._flowData.data[2*(i*a+t)+1],s=(Math.sqrt(r*r+e*e)-h)/(d-h);_[4*(i*a+t)]=255*s,_[4*(i*a+t)+1]=0,_[4*(i*a+t)+2]=0,_[4*(i*a+t)+3]=255}else _[4*(i*a+t)]=0,_[4*(i*a+t)+1]=0,_[4*(i*a+t)+2]=0,_[4*(i*a+t)+3]=0;const w=new n(a,e);w.internalFormat=6408,w.wrapMode=33071,w.flipped=!0;const x=new i(r,w,_);this.vertexArray=o,this.program=c,this.texture=x,this.min=h,this.max=d,this._flowData=null}detach(){this.vertexArray.dispose(),this.texture.dispose()}get ready(){return this.program.compiled}}export{h as Imagery,d as ImageryResources};
