import*as Elements from"../elements/elements.js";import*as SDK from"../sdk/sdk.js";import{WaitForNavigationCondition}from"./Conditions.js";import{ChangeStep,ClickStep,CloseStep,StepFrameContext,SubmitStep}from"./Steps.js";const RELEVANT_ROLES_FOR_ARIA_SELECTORS=new Set(["button","link","textbox","checkbox"]);export class RecordingEventHandler{constructor(e,t){this.target=t,this.session=e,this.lastStep=null,this.lastStepTimeout=null,this.runtimeAgent=t.runtimeAgent(),this.debuggerAgent=t.debuggerAgent(),this.runtimeModel=t.model(SDK.RuntimeModel.RuntimeModel),this.resourceTreeModel=t.model(SDK.ResourceTreeModel.ResourceTreeModel),this.domModel=t.model(SDK.DOMModel.DOMModel),this.axModel=t.model(SDK.AccessibilityModel.AccessibilityModel)}async isSubmitButton(e){const{result:t}=await this.runtimeAgent.invoke_callFunctionOn({functionDeclaration:function(){return"BUTTON"===this.tagName&&"submit"===this.type&&null!==this.form}.toString(),objectId:e});return t.value}async getSelector(e){const t=await this.getAriaSelector(e);if(t)return t;const n=Elements.DOMPath.cssPath(e);return n||null}async findTargetId(e,t){const n=e.find((e=>Boolean(e&&e.value&&e.value.className&&t.includes(e.value.className))));if(!n||!n.value||!n.value.objectId)return null;const i=await this.runtimeAgent.invoke_getProperties({objectId:n.value.objectId});if(!i)return null;const o=i.result.find((e=>"target"===e.name));return o&&o.value&&o.value.objectId||null}async getAriaSelector(e){await this.axModel.requestPartialAXTree(e);let t=this.axModel.axNodeForDOMNode(e);for(;t;){const e=t.role(),n=t.name(),i=e?e.value:null,o=n?n.value:null;if(o&&RELEVANT_ROLES_FOR_ARIA_SELECTORS.has(i))return`aria/${o}`;t=t.parentNode()}return null}async handleClickEvent(e,t){const n=await this.findTargetId(t,["MouseEvent","PointerEvent"]);if(!n)return void this.skip();const i=await this.domModel.pushNodeToFrontend(n);if(!i)throw new Error("Node should not be null.");if("BUTTON"===i.nodeName()&&"submit"===i.getAttribute("type"))return void this.skip();const o=await this.getSelector(i);if(!o)throw new Error("Could not find selector");this.appendStep(new ClickStep(e,o)),await this.resume()}async handleSubmitEvent(e,t){const n=await this.findTargetId(t,["SubmitEvent"]);if(!n)return void this.skip();const i=await this.domModel.pushNodeToFrontend(n);if(!i)throw new Error("Node should not be null.");const o=await this.getSelector(i);if(!o)throw new Error("Could not find selector");this.appendStep(new SubmitStep(e,o)),await this.resume()}async handleChangeEvent(e,t){const n=await this.findTargetId(t,["Event"]);if(!n)return void this.skip();const i=await this.domModel.pushNodeToFrontend(n);if(!i)throw new Error("Node should not be null.");const o=await this.getSelector(i);if(!o)throw new Error("Could not find selector");const{result:s}=await this.runtimeAgent.invoke_callFunctionOn({functionDeclaration:function(){return this.value}.toString(),objectId:n});this.appendStep(new ChangeStep(e,o,s.value)),await this.resume()}getTarget(){return"main"===this.target.id()?"main":this.target.inspectedURL()}getContextForFrame(e){const t=[];let n=e;for(;n;){const e=n.parentFrame();if(!e)break;const i=e.childFrames.indexOf(n);t.unshift(i),n=e}const i=this.getTarget();return new StepFrameContext(i,t)}async resume(){await this.debuggerAgent.invoke_setSkipAllPauses({skip:!0}),await this.debuggerAgent.invoke_resume({terminateOnResume:!1}),await this.debuggerAgent.invoke_setSkipAllPauses({skip:!1})}async skip(){await this.debuggerAgent.invoke_resume({terminateOnResume:!1})}paused(e){if("EventListener"!==e.reason)return void this.skip();const t=e.data.eventName,n=e.callFrames[0].scopeChain[0],i=e.callFrames[0].location.scriptId,o=this.runtimeModel.executionContextIdForScriptId(i),s=this.runtimeModel.executionContext(o);if(!s)throw new Error("Could not find execution context.");if(!s.frameId)throw new Error("Execution context is not assigned to a frame.");const r=this.resourceTreeModel.frameForId(s.frameId);if(!r)throw new Error("Could not find frame.");const a=this.getContextForFrame(r);n.object.objectId&&this.runtimeAgent.invoke_getProperties({objectId:n.object.objectId}).then((async({result:e})=>{switch(t){case"listener:click":return this.handleClickEvent(a,e);case"listener:submit":return this.handleSubmitEvent(a,e);case"listener:change":return this.handleChangeEvent(a,e);default:this.skip()}}))}appendStep(e){this.session.appendStep(e),this.lastStep=e,this.lastStepTimeout&&window.clearTimeout(this.lastStepTimeout),this.lastStepTimeout=window.setTimeout((()=>{this.lastStep=null,this.lastStepTimeout=null}),1e3)}addConditionToLastStep(e){this.lastStep&&this.lastStep.addCondition(e)}targetDestroyed(){this.appendStep(new CloseStep(this.getTarget()))}targetInfoChanged(e){this.addConditionToLastStep(new WaitForNavigationCondition(e))}breakpointResolved(){}resumed(){}scriptFailedToParse(){}scriptParsed(){}}