/* 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{destroyMaybe as n}from"../../../core/maybe.js";import{property as e,subclass as s}from"../../../core/accessorSupport/decorators.js";import a from"../../../geometry/Point.js";import{rotateBy as r,translateBy as c,padAndScaleAndRotateBy as h}from"../viewpointUtils.js";import{getGoToDuration as m}from"./duration.js";import p from"./ZoomBox.js";import u from"./actions/Pan.js";import l from"./actions/Pinch.js";import w from"./actions/Rotate.js";const v=10,g=1,d=new i({targetGeometry:new a}),_=[0,0],T=250;let f=class extends o{constructor(t){super(t),this._endTimer=null,this._lastEventTimestamp=null,this.animationManager=null,this.interacting=!1}initialize(){this.pan=new u({navigation:this}),this.rotate=new w({navigation:this}),this.pinch=new l({navigation:this}),this.zoomBox=new p({view:this.view,navigation:this})}destroy(){this.pan=n(this.pan),this.rotate=n(this.rotate),this.pinch=n(this.pinch),this.zoomBox=n(this.zoomBox),this.animationManager=null}begin(){this.stop(),this._set("interacting",!0)}end(){this._lastEventTimestamp=performance.now(),this._startTimer(T)}async zoom(t,i=this._getDefaultAnchor()){if(this.begin(),this.view.constraints.snapToZoom&&this.view.constraints.effectiveLODs)return t<1?this.zoomIn(i):this.zoomOut(i);this.setViewpoint(i,t,0,[0,0])}async zoomIn(t){const i=this.view,o=i.constraints.snapToNextScale(i.scale);return this._zoomToScale(o,t)}async zoomOut(t){const i=this.view,o=i.constraints.snapToPreviousScale(i.scale);return this._zoomToScale(o,t)}setViewpoint(t,i,o,n){this.begin(),this.view.stateManager.state.viewpoint=this._scaleRotateTranslateViewpoint(this.view.viewpoint,t,i,o,n),this.end()}setViewpointImmediate(t,i=0,o=[0,0],n=this._getDefaultAnchor()){this.view.stateManager.state.viewpoint=this._scaleRotateTranslateViewpoint(this.view.viewpoint,n,t,i,o)}continuousRotateClockwise(){const t=this.view.viewpoint;this.animationManager?.animateContinuous(t,t=>{r(t,t,-g)})}continuousRotateCounterclockwise(){const t=this.view.viewpoint;this.animationManager?.animateContinuous(t,t=>{r(t,t,g)})}resetRotation(){this.view.constraints.rotationEnabled&&(this.view.rotation=0)}continuousPanLeft(){this._continuousPan([-v,0])}continuousPanRight(){this._continuousPan([v,0])}continuousPanUp(){this._continuousPan([0,v])}continuousPanDown(){this._continuousPan([0,-v])}continuousPanVector({x:t,y:i}){this._continuousPan([t*v,i*v])}stop(){this.pan.stopMomentumNavigation(),this.animationManager?.stop(),this.end(),null!==this._endTimer&&(clearTimeout(this._endTimer),this._endTimer=null,this._set("interacting",!1))}_continuousPan(t){const i=this.view.viewpoint;this.animationManager?.animateContinuous(i,i=>{c(i,i,t),this.view.constraints.constrainByGeometry(i)})}_startTimer(t){return null!==this._endTimer||(this._endTimer=setTimeout(()=>{this._endTimer=null;const t=performance.now()-(this._lastEventTimestamp??0);t<T?this._endTimer=this._startTimer(t):this._set("interacting",!1)},t)),this._endTimer}_getDefaultAnchor(){const{size:t,padding:{left:i,right:o,top:n,bottom:e}}=this.view;return _[0]=.5*(t[0]-o+i),_[1]=.5*(t[1]-e+n),_}async _zoomToScale(t,i=this._getDefaultAnchor()){const{view:o}=this,{constraints:n,scale:e,viewpoint:s,size:a,padding:r}=o,c=n.canZoomInTo(t),p=n.canZoomOutTo(t);if(!(t<e&&!c||t>e&&!p))return h(d,s,t/e,0,i,a,r),n.constrainByGeometry(d),o.goTo(d,{animate:!0,animationMode:"always",duration:m(),pickClosestTarget:!1})}_scaleRotateTranslateViewpoint(t,i,o,n,e){const{view:s}=this,{size:a,padding:r,constraints:m,scale:p,viewpoint:u}=s,l=p*o,w=m.canZoomInTo(l),v=m.canZoomOutTo(l);return(o<1&&!w||o>1&&!v)&&(o=1),c(u,u,e),h(t,u,o,n,i,a,r),m.constrainByGeometry(t)}};t([e()],f.prototype,"animationManager",void 0),t([e({type:Boolean,readOnly:!0})],f.prototype,"interacting",void 0),t([e()],f.prototype,"pan",void 0),t([e()],f.prototype,"pinch",void 0),t([e()],f.prototype,"rotate",void 0),t([e()],f.prototype,"view",void 0),t([e()],f.prototype,"zoomBox",void 0),f=t([s("esri.views.2d.navigation.MapViewNavigation")],f);const y=f;export{y as default};
