/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{throwIfAborted as t}from"../../../../../core/promiseUtils.js";import{timeExtentsEqual as a,getFlowFloatUniforms as s,getFlowColorUniforms as e}from"../utils.js";import{AFlowResources as i}from"./AFlowResources.js";import{Mesh as r}from"../../webgl/meshing/Mesh.js";import{FlowImageryShader as o}from"../../webgl/shaderGraph/techniques/shaders/flow/FlowImageryShader.js";import{PrimitiveType as m,DataType as n}from"../../../../webgl/enums.js";import p from"../../../../webgl/Texture.js";import{TextureDescriptor as l}from"../../../../webgl/TextureDescriptor.js";import{VertexElementDescriptor as h}from"../../../../webgl/VertexElementDescriptor.js";class c{constructor(t){this._params=t,this.supportsContinuation=!1,this.slideoutDuration=0,this.animated=!1}isCompatible(t){if(!(t instanceof c))return!1;if(!a(this._params.timeExtent,t._params.timeExtent))return!1;let s=!0;return s=s&&this._params.loadImagery===t._params.loadImagery,s=s&&this._params.color.kind===t._params.color.kind,s=s&&this._params.opacity.kind===t._params.opacity.kind,s}updateResource(t){t.update(this._params.color,this._params.opacity)}async load(a,s,e){const{extent:i,size:r}=a;t(e);const o=await this._params.loadImagery(i,r[0],r[1],this._params.timeExtent,e);return new u(o,a,[],o,{color:this._params.color,opacity:this._params.opacity})}}const d=[new h("position",2,n.UNSIGNED_SHORT,0,8),new h("texcoord",2,n.UNSIGNED_SHORT,4,8)];class u extends i{static{this._shader=new o}constructor(t,a,s,e,i){super(a,s,e),this._flowData=t,this._values=i,this.kind="imagery"}attach(t){const{context:a}=t,{width:s,height:e}=this._flowData;this.mesh=new r(a,{vertex:{geometry:{data:new Uint16Array([0,0,0,1,s,0,1,1,0,e,0,0,s,e,1,0]),layout:d}},groups:[{primitive:m.TRIANGLE_STRIP}],parts:[{group:0,start:0,count:4}]});let i=1e6,o=-1e6;for(let r=0;r<e;r++)for(let t=0;t<s;t++)if(0!==this._flowData.mask[r*s+t]){const a=this._flowData.data[2*(r*s+t)],e=this._flowData.data[2*(r*s+t)+1],m=Math.sqrt(a*a+e*e);i=Math.min(i,m),o=Math.max(o,m)}const n=new Uint8Array(4*s*e);for(let r=0;r<e;r++)for(let t=0;t<s;t++)if(0!==this._flowData.mask[r*s+t]){const a=this._flowData.data[2*(r*s+t)],e=this._flowData.data[2*(r*s+t)+1],m=(Math.sqrt(a*a+e*e)-i)/(o-i);n[4*(r*s+t)]=255*m,n[4*(r*s+t)+1]=0,n[4*(r*s+t)+2]=0,n[4*(r*s+t)+3]=255}else n[4*(r*s+t)]=0,n[4*(r*s+t)+1]=0,n[4*(r*s+t)+2]=0,n[4*(r*s+t)+3]=0;const h=new l(s,e);h.internalFormat=6408,h.wrapMode=33071,h.flipped=!0;const c=new p(a,h,n);this.texture=c,this.min=i,this.max=o,this._flowData=null}detach(){this.mesh.destroy(),this.texture.dispose()}get ready(){return!0}update(t,a){this._values={color:t,opacity:a}}getProgramSpec(t){return{shader:u._shader,uniforms:{state:{time:t.time,dvsMat3:t.dvsMat3,displayViewMat3:t.displayViewMat3,displayOpacity:t.displayOpacity,startTime:t.startTime,endTime:t.endTime},config:{min:this.min,max:this.max},color:e(this._values.color),opacity:s(this._values.opacity)},textures:{texture:{texture:this.texture,unit:0}},defines:{vvColor:"ramp"===this._values.color.kind,vvOpacity:"ramp"===this._values.opacity.kind},optionalAttributes:null}}}export{c as Imagery,u as ImageryResources};
