/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{clock as t}from"../../../core/clock.js";import{removeMaybe as e}from"../../../core/maybe.js";import{InputHandler as i}from"../InputHandler.js";import{getDoubleClickParameters as o,manhattanDistance as n,getPointerId as r}from"./support.js";class s extends i{constructor(e={},i=t){super(!1),this._clock=i,this._pointerState=new Map,this._parameters=o(e),this._click=this.registerOutgoing("click"),this._doubleClick=this.registerOutgoing("double-click"),this.registerIncoming("immediate-click",t=>this._handleImmediateClick(t)),this.registerIncoming("pointer-down",t=>this._handlePointerDown(t))}onUninstall(){this._pointerState.forEach(t=>t.doubleClickTimer=e(t.doubleClickTimer))}get hasPendingInputs(){for(const t of this._pointerState.values())if(null!=t.doubleClickTimer)return!0;return!1}_clearDoubleClickTimer(t,i){const o=this._pointerState.get(t);o&&(o.doubleClickTimer=e(o.doubleClickTimer),i&&this._click.emit(o.event.data,void 0,o.event.modifiers),this._pointerState.delete(t),this.refreshHasPendingInputs())}_doubleClickTimeoutExceeded(t){const e=this._pointerState.get(t);1===e.pointerDownCount&&this._click.emit(e.event.data,void 0,e.event.modifiers),e.doubleClickTimer=null,this._pointerState.delete(t),this.refreshHasPendingInputs()}_handleImmediateClick(t){const e=t.data,{pointerType:i}=e.native,o=a(e);if(!this._pointerState.has(o))return void this._startClick(t);const r=this._pointerState.get(o),{data:s,modifiers:l}=r.event,c="touch"===i?this._parameters.maximumDoubleTouchDistance:this._parameters.maximumDoubleClickDistance;n(s,e)>c?(this._clearDoubleClickTimer(o,!0),this._startClick(t)):(this._clearDoubleClickTimer(o,!1),2===r.pointerDownCount&&this._doubleClick.emit(s,void 0,l))}_handlePointerDown(t){const e=r(t.data),i=this._pointerState.get(e);i&&(i.pointerDownCount+=1)}_startClick(t){const{data:e}=t,{native:{pointerType:i}}=e,o=r(e),n="touch"===i?this._parameters.maximumDoubleTouchDelay:this._parameters.maximumDoubleClickDelay,s=this._clock.setTimeout(()=>this._doubleClickTimeoutExceeded(o),n),a=1;this._pointerState.set(o,{event:t,doubleClickTimer:s,pointerDownCount:a}),this.refreshHasPendingInputs()}}function a(t){const{pointerId:e,pointerType:i,button:o}=t.native;return"mouse"===i?`${e}:${o}`:`${i}`}export{s as SingleAndDoubleClick};
