import{assert}from"./assert.js";import{keyDefinitions}from"./USKeyboardLayout.js";export class Keyboard{constructor(t){this._modifiers=0,this._pressedKeys=new Set,this._client=t}async down(t,e={text:void 0}){const i=this._keyDescriptionForString(t),s=this._pressedKeys.has(i.code);this._pressedKeys.add(i.code),this._modifiers|=this._modifierBit(i.key);const o=void 0===e.text?i.text:e.text;await this._client.send("Input.dispatchKeyEvent",{type:o?"keyDown":"rawKeyDown",modifiers:this._modifiers,windowsVirtualKeyCode:i.keyCode,code:i.code,key:i.key,text:o,unmodifiedText:o,autoRepeat:s,location:i.location,isKeypad:3===i.location})}_modifierBit(t){return"Alt"===t?1:"Control"===t?2:"Meta"===t?4:"Shift"===t?8:0}_keyDescriptionForString(t){const e=8&this._modifiers,i={key:"",keyCode:0,code:"",text:"",location:0},s=keyDefinitions[t];return assert(s,`Unknown key: "${t}"`),s.key&&(i.key=s.key),e&&s.shiftKey&&(i.key=s.shiftKey),s.keyCode&&(i.keyCode=s.keyCode),e&&s.shiftKeyCode&&(i.keyCode=s.shiftKeyCode),s.code&&(i.code=s.code),s.location&&(i.location=s.location),1===i.key.length&&(i.text=i.key),s.text&&(i.text=s.text),e&&s.shiftText&&(i.text=s.shiftText),-9&this._modifiers&&(i.text=""),i}async up(t){const e=this._keyDescriptionForString(t);this._modifiers&=~this._modifierBit(e.key),this._pressedKeys.delete(e.code),await this._client.send("Input.dispatchKeyEvent",{type:"keyUp",modifiers:this._modifiers,key:e.key,windowsVirtualKeyCode:e.keyCode,code:e.code,location:e.location})}async sendCharacter(t){await this._client.send("Input.insertText",{text:t})}charIsKey(t){return!!keyDefinitions[t]}async type(t,e={}){const i=e.delay||null;for(const s of t)this.charIsKey(s)?await this.press(s,{delay:i}):(i&&await new Promise((t=>setTimeout(t,i))),await this.sendCharacter(s))}async press(t,e={}){const{delay:i=null}=e;await this.down(t,e),i&&await new Promise((t=>setTimeout(t,e.delay))),await this.up(t)}}export class Mouse{constructor(t,e){this._x=0,this._y=0,this._button="none",this._client=t,this._keyboard=e}async move(t,e,i={}){const{steps:s=1}=i,o=this._x,n=this._y;this._x=t,this._y=e;for(let a=1;a<=s;a++)await this._client.send("Input.dispatchMouseEvent",{type:"mouseMoved",button:this._button,x:o+(this._x-o)*(a/s),y:n+(this._y-n)*(a/s),modifiers:this._keyboard._modifiers})}async click(t,e,i={}){const{delay:s=null}=i;null!==s?(await Promise.all([this.move(t,e),this.down(i)]),await new Promise((t=>setTimeout(t,s))),await this.up(i)):await Promise.all([this.move(t,e),this.down(i),this.up(i)])}async down(t={}){const{button:e="left",clickCount:i=1}=t;this._button=e,await this._client.send("Input.dispatchMouseEvent",{type:"mousePressed",button:e,x:this._x,y:this._y,modifiers:this._keyboard._modifiers,clickCount:i})}async up(t={}){const{button:e="left",clickCount:i=1}=t;this._button="none",await this._client.send("Input.dispatchMouseEvent",{type:"mouseReleased",button:e,x:this._x,y:this._y,modifiers:this._keyboard._modifiers,clickCount:i})}async wheel(t={}){const{deltaX:e=0,deltaY:i=0}=t;await this._client.send("Input.dispatchMouseEvent",{type:"mouseWheel",x:this._x,y:this._y,deltaX:e,deltaY:i,modifiers:this._keyboard._modifiers,pointerType:"mouse"})}}export class Touchscreen{constructor(t,e){this._client=t,this._keyboard=e}async tap(t,e){const i=[{x:Math.round(t),y:Math.round(e)}];await this._client.send("Input.dispatchTouchEvent",{type:"touchStart",touchPoints:i,modifiers:this._keyboard._modifiers}),await this._client.send("Input.dispatchTouchEvent",{type:"touchEnd",touchPoints:[],modifiers:this._keyboard._modifiers})}}