import*as SDK from"../sdk/sdk.js";export class InputModel extends SDK.SDKModel.SDKModel{constructor(t){super(t),this._inputAgent=t.inputAgent(),this._eventDispatchTimer=0,this._dispatchEventDataList=[],this._finishCallback=null,this._reset()}_reset(){this._lastEventTime=null,this._replayPaused=!1,this._dispatchingIndex=0,window.clearTimeout(this._eventDispatchTimer)}setEvents(t){this._dispatchEventDataList=[];for(const e of t.sortedProcesses())for(const s of e.sortedThreads())this._processThreadEvents(t,s);this._dispatchEventDataList.sort((function(t,e){return t.timestamp-e.timestamp}))}startReplay(t){this._reset(),this._finishCallback=t,this._dispatchEventDataList.length?this._dispatchNextEvent():this._replayStopped()}pause(){window.clearTimeout(this._eventDispatchTimer),this._dispatchingIndex>=this._dispatchEventDataList.length?this._replayStopped():this._replayPaused=!0}resume(){this._replayPaused=!1,this._dispatchingIndex<this._dispatchEventDataList.length&&this._dispatchNextEvent()}_processThreadEvents(t,e){for(const s of e.events())"EventDispatch"===s.name&&this._isValidInputEvent(s.args.data)&&this._dispatchEventDataList.push(s.args.data)}_isValidInputEvent(t){return this._isMouseEvent(t)||this._isKeyboardEvent(t)}_isMouseEvent(t){return!!MOUSE_EVENT_TYPE_TO_REQUEST_TYPE.has(t.type)&&("x"in t&&"y"in t)}_isKeyboardEvent(t){return!!KEYBOARD_EVENT_TYPE_TO_REQUEST_TYPE.has(t.type)&&("code"in t&&"key"in t)}_dispatchNextEvent(){const t=this._dispatchEventDataList[this._dispatchingIndex];if(this._lastEventTime=t.timestamp,MOUSE_EVENT_TYPE_TO_REQUEST_TYPE.has(t.type)?this._dispatchMouseEvent(t):KEYBOARD_EVENT_TYPE_TO_REQUEST_TYPE.has(t.type)&&this._dispatchKeyEvent(t),++this._dispatchingIndex,this._dispatchingIndex<this._dispatchEventDataList.length){const t=(this._dispatchEventDataList[this._dispatchingIndex].timestamp-this._lastEventTime)/1e3;this._eventDispatchTimer=window.setTimeout(this._dispatchNextEvent.bind(this),t)}else this._replayStopped()}async _dispatchMouseEvent(t){const e=MOUSE_EVENT_TYPE_TO_REQUEST_TYPE.get(t.type);if(!e)throw new Error(`Could not find mouse event type for eventData ${t.type}`);const s=BUTTONID_TO_ACTION_NAME.get(t.button),i={type:e,x:t.x,y:t.y,modifiers:t.modifiers,button:"mousedown"===t.type||"mouseup"===t.type?s:Protocol.Input.MouseButton.None,buttons:t.buttons,clickCount:t.clickCount,deltaX:t.deltaX,deltaY:t.deltaY};await this._inputAgent.invoke_dispatchMouseEvent(i)}async _dispatchKeyEvent(t){const e=KEYBOARD_EVENT_TYPE_TO_REQUEST_TYPE.get(t.type);if(!e)throw new Error(`Could not find key event type for eventData ${t.type}`);const s="keypress"===t.type?t.key[0]:void 0,i={type:e,modifiers:t.modifiers,text:s,unmodifiedText:s?s.toLowerCase():void 0,code:t.code,key:t.key};await this._inputAgent.invoke_dispatchKeyEvent(i)}_replayStopped(){window.clearTimeout(this._eventDispatchTimer),this._reset(),this._finishCallback&&this._finishCallback()}}const MOUSE_EVENT_TYPE_TO_REQUEST_TYPE=new Map([["mousedown",Protocol.Input.DispatchMouseEventRequestType.MousePressed],["mouseup",Protocol.Input.DispatchMouseEventRequestType.MouseReleased],["mousemove",Protocol.Input.DispatchMouseEventRequestType.MouseMoved],["wheel",Protocol.Input.DispatchMouseEventRequestType.MouseWheel]]),KEYBOARD_EVENT_TYPE_TO_REQUEST_TYPE=new Map([["keydown",Protocol.Input.DispatchKeyEventRequestType.KeyDown],["keyup",Protocol.Input.DispatchKeyEventRequestType.KeyUp],["keypress",Protocol.Input.DispatchKeyEventRequestType.Char]]),BUTTONID_TO_ACTION_NAME=new Map([[0,Protocol.Input.MouseButton.Left],[1,Protocol.Input.MouseButton.Middle],[2,Protocol.Input.MouseButton.Right],[3,Protocol.Input.MouseButton.Back],[4,Protocol.Input.MouseButton.Forward]]);SDK.SDKModel.SDKModel.register(InputModel,SDK.SDKModel.Capability.Input,!1);