/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../../core/Handles.js";import{deg2rad as t}from"../../../core/mathUtils.js";import{destroyMaybe as r}from"../../../core/maybe.js";import{watch as i,initial as s}from"../../../core/reactiveUtils.js";import{convertUnit as n}from"../../../core/units.js";import{fromRotation as o,translate as a,rotate as l,scaleByVec2 as m}from"../../../core/libs/gl-matrix-2/math/mat3.js";import{create as c}from"../../../core/libs/gl-matrix-2/factories/mat3f32.js";import{set as h,sub as p,scale as d,rotate as u}from"../../../core/libs/gl-matrix-2/math/vec2.js";import{ZEROS as f}from"../../../core/libs/gl-matrix-2/factories/vec2f32.js";import{create as j}from"../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{isLoadedOrLoadFor as _,project as g}from"../../../geometry/projectionUtils.js";import{getClosestDenormalizedXToReference as C}from"../../../geometry/support/normalizeUtils.js";import{getResolutionForScale as R}from"../../../geometry/support/scaleUtils.js";import{equals as v}from"../../../geometry/support/spatialReferenceUtils.js";import{DisplayObject as y}from"../engine/DisplayObject.js";import{GridTechnique as U}from"../engine/webgl/shaderGraph/techniques/grid/GridTechnique.js";import{minimumPixelsPerStrideForDisplayWhenScalingOff as S}from"./constants.js";import{measureMetersPerUnitAtReferencePoint as b,getScaleFactor as L}from"./gridUtils.js";const x=j();class P extends y{constructor(){super(),this._handles=new e,this._projectedCenter=null,this._metersPerSRUnit=null,this._technique=new U,this._grid=null,this.visible=!0}destroy(){super.destroy(),this._handles=r(this._handles),this._technique.shutdown()}get grid(){return this._grid}set grid(e){this._grid=e,this._handles.removeAll(),this._handles.add([i(()=>e?.center,()=>{this._projectedCenter=null},s),i(()=>[e?.center,e?.dynamicScaling,e?.majorLineColor,e?.majorLineInterval,e?.minorLineColor,e?.rotateWithMap,e?.rotation,e?.spacing,e?.units],()=>this.requestRender(),s)])}_createTransforms(){return{displayViewScreenMat3:c()}}doRender(e){if(1!==e.drawPhase||null==this.grid||!this.visible)return;const{spacing:t,units:r,majorLineInterval:i,dynamicScaling:s,majorLineColor:o,minorLineColor:a}=this.grid;if(0===t)return;if(this._updateDerivedValues(e),null==this._projectedCenter||null==this._metersPerSRUnit)return;const{scale:l,spatialReference:m}=e.state,c=n(t,r,"meters"),h=this._metersPerSRUnit*R(l,m),p=c/h;if(!s&&p<S)return;const d=c*L(i,p,s);this._updateTransform(e,h,d),this._technique.render(e,{transform:{dvs:this.transforms.displayViewScreenMat3},config:{pxPerCell:d/h,minorLineColor:M(a),majorLineColor:M(o),majorLineInterval:i,halfWidth:.25,aaWidth:.5}})}_updateDerivedValues(e){if(!this.grid)return;const{center:t}=this.grid,{spatialReference:r}=e.state;this._projectedCenter&&v(this._projectedCenter.spatialReference,r)||(this._metersPerSRUnit=null,v(t.spatialReference,r)?this._projectedCenter=t:_(t.spatialReference,r)?this._projectedCenter=g(t,r):this.requestRender()),null==this._metersPerSRUnit&&null!=this._projectedCenter&&(this._metersPerSRUnit=b(this._projectedCenter))}_updateTransform(e,r,i){const{grid:s}=this,{center:n,rotation:c,size:j,spatialReference:_}=e.state;if(null==s||null==this._projectedCenter||null==this._metersPerSRUnit)return;const g=r*(j[0]/2),R=r*(j[1]/2),v=this._metersPerSRUnit/i,y=this._projectedCenter,U=t(s.rotation),S=t(c),b=this.transforms.displayViewScreenMat3;o(b,-U);const L=_.isWrappable?C(y.x,n[0],_):y.x,P=h(x,L,y.y),M=p(x,n,P);d(M,M,v),s.rotateWithMap||u(M,M,f,-S),u(M,M,f,-U),d(M,M,1/s.majorLineInterval),w(M,M),d(M,M,s.majorLineInterval),u(M,M,f,U),a(b,b,M),s.rotateWithMap&&l(b,b,S);const q=h(x,g/i,R/i);m(b,b,q)}}function w(e,t){return e[0]=t[0]-Math.trunc(t[0]),e[1]=t[1]-Math.trunc(t[1]),e}function M(e){const[t,r,i,s]=e.toArray().map(e=>e/255);return[t*s,r*s,i*s,s]}export{P as default};
