/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as t}from"tslib";import{acosClamped as i,clamp as s}from"../../../../core/mathUtils.js";import{createScreenPointArray as r}from"../../../../core/screenUtils.js";import{property as e,subclass as o}from"../../../../core/accessorSupport/decorators.js";import{fromRotation as a,rotate as n}from"../../../../core/libs/gl-matrix-2/math/mat4.js";import{create as h}from"../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{copy as p}from"../../../../core/libs/gl-matrix-2/math/vec2.js";import{create as m}from"../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{copy as c,subtract as l,length as _,normalize as v,add as P,scale as f,dot as w,cross as u,transformMat4 as C}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as d}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{applyAll as g}from"../../camera/constraintUtils.js";import{ConstraintOptions as x}from"../../camera/constraintUtils/ConstraintOptions.js";import{getVoxelWasm as y}from"../../layers/VoxelWasm.js";import{TiltRange as b}from"../Constraints.js";import{InteractiveController as j}from"./InteractiveController.js";import{excludeTerrain as R,normalizeCoordinate as M,getTiltScaleFactor as U,maxRotatePivotDistanceModifier as V,minRotatePivotDistance as D,inferNavigationMode as O,rotatePivotSearchAreaSize as T}from"../utils/navigationUtils.js";let A=class extends j{get _intersectionHelper(){return this.view.sceneIntersectionHelper}constructor(t){super(t),this.pivot=0,this._rotScale=0,this._lastPoint=m(),this._tmpWorldUp=d(),this._tmpViewDir=d(),this._tmpRotCurPoint=m(),this._tmpTransf=h(),this._tmpAxis=d(),this._tmpPivotPoint=d(),this._pivotPos=d(),this._constraintOptions=new x(15,2,0,this.startCamera)}initialize(){this._rotScale=0===this.pivot?3:1.5}begin(t){if(this.running){switch(this.pivot){case 1:c(this._pivotPos,this.startCamera.eye),this._constraintOptions.interactionType=3,this._constraintOptions.tiltMode=1,this._constraintOptions.selection=0;break;case 0:{const i=this._intersectionHelper.intersectRayFreePointFallback(this.startCamera.ray,this._pivotPos,this.view.basemapTerrain.invisible?R:{});i||c(this._pivotPos,this.startCamera.center),this._constrainPivotPoint(t,i),this.startCamera.center=this._pivotPos,this._constraintOptions.interactionType=2,this._constraintOptions.tiltMode=0,this._constraintOptions.selection=11;break}}M(this.startCamera,t,this._lastPoint)}}_constrainPivotPoint(t,i){const s=this.startCamera,e=d();l(e,this._pivotPos,s.eye);const o=_(e),a=Math.abs(this.view.camera.position.z);this.view.renderCoordsHelper.worldUpAtPosition(s.eye,W);let n=Math.max(U(W,s.viewForward,V)*a,D);i&&(n=Math.min(o,n));const h=r(s.width/s.pixelRatio*.5,s.height/s.pixelRatio*.5),p=O(this.startCamera,h,this.view.renderCoordsHelper,this.view.viewingMode);let m=this.view.stage.renderView.getMinimalDepthForArea(y(this.view),s.fullWidth/s.pixelRatio*.5,s.fullHeight/s.pixelRatio*.5,s,2.5*T,T),w=this.view.stage.renderView.getMinimalDepthForArea(y(this.view),t[0],t[1],s,T);null==m&&null==w||(m=m??w??0,w=null==w||1===p?m:w,n=m>w?w:m,n=i?Math.min(n,o):n),v(e,e),c(this._pivotPos,P(this._tmpPivotPoint,s.eye,f(this._tmpPivotPoint,e,n)))}update(t){if(this.running){switch(this.pivot){case 1:this.currentCamera.center=this._applyRotation(this.currentCamera,t,this.currentCamera.center,this._pivotPos);break;case 0:this.currentCamera.center=this._pivotPos,this.currentCamera.eye=this._applyRotation(this.currentCamera,t,this.currentCamera.eye,this._pivotPos)}g(this.view,this.currentCamera,this._constraintOptions),this.commitCamera()}}end(){this.running&&this.finishController()}_applyRotation(t,r,e,o){this.view.renderCoordsHelper.worldUpAtPosition(o,this._tmpWorldUp),M(t,r,this._tmpRotCurPoint);let h=(this._lastPoint[1]-this._tmpRotCurPoint[1])*this._rotScale,m=(this._tmpRotCurPoint[0]-this._lastPoint[0])*this._rotScale;l(this._tmpViewDir,e,o);const c=_(this._tmpViewDir),v=i(w(this._tmpViewDir,this._tmpWorldUp)/c);if(1===this.pivot){h*=-.5;const t=.5*Math.PI-v,i=.5*Math.PI*.99;h=t-Math.max(-i,Math.min(i,t+h))}return h=s(h+v,b.min,b.max)-v,u(this._tmpAxis,t.up,this._tmpViewDir),0===this.pivot&&(m=-m),a(this._tmpTransf,m,this._tmpWorldUp),n(this._tmpTransf,this._tmpTransf,h,this._tmpAxis),C(this._tmpViewDir,this._tmpViewDir,this._tmpTransf),t.up=C(H,t.up,this._tmpTransf),P(H,o,this._tmpViewDir),p(this._lastPoint,this._tmpRotCurPoint),H}};t([e()],A.prototype,"pivot",void 0),A=t([o("esri.views.3d.state.controllers.RotateController")],A);const H=d(),W=d();export{A as RotateController};
