/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{disposeMaybe as t}from"../../../../core/maybe.js";import{identity as e,multiply as r,translate as i,scale as s,rotate as o}from"../../../../core/libs/gl-matrix-2/math/mat3.js";import{create as a}from"../../../../core/libs/gl-matrix-2/factories/mat3f32.js";import{fromValues as n}from"../../../../core/libs/gl-matrix-2/factories/vec2f32.js";import{fromValues as l}from"../../../../core/libs/gl-matrix-2/factories/vec3f32.js";import{normalizeMapX as f}from"../../../../geometry/support/normalizeUtils.js";import{DisplayObject as m}from"../../engine/DisplayObject.js";import{createProgramDescriptor as u}from"../../engine/webgl/Utils.js";import{BufferObject as c}from"../../../webgl/BufferObject.js";import{PrimitiveType as h,DataType as _}from"../../../webgl/enums.js";import{VertexArrayObject as p}from"../../../webgl/VertexArrayObject.js";import{VertexBuffer as d}from"../../../webgl/VertexBuffer.js";const g=Math.PI/180,v=4;class x extends m{constructor(t){super(),this._program=null,this._vao=null,this._vertexBuffer=null,this._indexBuffer=null,this._dvsMat3=a(),this._localOrigin={x:0,y:0},this._getBounds=t}destroy(){super.destroy(),this._vao&&(this._vao.dispose(),this._vao=null,this._vertexBuffer=null,this._indexBuffer=null),this._program=t(this._program)}doRender(t){const{context:e}=t,r=this._getBounds();if(r.length<1)return;this._createShaderProgram(e),this._updateMatricesAndLocalOrigin(t),this._updateBufferData(e,r),e.setBlendingEnabled(!0),e.setDepthTestEnabled(!1),e.setStencilWriteMask(0),e.setStencilTestEnabled(!1),e.setBlendFunction(1,771),e.setColorMask(!0,!0,!0,!0);const i=this._program;e.bindVAO(this._vao),e.useProgram(i),i.setUniformMatrix3fv("u_dvsMat3",this._dvsMat3),e.gl.lineWidth(1),e.drawElements(h.LINES,8*r.length,_.UNSIGNED_INT,0),e.bindVAO(null)}_createTransforms(){return{displayViewScreenMat3:a()}}_createShaderProgram(t){if(this._program)return;const e="precision highp float;\n        uniform mat3 u_dvsMat3;\n\n        attribute vec2 a_position;\n\n        void main() {\n          mediump vec3 pos = u_dvsMat3 * vec3(a_position, 1.0);\n          gl_Position = vec4(pos.xy, 0.0, 1.0);\n        }",r="precision mediump float;\n      void main() {\n        gl_FragColor = vec4(0.75, 0.0, 0.0, 0.75);\n      }";this._program=t.programCache.acquire(e,r,b().attributes)}_updateMatricesAndLocalOrigin(t){const{state:a}=t,{displayMat3:m,size:u,resolution:c,pixelRatio:h,rotation:_,viewpoint:p}=a,d=g*_,{x:v,y:x}=p.targetGeometry,b=f(v,a.spatialReference);this._localOrigin.x=b,this._localOrigin.y=x;const B=h*u[0],y=h*u[1],j=c*B,M=c*y,O=e(this._dvsMat3);r(O,O,m),i(O,O,n(B/2,y/2)),s(O,O,l(u[0]/j,-y/M,1)),o(O,O,-d)}_updateBufferData(t,e){const{x:r,y:i}=this._localOrigin,s=2*v*e.length,o=new Float32Array(s),a=new Uint32Array(8*e.length);let n=0,l=0;for(const m of e)m&&(o[2*n]=m[0]-r,o[2*n+1]=m[1]-i,o[2*n+2]=m[0]-r,o[2*n+3]=m[3]-i,o[2*n+4]=m[2]-r,o[2*n+5]=m[3]-i,o[2*n+6]=m[2]-r,o[2*n+7]=m[1]-i,a[l]=n+0,a[l+1]=n+3,a[l+2]=n+3,a[l+3]=n+2,a[l+4]=n+2,a[l+5]=n+1,a[l+6]=n+1,a[l+7]=n+0,n+=4,l+=8);const f=b();this._vertexBuffer?this._vertexBuffer.setData(o.buffer):this._vertexBuffer=new d(t,f.bufferLayout,o.buffer,35048),this._indexBuffer?this._indexBuffer.setData(a):this._indexBuffer=c.createIndex(t,35048,a),this._vao||(this._vao=new p(t,this._vertexBuffer,this._indexBuffer))}}const b=()=>u("bounds",[{location:0,name:"a_position",count:2,type:_.FLOAT}]);export{x as default};
