/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{DragEventSeparator as t}from"../DragEventSeparator.js";import{InputHandler as e}from"../InputHandler.js";class a extends e{constructor(e=20,a=40){super(!1),this._threshold=e,this._maxDelta=a,this._state="ready",this._emittedArtificalEnd2=!1,this._vertical=this.registerOutgoing("vertical-two-finger-drag"),this._artificalDrag=this.registerOutgoing("drag"),this._dragEventSeparator=new t({start:(t,e)=>this._observeStart(t,e),update:(t,e,a)=>this._observeUpdate(t,e,a),end:(t,e)=>this._observeEnd(e)}),this.registerIncoming("drag",t=>this._dragEventSeparator.handle(t))}get failed(){return"failed"===this._state}_observeStart(t,e){1===t&&this._emittedArtificalEnd2&&(this._emittedArtificalEnd2=!1,this._artificalDrag.emit({action:"start",button:e.data.button,buttons:e.data.buttons,pointerType:e.data.pointerType,timestamp:e.data.timestamp,pointers:e.data.pointers,pointer:e.data.pointer,angle:e.data.angle,radius:e.data.radius,center:e.data.center}),e.stopPropagation()),this._state=2===t?"ready":"failed"}_observeUpdate(t,e,a){if("failed"!==this._state&&2===t)return"active"===this._state?(this._vertical.emit({delta:e.data.center.y-this._thresholdReachedCenter.y,action:"update",pointer:e.data.pointer}),void e.stopPropagation()):void(this._checkMovementWithinLimits(e.data,a.data)?this._checkVerticalThresholdReached(e.data,a.data)&&(this._state="active",this._emittedArtificalEnd2=!0,this._thresholdReachedCenter=e.data.center,this._artificalDrag.emit({action:"end",button:e.data.button,buttons:e.data.buttons,pointerType:e.data.pointerType,timestamp:e.data.timestamp,pointers:e.data.pointers,pointer:e.data.pointer,angle:e.data.angle,radius:e.data.radius,center:e.data.center}),this._vertical.emit({delta:e.data.center.y-this._thresholdReachedCenter.y,action:"begin",pointer:e.data.pointer}),e.stopPropagation()):this._state="failed")}_observeEnd(t){"active"===this._state&&(this._vertical.emit({delta:t.data.center.y-this._thresholdReachedCenter.y,action:"end",pointer:t.data.pointer}),this._state="ready",t.stopPropagation())}_checkMovementWithinLimits(t,e){let a=-1/0,i=1/0,r=-1/0,s=1/0;for(const{x:m,y:g}of e.pointers.values())a=Math.max(a,m),i=Math.min(i,m),r=Math.max(r,g),s=Math.min(s,g);let n=-1/0,o=1/0,d=-1/0,h=1/0;for(const{x:m,y:g}of t.pointers.values())n=Math.max(n,m),o=Math.min(o,m),d=Math.max(d,g),h=Math.min(h,g);const c=a-i,p=r-s,l=n-o,_=d-h;return Math.abs(t.center.x-e.center.x)<this._threshold&&Math.abs(l-c)<=this._maxDelta&&Math.abs(_-p)<=this._maxDelta}_checkVerticalThresholdReached(t,e){let a=Math.abs(t.center.y-e.center.y);return t.pointers.forEach((t,i)=>{const r=e.pointers.get(i);a=Math.min(a,Math.abs(t.y-r.y))}),a>=this._threshold}}export{a as VerticalTwoFingerDrag};
