/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../chunks/_rollupPluginBabelHelpers","../../../../chunks/mat3","../../../../chunks/mat3f32","../../../../chunks/vec2f32","../../../../chunks/vec3f32","../../../../geometry/support/normalizeUtils","../../engine/DisplayObject","../../engine/webgl/Utils","../../../webgl/BufferObject","../../../webgl/enums","../../../webgl/VertexArrayObject"],(function(e,t,i,r,n,a,s,o,u,c,l){"use strict";const f=Math.PI/180,_=4;let h=function(s){function o(e){var t;return(t=s.call(this)||this)._dvsMat3=i.create(),t._localOrigin={x:0,y:0},t._getBounds=e,t}e._inheritsLoose(o,s);var h=o.prototype;return h.destroy=function(){this._vao&&(this._vao.dispose(!0),this._vao=null,this._vertexBuffer=null,this._indexBuffer=null),this._program&&(this._program.dispose(),this._program=null)},h.doRender=function(e){const{context:t}=e,i=this._getBounds();if(i.length<1)return;this._createShaderProgram(t),this._updateMatricesAndLocalOrigin(e),this._updateBufferData(t,i),t.setBlendingEnabled(!0),t.setDepthTestEnabled(!1),t.setStencilWriteMask(0),t.setStencilTestEnabled(!1),t.setBlendFunction(c.BlendFactor.ONE,c.BlendFactor.ONE_MINUS_SRC_ALPHA),t.setColorMask(!0,!0,!0,!0);const r=this._program;t.bindVAO(this._vao),t.useProgram(r),r.setUniformMatrix3fv("u_dvsMat3",this._dvsMat3),t.gl.lineWidth(1),t.drawElements(c.PrimitiveType.LINES,8*i.length,c.DataType.UNSIGNED_INT,0),t.bindVAO()},h._createTransforms=function(){return{dvs:i.create()}},h._createShaderProgram=function(e){if(this._program)return;const t="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        }",i="precision mediump float;\n      void main() {\n        gl_FragColor = vec4(0.75, 0.0, 0.0, 0.75);\n      }";this._program=e.programCache.acquire(t,i,d().attributes)},h._updateMatricesAndLocalOrigin=function(e){const{state:i}=e,{displayMat3:s,size:o,resolution:u,pixelRatio:c,rotation:l,viewpoint:_}=i,h=f*l,{x:d,y:p}=_.targetGeometry,g=a.normalizeMapX(d,i.spatialReference);this._localOrigin.x=g,this._localOrigin.y=p;const m=c*o[0],v=c*o[1],y=u*m,b=u*v,x=t.identity(this._dvsMat3);t.multiply(x,x,s),t.translate(x,x,r.fromValues(m/2,v/2)),t.scale(x,x,n.fromValues(o[0]/y,-v/b,1)),t.rotate(x,x,-h)},h._updateBufferData=function(e,t){const{x:i,y:r}=this._localOrigin,n=2*_*t.length,a=new Float32Array(n),s=new Uint32Array(8*t.length);let o=0,f=0;for(const u of t)u&&(a[2*o+0]=u[0]-i,a[2*o+1]=u[1]-r,a[2*o+2]=u[0]-i,a[2*o+3]=u[3]-r,a[2*o+4]=u[2]-i,a[2*o+5]=u[3]-r,a[2*o+6]=u[2]-i,a[2*o+7]=u[1]-r,s[f+0]=o+0,s[f+1]=o+3,s[f+2]=o+3,s[f+3]=o+2,s[f+4]=o+2,s[f+5]=o+1,s[f+6]=o+1,s[f+7]=o+0,o+=4,f+=8);if(this._vertexBuffer?this._vertexBuffer.setData(a.buffer):this._vertexBuffer=u.BufferObject.createVertex(e,c.Usage.DYNAMIC_DRAW,a.buffer),this._indexBuffer?this._indexBuffer.setData(s):this._indexBuffer=u.BufferObject.createIndex(e,c.Usage.DYNAMIC_DRAW,s),!this._vao){const t=d();this._vao=new l.VertexArrayObject(e,t.attributes,t.bufferLayouts,{geometry:this._vertexBuffer},this._indexBuffer)}},o}(s.DisplayObject);const d=()=>o.createProgramDescriptor("bounds",{geometry:[{location:0,name:"a_position",count:2,type:c.DataType.FLOAT}]});return h}));
