/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{__decorate as t}from"tslib";import e from"../../../core/Accessor.js";import{createScreenPoint as i}from"../../../core/screenUtils.js";import{property as r,subclass as o}from"../../../core/accessorSupport/decorators.js";import{getGoToDuration as s}from"./duration.js";import{ViewEventPriorities as a}from"../../input/InputManager.js";const n="esri-zoom-box",h={container:`${n}__container`,overlay:`${n}__overlay`,background:`${n}__overlay-background`,box:`${n}__outline`},l={zoom:"Shift",counter:"Control"};let d=class extends e{constructor(t){super(t),this._container=null,this._overlay=null,this._backgroundShape=null,this._boxShape=null,this._box={x:0,y:0,width:0,height:0},this._rafId=null,this._redraw=this._redraw.bind(this)}destroy(){this.view=null}set view(t){this.removeAllHandles(),this._destroyOverlay(),this._set("view",t),t&&this.addHandles([t.on("drag",[l.zoom],t=>this._handleDrag(t,1),a.INTERNAL),t.on("drag",[l.zoom,l.counter],t=>this._handleDrag(t,-1),a.INTERNAL)])}_start(){this._createContainer(),this._createOverlay(),this.navigation.begin()}_update(t,e,i,r){this._box.x=t,this._box.y=e,this._box.width=i,this._box.height=r,this._rafId||(this._rafId=requestAnimationFrame(this._redraw))}_end(t,e,r,o,a){const n=this.view,h=n.toMap(i(t+.5*r,e+.5*o));let l=Math.max(r/n.width,o/n.height);-1===a&&(l=1/l),this._destroyOverlay(),this.navigation.end(),n.goTo({center:h,scale:n.scale*l},{animationMode:"always",duration:s()})}_updateBox(t,e,i,r){const o=this._boxShape;o.setAttributeNS(null,"x",""+t),o.setAttributeNS(null,"y",""+e),o.setAttributeNS(null,"width",""+i),o.setAttributeNS(null,"height",""+r),o.setAttributeNS(null,"class",h.box)}_updateBackground(t,e,i,r){this._backgroundShape.setAttributeNS(null,"d",this._toSVGPath(t,e,i,r,this.view.width,this.view.height))}_createContainer(){const t=document.createElement("div");t.className=h.container,this.view.root.appendChild(t),this._container=t}_createOverlay(){const t=this.view.width,e=this.view.height,i=document.createElementNS("http://www.w3.org/2000/svg","path");i.setAttributeNS(null,"d","M 0 0 L "+t+" 0 L "+t+" "+e+" L 0 "+e+" Z"),i.setAttributeNS(null,"class",h.background);const r=document.createElementNS("http://www.w3.org/2000/svg","rect"),o=document.createElementNS("http://www.w3.org/2000/svg","svg");o.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:xlink","http://www.w3.org/1999/xlink"),o.setAttributeNS(null,"class",h.overlay),o.appendChild(i),o.appendChild(r),this._container.appendChild(o),this._backgroundShape=i,this._boxShape=r,this._overlay=o}_destroyOverlay(){this._container&&this._container.parentNode&&this._container.parentNode.removeChild(this._container),this._container=this._backgroundShape=this._boxShape=this._overlay=null}_toSVGPath(t,e,i,r,o,s){const a=t+i,n=e+r;return"M 0 0 L "+o+" 0 L "+o+" "+s+" L 0 "+s+" ZM "+t+" "+e+" L "+t+" "+n+" L "+a+" "+n+" L "+a+" "+e+" Z"}_handleDrag(t,e){const i=t.x,r=t.y,o=t.origin.x,s=t.origin.y;let a,n,h,l;switch(i>o?(a=o,h=i-o):(a=i,h=o-i),r>s?(n=s,l=r-s):(n=r,l=s-r),t.action){case"start":this._start();break;case"update":this._update(a,n,h,l);break;case"end":this._end(a,n,h,l,e)}t.stopPropagation()}_redraw(){if(!this._rafId)return;if(this._rafId=null,!this._overlay)return;const{x:t,y:e,width:i,height:r}=this._box;this._updateBox(t,e,i,r),this._updateBackground(t,e,i,r),this._rafId=requestAnimationFrame(this._redraw)}};t([r()],d.prototype,"navigation",void 0),t([r()],d.prototype,"view",null),d=t([o("esri.views.2d.navigation.ZoomBox")],d);export{d as default};
