/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{GamepadKeyboardController as t}from"../../state/controllers/GamepadKeyboardController.js";import{InputHandler as i}from"../../../input/InputHandler.js";import{onVisibilityChange as e}from"../../../input/VisibilityChange.js";class o extends i{constructor(t,i){super(!0),this._view=t,this._keyToDirection=new Map,this._keyToAction=new Map,this._disableMovements={pan:!0,zoom:!1,ascend:!0,rotate:!1,mode:2},this._stickyKeyTimeoutHandle=void 0,this._stickyKeyDuration=200,this._addKeysMapping(i),this.registerIncoming("key-down",null,t=>this._handleKeyDown(t)),this.registerIncoming("key-up",null,t=>this._handleKeyUp(t)),this.registerIncoming("blur",null,()=>this._handleStop()),this._visibilityHandle=e(t=>t?null:this._handleStop())}onUninstall(){this._visibilityHandle?.remove(),this._handleStop()}setStickyKeyDuration(t){this._stickyKeyDuration=t}_addKeysMapping(t){this._addKeyMapping(t.pan.left,0),this._addKeyMapping(t.pan.right,1),this._addKeyMapping(t.pan.forward,2),this._addKeyMapping(t.pan.backward,3),this._addKeyMapping(t.pan.up,4),this._addKeyMapping(t.pan.down,5),this._addKeyMapping(t.lookAround.headingLeft,6),this._addKeyMapping(t.lookAround.headingRight,7),this._addKeyMapping(t.lookAround.tiltUp,8),this._addKeyMapping(t.lookAround.tiltDown,9),this._addKeyMapping(t.zoom.zoomIn,10),this._addKeyMapping(t.zoom.zoomOut,11),this._addKeyAction(t.reset.heading,()=>this._resetHeading()),this._addKeyAction(t.reset.tilt,()=>this._resetTilt())}_addKeyMapping(t,i){for(const e of this._eachKey(t))this._keyToDirection.set(e,i)}*_eachKey(t){"string"==typeof t?yield t:yield*t}_addKeyAction(t,i){for(const e of this._eachKey(t))this._keyToAction.set(e,i)}_handleKeyDown(i){if(i.data.native.ctrlKey||i.data.native.metaKey)return;const e=this._keyToAction.get(i.data.key);if(null!=e)return i.stopPropagation(),void e();const o=this._keyToDirection.get(i.data.key);if(null!=o&&(this._cameraControllerKeyboard&&this._cameraControllerKeyboard.running||(this._cameraControllerKeyboard=new t({view:this._view,disableMovements:this._disableMovements}),this._view.state.switchCameraController(this._cameraControllerKeyboard)),this._cameraControllerKeyboard.running)){if(i.stopPropagation(),this._cameraControllerKeyboard.directionActive(o))return;if(this._cameraControllerKeyboard.activateDirection(o),this._cameraControllerKeyboard.countActiveDirections()>1||!this._isPanning(o))return;this._stickyKeyDuration>0&&(this._stickyKeyTimeoutHandle=setTimeout(()=>{this._stickyKeyTimeoutHandle=void 0,null!=this._stickyDirection&&void 0!==this._stickyDirection&&(this._cameraControllerKeyboard?.deactivateDirection(this._stickyDirection),this._stickyDirection=void 0)},this._stickyKeyDuration))}}_handleStop(){this._cameraControllerKeyboard?.running&&(this._cameraControllerKeyboard.finishController(),this._cameraControllerKeyboard=null)}_handleKeyUp(t){if(t.data.native.ctrlKey||t.data.native.metaKey||!this._cameraControllerKeyboard?.running)return;const i=this._keyToDirection.get(t.data.key);if(null==i)return;t.stopPropagation();const e=void 0===this._stickyKeyTimeoutHandle;void 0===this._stickyKeyTimeoutHandle||1!==this._cameraControllerKeyboard?.countActiveDirections()?(this._cameraControllerKeyboard?.deactivateDirection(i),e&&(this._stickyDirection=void 0)):this._stickyDirection=i}_isPanning(t){return t<=3}_resetHeading(){this._view.goTo({heading:0}).catch(()=>{})}_resetTilt(){this._view.goTo({tilt:0}).catch(()=>{})}}export{o as KeyboardNavigation};
