export class Accessibility{constructor(e){this._client=e}async snapshot(e={}){const{interestingOnly:s=!0,root:t=null}=e,{nodes:i}=await this._client.send("Accessibility.getFullAXTree");let a=null;if(t){const{node:e}=await this._client.send("DOM.describeNode",{objectId:t._remoteObject.objectId});a=e.backendNodeId}const o=AXNode.createTree(i);let l=o;if(a&&(l=o.find((e=>e.payload.backendDOMNodeId===a)),!l))return null;if(!s)return this.serializeTree(l)[0];const n=new Set;return this.collectInterestingNodes(n,o,!1),n.has(l)?this.serializeTree(l,n)[0]:null}serializeTree(e,s){const t=[];for(const a of e.children)t.push(...this.serializeTree(a,s));if(s&&!s.has(e))return t;const i=e.serialize();return t.length&&(i.children=t),[i]}collectInterestingNodes(e,s,t){if(s.isInteresting(t)&&e.add(s),!s.isLeafNode()){t=t||s.isControl();for(const i of s.children)this.collectInterestingNodes(e,i,t)}}}class AXNode{constructor(e){this.children=[],this._richlyEditable=!1,this._editable=!1,this._focusable=!1,this._hidden=!1,this.payload=e,this._name=this.payload.name?this.payload.name.value:"",this._role=this.payload.role?this.payload.role.value:"Unknown",this._ignored=this.payload.ignored;for(const s of this.payload.properties||[])"editable"===s.name&&(this._richlyEditable="richtext"===s.value.value,this._editable=!0),"focusable"===s.name&&(this._focusable=s.value.value),"hidden"===s.name&&(this._hidden=s.value.value)}_isPlainTextField(){return!this._richlyEditable&&(!!this._editable||("textbox"===this._role||"searchbox"===this._role))}_isTextOnlyObject(){const e=this._role;return"LineBreak"===e||"text"===e||"InlineTextBox"===e}_hasFocusableChild(){if(void 0===this._cachedHasFocusableChild){this._cachedHasFocusableChild=!1;for(const e of this.children)if(e._focusable||e._hasFocusableChild()){this._cachedHasFocusableChild=!0;break}}return this._cachedHasFocusableChild}find(e){if(e(this))return this;for(const s of this.children){const t=s.find(e);if(t)return t}return null}isLeafNode(){if(!this.children.length)return!0;if(this._isPlainTextField()||this._isTextOnlyObject())return!0;switch(this._role){case"doc-cover":case"graphics-symbol":case"img":case"Meter":case"scrollbar":case"slider":case"separator":case"progressbar":return!0}return!this._hasFocusableChild()&&(!(!this._focusable||!this._name)||!("heading"!==this._role||!this._name))}isControl(){switch(this._role){case"button":case"checkbox":case"ColorWell":case"combobox":case"DisclosureTriangle":case"listbox":case"menu":case"menubar":case"menuitem":case"menuitemcheckbox":case"menuitemradio":case"radio":case"scrollbar":case"searchbox":case"slider":case"spinbutton":case"switch":case"tab":case"textbox":case"tree":case"treeitem":return!0;default:return!1}}isInteresting(e){return"Ignored"!==this._role&&!this._hidden&&!this._ignored&&(!(!this._focusable&&!this._richlyEditable)||(!!this.isControl()||!e&&(this.isLeafNode()&&!!this._name)))}serialize(){const e=new Map;for(const d of this.payload.properties||[])e.set(d.name.toLowerCase(),d.value.value);this.payload.name&&e.set("name",this.payload.name.value),this.payload.value&&e.set("value",this.payload.value.value),this.payload.description&&e.set("description",this.payload.description.value);const s={role:this._role},t=["name","value","description","keyshortcuts","roledescription","valuetext"];for(const d of t)e.has(d)&&(s[d]=(i=d,e.get(i)));var i;const a=["disabled","expanded","focused","modal","multiline","multiselectable","readonly","required","selected"],o=s=>e.get(s);for(const d of a){if("focused"===d&&"WebArea"===this._role)continue;o(d)&&(s[d]=o(d))}const l=["checked","pressed"];for(const d of l){if(!e.has(d))continue;const t=e.get(d);s[d]="mixed"===t?"mixed":"true"===t}const n=["level","valuemax","valuemin"],r=s=>e.get(s);for(const d of n)e.has(d)&&(s[d]=r(d));const c=["autocomplete","haspopup","invalid","orientation"],h=s=>e.get(s);for(const d of c){const e=h(d);e&&"false"!==e&&(s[d]=h(d))}return s}static createTree(e){const s=new Map;for(const t of e)s.set(t.nodeId,new AXNode(t));for(const t of s.values())for(const e of t.payload.childIds||[])t.children.push(s.get(e));return s.values().next().value}}