/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{disposeMaybe as t}from"../../../../core/maybe.js";import{toRadian as i}from"../../../../core/libs/gl-matrix-2/math/common.js";import{fromTranslation as e,scale as r,rotate as s,translate 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/vec2f64.js";import{set as l}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as m}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{backbufferStencilVisible as c,backbufferStencilClipped as h}from"./definitions.js";import d from"./VertexStream.js";import{stencil as _}from"./shaders/StencilPrograms.js";import{createProgram as p}from"../../../webgl/ProgramTemplate.js";const f=n(-.5,-.5);class u{constructor(){this._centerNdc=m(),this._pxToNdc=m(),this._worldDimensionsPx=m(),this._mat3=a(),this._initialized=!1}dispose(){this._program=t(this._program),this._quad=t(this._quad)}render(t,i,e){const{context:r}=t,s=this._updateGeometry(t,e);if(null!=i){const{r:t,g:e,b:s,a:o}=i;r.setClearColor(o*t/255,o*e/255,o*s/255,o)}else r.setClearColor(0,0,0,0);if(r.setStencilFunction(519,0,255),r.setStencilWriteMask(255),!s)return r.setClearStencil(c),void r.clear(17408);r.setClearStencil(h),r.clear(17408),this._initialized||this._initialize(r),r.setDepthWriteEnabled(!1),r.setDepthTestEnabled(!1),r.setColorMask(!1,!1,!1,!1),r.setBlendingEnabled(!1),r.setStencilOp(7680,7680,7681),r.setStencilFunction(519,c,255),r.setStencilTestEnabled(!0),r.useProgram(this._program),this._program.setUniformMatrix3fv("u_worldExtent",this._mat3),this._quad.bind(),this._quad.draw(),this._quad.unbind()}_initialize(t){this._initialized||(this._quad=new d(t,[0,0,1,0,0,1,1,1]),this._program=p(t,_,this._quad.locations),this._initialized=!0)}_updateGeometry(t,a){const{state:n,pixelRatio:m}=t,{size:c,rotation:h}=n,d=Math.round(c[0]*m),_=Math.round(c[1]*m);if(!n.spatialReference.isWrappable)return!1;const p=i(h),u=Math.abs(Math.cos(p)),b=Math.abs(Math.sin(p)),g=Math.round(d*u+_*b),x=Math.round(m*n.worldScreenWidth);if(g<=x)return!1;const j=d*b+_*u,M=(a.left-a.right)*m/d,S=(a.bottom-a.top)*m/_;l(this._worldDimensionsPx,x,j,1),l(this._pxToNdc,2/d,-2/_,1),l(this._centerNdc,M,S,1);const w=this._mat3;return e(w,this._centerNdc),r(w,w,this._pxToNdc),0!==h&&s(w,w,p),r(w,w,this._worldDimensionsPx),o(w,w,f),!0}}export{u as WorldExtentRenderer};
