import{DeferredDOMNode}from"./DOMModel.js";import{Capability,SDKModel}from"./SDKModel.js";export const CoreAxPropertyName={Name:"name",Description:"description",Value:"value",Role:"role"};export let CoreOrProtocolAxProperty;export class AccessibilityNode{constructor(e,t){this._accessibilityModel=e,this._agent=e._agent,this._id=t.nodeId,e._setAXNodeForAXId(this._id,this),t.backendDOMNodeId?(e._setAXNodeForBackendDOMNodeId(t.backendDOMNodeId,this),this._backendDOMNodeId=t.backendDOMNodeId,this._deferredDOMNode=new DeferredDOMNode(e.target(),t.backendDOMNodeId)):(this._backendDOMNodeId=null,this._deferredDOMNode=null),this._ignored=t.ignored,this._ignored&&"ignoredReasons"in t&&(this._ignoredReasons=t.ignoredReasons),this._role=t.role||null,this._name=t.name||null,this._description=t.description||null,this._value=t.value||null,this._properties=t.properties||null,this._childIds=t.childIds||null,this._parentNode=null}id(){return this._id}accessibilityModel(){return this._accessibilityModel}ignored(){return this._ignored}ignoredReasons(){return this._ignoredReasons||null}role(){return this._role||null}coreProperties(){const e=[];return this._name&&e.push({name:CoreAxPropertyName.Name,value:this._name}),this._description&&e.push({name:CoreAxPropertyName.Description,value:this._description}),this._value&&e.push({name:CoreAxPropertyName.Value,value:this._value}),e}name(){return this._name||null}description(){return this._description||null}value(){return this._value||null}properties(){return this._properties||null}parentNode(){return this._parentNode}_setParentNode(e){this._parentNode=e}isDOMNode(){return Boolean(this._backendDOMNodeId)}backendDOMNodeId(){return this._backendDOMNodeId}deferredDOMNode(){return this._deferredDOMNode}highlightDOMNode(){const e=this.deferredDOMNode();e&&e.highlight()}children(){if(!this._childIds)return[];const e=[];for(const t of this._childIds){const o=this._accessibilityModel.axNodeForId(t);o&&e.push(o)}return e}numChildren(){return this._childIds?this._childIds.length:0}hasOnlyUnloadedChildren(){return!(!this._childIds||!this._childIds.length)&&!this._childIds.some((e=>null!==this._accessibilityModel.axNodeForId(e)))}}export class AccessibilityModel extends SDKModel{constructor(e){super(e),this._agent=e.accessibilityAgent(),this.resumeModel(),this._axIdToAXNode=new Map,this._backendDOMNodeIdToAXNode=new Map}clear(){this._axIdToAXNode.clear()}async resumeModel(){await this._agent.invoke_enable()}async suspendModel(){await this._agent.invoke_disable()}async requestPartialAXTree(e){const{nodes:t}=await this._agent.invoke_getPartialAXTree({nodeId:e.id,backendNodeId:void 0,objectId:void 0,fetchRelatives:!0});if(t){for(const e of t)new AccessibilityNode(this,e);for(const e of this._axIdToAXNode.values())for(const t of e.children())t._setParentNode(e)}}async requestRootNode(e=2){const{nodes:t}=await this._agent.invoke_getFullAXTree({max_depth:e});if(!t)return;const o=t.map((e=>new AccessibilityNode(this,e)));for(const s of this._axIdToAXNode.values())for(const e of s.children())e._setParentNode(s);return o[0]}async requestAXChildren(e){const{nodes:t}=await this._agent.invoke_getChildAXNodes({id:e});if(!t)return;const o=[];for(const s of t)o.push(new AccessibilityNode(this,s));for(const s of this._axIdToAXNode.values())for(const e of s.children())e._setParentNode(s);return o}axNodeForId(e){return this._axIdToAXNode.get(e)||null}_setAXNodeForAXId(e,t){this._axIdToAXNode.set(e,t)}axNodeForDOMNode(e){return e?this._backendDOMNodeIdToAXNode.get(e.backendNodeId()):null}_setAXNodeForBackendDOMNodeId(e,t){this._backendDOMNodeIdToAXNode.set(e,t)}}SDKModel.register(AccessibilityModel,Capability.DOM,!1);