/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{on as t}from"../../../../core/events.js";import e from"../../../../core/Handles.js";import{disposeMaybe as r}from"../../../../core/maybe.js";import{when as i,initial as o}from"../../../../core/reactiveUtils.js";import{createScreenPoint as s}from"../../../../core/screenUtils.js";import{create as n}from"../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import a from"../../../../geometry/Point.js";import l from"../../../../geometry/Polygon.js";import{project as h}from"../../../../geometry/projectionUtils.js";import u from"../../../../layers/support/ControlPoint.js";import{DisplayObject as p}from"../DisplayObject.js";import m from"../../../layers/support/GeometryClipArea.js";import d from"../../../webgl/Texture.js";import{TextureDescriptor as c}from"../../../webgl/TextureDescriptor.js";const f=2;class x extends p{constructor(r){super(),this.element=r,this._handles=new e,this.isWrapAround=!1,this.perspectiveTransform=n(),this.wrapAroundShift=0,this.clipGeometry=null,this._handles.add(i(()=>this.element,()=>{const e=this.element;this.ready(),e&&this._handles.add(t(e,"play",()=>this.requestRender()))},o))}getMesh(t){throw new Error("Method not implemented.")}destroy(){super.destroy(),this._handles.destroy(),this.texture=r(this.texture)}get textureSize(){if(!this.texture)return[1,1];const t=this.texture.descriptor;return[t.width,t.height]}get dvsMat3(){return this.parent.dvsMat3}beforeRender(t){const e=this.element;if(null==e)return;const{context:r}=t,{videoWidth:i,videoHeight:o}=e;if(0!==i&&0!==o){if(this.texture)e.paused||t.animationsEnabled&&this.texture.setData(e);else{const t=new c(i,o);t.wrapMode=33071,t.preMultiplyAlpha=!0,this.texture=new d(r,t,e)}e.paused||this.texture.generateMipmap(),super.beforeRender(t)}}_createTransforms(){return null}updateDrawCoords(t,e,r,i){const o=this.element,s=this._getFrameInfo();if(!o||!s)return;const{spatialReference:n}=r;this._initializeData(t,s,n);const{controlPoints:a,horizon:u}=s,p=Math.sqrt(a.length),d=p,{x:c,y:x}=t,y=this._vertices,g=a[0],w=a[p-1],P=a[(d-1)*p],_=a[(d-1)*p+p-1],v=h(u?u[0].mapPoint:g.mapPoint,n),j=h(u?u[1].mapPoint:w.mapPoint,n),A=h(P.mapPoint,n),M=h(_.mapPoint,n);this.clipGeometry=u?new m({geometry:l.fromJSON({rings:[[[A.x,A.y],[M.x,M.y],[j.x,j.y],[v.x,v.y],[A.x,A.y]]],spatialReference:n})}):null;for(let l=0;l<a.length;l++){const t=a[l],{sourcePoint:e,mapPoint:r}=t;if(null==e||null==r)continue;const i=h(r,n);y[l*f+0]=i.x-c,y[l*f+1]=i.y-x}let C=e;if(i){const t=Math.min(v.x,j.x,A.x,M.x),e=Math.max(v.x,j.x,A.x,M.x),{worldWidth:r,xBounds:o}=i,[s,n]=o;t<s&&e>s?C=r:e>n&&t<n&&(C=-r)}this.wrapAroundShift=C,this.isWrapAround=0!==C}draw(t,e){if(!this.visible)return;if(!(this.isReady&&this._vertices&&this._indices&&this._texCoords))return void this.requestRender();this.stage||console.warn("OverlayMultipoint: stage is null"),e.render(t,{transform:{dvs:this.dvsMat3},config:{perspective:this.perspectiveTransform,texSize:this.textureSize,wrapAroundShift:this.wrapAroundShift,isWrapAround:this.isWrapAround,opacity:this.opacity,texture:{texture:this.texture,unit:0}},position:this._vertices,tex:this._texCoords,index:this._indices})}_initializeData(t,e,r){if(null!=this._vertices&&null!=this._indices)return;const{controlPoints:i}=e,o=Math.sqrt(i.length),s=o,n=new Float32Array(f*i.length),a=new Uint16Array(2*i.length);for(let p=0;p<i.length;p++){const e=i[p],{sourcePoint:o,mapPoint:s}=e;if(null==o||null==s)continue;const l=h(s,r);n[p*f+0]=l.x-t.x,n[p*f+1]=l.y-t.y,a[2*p+0]=o.x,a[2*p+1]=o.y}const l=new Uint16Array(s*o+(s-2)*(o+2));let u=0;for(let h=0;h<s;h++){for(let t=0;t<o;t++)l[u++]=h*o+t,l[u++]=(h+1)*o+t;h<s-2&&(l[u++]=(h+1)*o+(o-1),l[u++]=(h+1)*o)}this._vertices=n,this._texCoords=a,this._indices=l}_getFrameInfo(){if(!this.groundControlPoints)return null;const t=this._getFrameControlPoints(),e=this.frameHorizonPoints;let r=null;if(e){const t=e.startX,i=e.startY,o=e.endX,n=e.endY;r=[new u({sourcePoint:s(t,i),mapPoint:new a(e.startLongitude,e.startLatitude)}),new u({sourcePoint:s(o,n),mapPoint:new a(e.endLongitude,e.endLatitude)})]}return{controlPoints:t,horizon:r}}_getFrameControlPoints(){const t=this.groundControlPoints,e=t?.length;if(!e)return[];const r=new Array(e),i=Math.max(...t.map(({x:t})=>t)),o=this.element.videoWidth/i;for(let n=0;n<e;n++){const{x:e,y:i,lat:l,lon:h}=t[n];r[n]=new u({sourcePoint:s(e*o,-i*o),mapPoint:new a(h,l)})}return r}}export{x as default};
