/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{__decorate as t}from"tslib";import i from"../../../../Viewpoint.js";import o from"../../../../core/Accessor.js";import{when as s}from"../../../../core/reactiveUtils.js";import{Seconds as e,secondsFromMilliseconds as n}from"../../../../core/time.js";import{property as m,subclass as a}from"../../../../core/accessorSupport/decorators.js";import{set as r,add as h}from"../../../../core/libs/gl-matrix-2/math/vec2.js";import{create as u}from"../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import l from"../../../../geometry/Point.js";import{getPaddingScreenTranslation as p,scaleAndRotateBy as _}from"../../viewpointUtils.js";import{RotationMomentumEstimator as c}from"../../../navigation/RotationMomentumEstimator.js";import{ZoomMomentumEstimator as d}from"../../../navigation/ZoomMomentumEstimator.js";let M=class extends o{constructor(t){super(t),this._animationTime=e(0),this._momentumFinished=!1,this._previousAngle=0,this._previousRadius=0,this._previousCenter=null,this._rotationMomentumEstimator=new c(.6,.15,.95),this._rotationDirection=1,this._startAngle=0,this._startRadius=0,this._updateTimestamp=null,this._zoomDirection=1,this._zoomMomentumEstimator=new d,this._zoomOnly=null,this.viewpoint=new i({targetGeometry:new l,scale:0,rotation:0}),this.zoomMomentum=null,this.rotateMomentum=null,this.addHandles(s(()=>this._momentumFinished,()=>this.navigation.stop()))}begin(t,i){this.navigation.begin(),this._rotationMomentumEstimator.reset(),this._zoomMomentumEstimator.reset(),this._zoomOnly=null,this._previousAngle=this._startAngle=i.angle,this._previousRadius=this._startRadius=i.radius,this._previousCenter=i.center,this._updateTimestamp=null,t.constraints.rotationEnabled&&this.addToRotateEstimator(0,i.timestamp),this.addToZoomEstimator(i,1)}update(t,i){null===this._updateTimestamp&&(this._updateTimestamp=i.timestamp);const o=i.angle,s=i.radius,e=i.center,n=Math.abs(180*(o-this._startAngle)/Math.PI),m=Math.abs(s-this._startRadius),a=this._startRadius/s;if(this._previousRadius&&this._previousCenter){const r=s/this._previousRadius;let h=180*(o-this._previousAngle)/Math.PI;this._rotationDirection=h>=0?1:-1,this._zoomDirection=r>=1?1:-1,t.constraints.rotationEnabled?(null===this._zoomOnly&&i.timestamp-this._updateTimestamp>200&&(this._zoomOnly=m-n>0),null===this._zoomOnly||this._zoomOnly?h=0:this.addToRotateEstimator(o-this._startAngle,i.timestamp)):h=0,this.addToZoomEstimator(i,a),this.navigation.setViewpoint([e.x,e.y],1/r,h,[this._previousCenter.x-e.x,e.y-this._previousCenter.y])}this._previousAngle=o,this._previousRadius=s,this._previousCenter=e}end(t){this.rotateMomentum=this._rotationMomentumEstimator.evaluateMomentum(),this.zoomMomentum=this._zoomMomentumEstimator.evaluateMomentum(),this._animationTime=e(0),(this.rotateMomentum||this.zoomMomentum)&&this.onAnimationUpdate(t),this.navigation.end()}addToRotateEstimator(t,i){this._rotationMomentumEstimator.add(t,.001*i)}addToZoomEstimator(t,i){this._zoomMomentumEstimator.add(i,.001*t.timestamp)}canZoomIn(t){const i=t.scale,o=t.constraints.effectiveMaxScale;return 0===o||i>o}canZoomOut(t){const i=t.scale,o=t.constraints.effectiveMinScale;return 0===o||i<o}onAnimationUpdate(t){this.navigation.animationManager?.animateContinuous(t.viewpoint,(i,o)=>{const s=!this.canZoomIn(t)&&this._zoomDirection>1||!this.canZoomOut(t)&&this._zoomDirection<1,m=!this.rotateMomentum||this.rotateMomentum.isFinished(this._animationTime),a=s||!this.zoomMomentum||this.zoomMomentum.isFinished(this._animationTime),l=n(o);if(this._momentumFinished=m&&a,!this._momentumFinished){const o=this.rotateMomentum?Math.abs(this.rotateMomentum.valueDelta(this._animationTime,l))*this._rotationDirection*180/Math.PI:0;let s=this.zoomMomentum?Math.abs(this.zoomMomentum.valueDelta(this._animationTime,l)):1;const e=u(),n=u();if(this._previousCenter){r(e,this._previousCenter.x,this._previousCenter.y),p(n,t.size,t.padding),h(e,e,n);const{constraints:m,scale:a}=t,u=a*s;s<1&&!m.canZoomInTo(u)?(s=a/m.effectiveMaxScale,this.zoomMomentum=null,this.rotateMomentum=null):s>1&&!m.canZoomOutTo(u)&&(s=a/m.effectiveMinScale,this.zoomMomentum=null,this.rotateMomentum=null),_(i,t.viewpoint,s,o,e,t.size),t.constraints.constrainByGeometry(i)}}this._animationTime=e(this._animationTime+l)})}stopMomentumNavigation(){(this.rotateMomentum||this.zoomMomentum)&&(this.rotateMomentum&&(this._rotationMomentumEstimator.reset(),this.rotateMomentum=null),this.zoomMomentum&&(this._zoomMomentumEstimator.reset(),this.zoomMomentum=null),this.navigation.stop())}};t([m()],M.prototype,"_momentumFinished",void 0),t([m()],M.prototype,"viewpoint",void 0),t([m()],M.prototype,"navigation",void 0),M=t([a("esri.views.2d.navigation.actions.Pinch")],M);export{M as default};
