/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.25/esri/copyright.txt for details. */ import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../core/Accessor.js";import r from"../../core/Logger.js";import{isNone as i,unwrapOrThrow as n}from"../../core/maybe.js";import s from"../../core/Queue.js";import{property as o}from"../../core/accessorSupport/decorators/property.js";import"../../core/arrayUtils.js";import"../../core/accessorSupport/ensureType.js";import{subclass as a}from"../../core/accessorSupport/decorators/subclass.js";import{PropertiesPool as l}from"../3d/support/PropertiesPool.js";import{primaryKey as h,isSystemModifier as p}from"./keys.js";import{LatestPointer as d}from"./handlers/LatestPointer.js";import{MultiTouch as c}from"./handlers/MultiTouch.js";let u=class extends t{constructor(e){super(e),this._pointerCaptures=new Map,this._nameToGroup={},this._handlers=[],this._handlersPriority=[],this._currentPropagation=null,this._updateDependenciesAfterPropagation=!1,this._sourceEvents=new Set,this._keyModifiers=new Set,this._activeKeyModifiers=new Set,this._stoppedPropagationEventIds=new Set,this.primaryKey=h,this._latestPointerType="mouse",this._propertiesPool=new l({latestPointerLocation:v},this),this.latestPointerLocation=null,this.test={timestamp:void 0,hasCurrentPropagation:()=>!!this._currentPropagation}}initialize(){this.eventSource.onEventReceived=this._onEventReceived.bind(this),this._installRecognizers()}destroy(){const e=Object.keys(this._nameToGroup);for(const t of e)this.uninstallHandlers(t);this.eventSource.destroy(),this._currentPropagation=null,this._propertiesPool.destroy()}get hasPendingInputs(){return this._handlers.some((e=>e.handler.hasPendingInputs))}get latestPointerType(){return this._latestPointerType}get multiTouchActive(){return this._multiTouchHandler.multiTouchActive}installHandlers(e,t,i=P.INTERNAL){if(this._nameToGroup[e])return void r.getLogger(this.declaredClass).error("There is already an InputHandler group registered under the name `"+e+"`");if(0===t.length)return void r.getLogger(this.declaredClass).error("Can't register a group of zero handlers");const n={name:e,handlers:t.map((e=>({handler:e,active:!0,removed:!1,priorityIndex:0,groupPriority:i,eventCallback:null,uninstallCallback:null})))};this._nameToGroup[e]=n;for(let r=n.handlers.length-1;r>=0;r--){const e=n.handlers[r];this._handlers.push(e),e.handler.onInstall({updateDependencies:()=>{this.updateDependencies()},emit:(t,r,i,n,s)=>{this._emitInputEvent(e.priorityIndex+1,t,r,i,s,n)},setPointerCapture:(t,r)=>{this._setPointerCapture(n,e,t,r)},setEventCallback:t=>{e.eventCallback=t},setUninstallCallback:t=>{e.uninstallCallback=t},refreshHasPendingInputs:()=>{this.notifyChange("hasPendingInputs")}})}this.updateDependencies()}uninstallHandlers(e){const t=this._nameToGroup[e];t?(t.handlers.forEach((e=>{e.removed=!0,e.uninstallCallback?.()})),delete this._nameToGroup[e],this._currentPropagation?this._currentPropagation.needsHandlerGarbageCollect=!0:this._garbageCollectRemovedHandlers()):r.getLogger(this.declaredClass).error("There is no InputHandler group registered under the name `"+e+"`")}hasHandlers(e){return void 0!==this._nameToGroup[e]}updateDependencies(){if(this._currentPropagation)return void(this._updateDependenciesAfterPropagation=!0);this._updateDependenciesAfterPropagation=!1;const e=new Set,t=new Set;this._handlersPriority=[];for(let r=this._handlers.length-1;r>=0;r--){const e=this._handlers[r];e.priorityIndex=r,this._handlersPriority.push(e)}this._handlersPriority=this._sortHandlersPriority(this._handlersPriority);for(let r=this._handlersPriority.length-1;r>=0;r--){const i=this._handlersPriority[r];i.priorityIndex=r;let n=i.handler.hasSideEffects;if(!n)for(const t of i.handler.outgoingEventTypes)if(e.has(t)){n=!0;break}if(n)for(const r of i.handler.incomingEventMatches){e.add(r.eventType);for(const e of r.keyModifiers)p(e)||t.add(e)}i.active=n}this._sourceEvents=e,this._keyModifiers=t,this._pointerCaptures.size>0&&this._sourceEvents.add("pointer-capture-lost"),this._keyModifiers.size>0&&(this._sourceEvents.add("key-down"),this._sourceEvents.add("key-up")),this.eventSource&&(this.eventSource.activeEvents=this._sourceEvents)}_setLatestPointer(e,t,r){this._latestPointerType=e;const n=this._get("latestPointerLocation");if(i(n)||n.x!==t||n.y!==r){const e=this._propertiesPool.get("latestPointerLocation");e.x=t,e.y=r,this._set("latestPointerLocation",e)}}_onEventReceived(e,t){if("pointer-capture-lost"===e){const e=t;this._pointerCaptures.delete(e.native.pointerId)}this._updateKeyModifiers(e,t);const r=null!=this.test.timestamp?this.test.timestamp:t.native?t.native.timestamp:void 0,i=t.native?t.native.cancelable:void 0;this._emitInputEventFromSource(e,t,r,i)}_updateKeyModifiers(e,t){if(!t)return;let r=!1;const i=()=>{if(!r){const e=new Set;this._activeKeyModifiers.forEach((t=>{e.add(t)})),this._activeKeyModifiers=e,r=!0}},n=(e,t)=>{t&&!this._activeKeyModifiers.has(e)?(i(),this._activeKeyModifiers.add(e)):!t&&this._activeKeyModifiers.has(e)&&(i(),this._activeKeyModifiers.delete(e))};if("key-down"===e||"key-up"===e){const r=t.key;this._keyModifiers.has(r)&&n(r,"key-down"===e)}const s=t.native;n("Alt",!(!s||!s.altKey)),n("Ctrl",!(!s||!s.ctrlKey)),n("Shift",!(!s||!s.shiftKey)),n("Meta",!(!s||!s.metaKey)),n("Primary",this._activeKeyModifiers.has(this.primaryKey))}_installRecognizers(){this._latestPointerHandler=new d(((e,t,r)=>this._setLatestPointer(e,t,r))),this._multiTouchHandler=new c,this.installHandlers("input-manager-logic",[this._latestPointerHandler,this._multiTouchHandler],P.ALWAYS),this.recognizers.length>0&&this.installHandlers("default",this.recognizers,P.INTERNAL)}_setPointerCapture(e,t,r,i){const n=e.name+"-"+t.priorityIndex,s=this._pointerCaptures.get(r.pointerId)||new Set;this._pointerCaptures.set(r.pointerId,s),i?(s.add(n),1===s.size&&this.eventSource&&this.eventSource.setPointerCapture(r,!0)):s.has(n)&&(s.delete(n),0===s.size&&(this._pointerCaptures.delete(r.pointerId),this.eventSource&&this.eventSource.setPointerCapture(r,!1)))}_garbageCollectRemovedHandlers(){this._handlers=this._handlers.filter((e=>!e.removed)),this.updateDependencies()}_emitInputEventFromSource(e,t,r,i){this._emitInputEvent(0,e,t,r,i)}_emitInputEvent(e,t,r,i,n,s){const o=void 0!==i?i:this._currentPropagation?this._currentPropagation.timestamp:performance.now(),a=void 0!==n&&n,l={event:new _(t,r,o,s||this._activeKeyModifiers,a),priorityIndex:e};this._currentPropagation?this._currentPropagation.events.push(l):this._doNewPropagation(l)}_doNewPropagation(e){this._currentPropagation={events:new s,currentHandler:null,needsHandlerGarbageCollect:!1,timestamp:e.event.timestamp},this._currentPropagation.events.push(e),this._continuePropagation()}_continuePropagation(){const e=n(this._currentPropagation);for(;e.events.length>0;){const{event:t,priorityIndex:r}=e.events.pop(),i=t.data&&t.data.eventId;if(!(null!=i&&this._stoppedPropagationEventIds.has(i)))for(e.currentHandler=this._handlersPriority[r];e.currentHandler;){if(e.currentHandler.removed)e.needsHandlerGarbageCollect=!0;else{if(e.currentHandler.active&&!t.shouldStopPropagation()&&e.currentHandler.eventCallback?.(t),t.shouldStopPropagation()){null!=i&&this._stoppedPropagationEventIds.add(i);break}if(t.shouldPausePropagation((()=>this._continuePropagation())))return void this._pausePropagation({event:t,priorityIndex:e.currentHandler.priorityIndex+1})}e.currentHandler=this._handlersPriority[e.currentHandler.priorityIndex+1]}}e.needsHandlerGarbageCollect&&this._garbageCollectRemovedHandlers(),this.hasPendingInputs||this._stoppedPropagationEventIds.clear(),this._currentPropagation=null,this._updateDependenciesAfterPropagation&&this.updateDependencies()}_pausePropagation(e){const t=new s;t.push(e);const r=this._currentPropagation;if(r){for(;r.events.length;)t.push(r.events.pop());r.events=t,r.currentHandler=null}}_compareHandlerPriority(e,t){if(e.handler.hasSideEffects!==t.handler.hasSideEffects)return e.handler.hasSideEffects?1:-1;if(e.groupPriority!==t.groupPriority)return e.groupPriority>t.groupPriority?-1:1;for(const r of e.handler.incomingEventMatches)for(const e of t.handler.incomingEventMatches){if(r.eventType!==e.eventType)continue;const t=r.keyModifiers.filter((t=>e.keyModifiers.includes(t)));if(t.length===r.keyModifiers.length!==(t.length===e.keyModifiers.length))return r.keyModifiers.length>e.keyModifiers.length?-1:1}return e.priorityIndex>t.priorityIndex?-1:1}_sortHandlersPriority(e){const t=[];for(const r of e){let e=0;for(;e=0;)e++;t.splice(e,0,r)}return t}get debug(){const e=e=>{const t=this._setPointerCapture;this._setPointerCapture=()=>{},e(),this._setPointerCapture=t};return{injectEvent:(t,r)=>{e((()=>{this._onEventReceived(t,r)}))},disablePointerCapture:e}}};e([o({readOnly:!0})],u.prototype,"hasPendingInputs",null),e([o({constructOnly:!0})],u.prototype,"eventSource",void 0),e([o({constructOnly:!0})],u.prototype,"recognizers",void 0),e([o()],u.prototype,"_latestPointerType",void 0),e([o()],u.prototype,"latestPointerType",null),e([o()],u.prototype,"multiTouchActive",null),e([o({readOnly:!0})],u.prototype,"latestPointerLocation",void 0),u=e([a("esri.views.input.InputManager")],u);class _{constructor(e,t,r,i,n){this.type=e,this.data=t,this.timestamp=r,this.modifiers=i,this.cancelable=n,this._propagationState=g.NONE,this._resumeCallback=null}stopPropagation(){this._propagationState|=g.STOPPED}shouldStopPropagation(){return 0!=(this._propagationState&g.STOPPED)}async(e){this._propagationState|=g.PAUSED;const t=(e,t)=>{this._propagationState&=~g.PAUSED;const r=this._resumeCallback;if(this._resumeCallback=null,r&&r(),t)throw e;return e};return("function"==typeof e?e():e).then((e=>t(e,!1)),(e=>t(e,!0)))}shouldPausePropagation(e){return!!(this._propagationState&g.PAUSED)&&(this._resumeCallback=e,!0)}preventDefault(){this.data.native.preventDefault()}}var g;!function(e){e[e.NONE=0]="NONE",e[e.STOPPED=1]="STOPPED",e[e.PAUSED=2]="PAUSED"}(g||(g={}));const P={ALWAYS:1,DEFAULT:0,TOOL:-1,WIDGET:-2,INTERNAL:-3};class v{}export{u as InputManager,P as ViewEventPriorities};