import*as i18n from"../i18n/i18n.js";import*as UI from"../ui/ui.js";import{AccessibilitySubPane}from"./AccessibilitySubPane.js";import{ariaMetadata}from"./ARIAMetadata.js";const UIStrings={ariaAttributes:"ARIA Attributes",noAriaAttributes:"No ARIA attributes"},str_=i18n.i18n.registerUIStrings("accessibility/ARIAAttributesView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class ARIAAttributesPane extends AccessibilitySubPane{constructor(){super(i18nString(UIStrings.ariaAttributes)),this._noPropertiesInfo=this.createInfo(i18nString(UIStrings.noAriaAttributes)),this._treeOutline=this.createTreeOutline()}setNode(t){if(super.setNode(t),this._treeOutline.removeChildren(),!t)return;const e=t.domModel().target(),i=t.attributes();for(let r=0;r<i.length;++r){const t=i[r];this._isARIAAttribute(t)&&this._treeOutline.appendChild(new ARIAAttributesTreeElement(this,t,e))}const a=0!==this._treeOutline.rootElement().childCount();this._noPropertiesInfo.classList.toggle("hidden",a),this._treeOutline.element.classList.toggle("hidden",!a)}_isARIAAttribute(t){return ATTRIBUTES.has(t.name)}}export class ARIAAttributesTreeElement extends UI.TreeOutline.TreeElement{constructor(t,e,i){super(""),this._parentPane=t,this._attribute=e,this.selectable=!1}static createARIAValueElement(t){const e=document.createElement("span");return e.classList.add("monospace"),e.setTextContentTruncatedIfNeeded(t||""),e}onattach(){this._populateListItem(),this.listItemElement.addEventListener("click",this._mouseClick.bind(this))}_populateListItem(){this.listItemElement.removeChildren(),this.appendNameElement(this._attribute.name),this.listItemElement.createChild("span","separator").textContent=":\xa0",this.appendAttributeValueElement(this._attribute.value)}appendNameElement(t){this._nameElement=document.createElement("span"),this._nameElement.textContent=t,this._nameElement.classList.add("ax-name"),this._nameElement.classList.add("monospace"),this.listItemElement.appendChild(this._nameElement)}appendAttributeValueElement(t){this._valueElement=ARIAAttributesTreeElement.createARIAValueElement(t),this.listItemElement.appendChild(this._valueElement)}_mouseClick(t){t.target!==this.listItemElement&&(t.consume(!0),this._startEditing())}_startEditing(){const t=this._valueElement;if(!t||UI.UIUtils.isBeingEdited(t))return;const e=t.textContent||"";const i=this._nameElement.textContent||"";this._prompt=new ARIAAttributePrompt(ariaMetadata().valuesForProperty(i),this),this._prompt.setAutocompletionTimeout(0);this._prompt.attachAndStartEditing(t,function(t,e){const i=e.target.textContent||"";this._editingCommitted(i,t)}.bind(this,e)).addEventListener("keydown",(t=>this._editingValueKeyDown(e,t)),!1);const a=t.getComponentSelection();a&&a.selectAllChildren(t)}_removePrompt(){this._prompt&&(this._prompt.detach(),delete this._prompt)}_editingCommitted(t,e){if(this._removePrompt(),t!==e){this._parentPane.node().setAttributeValue(this._attribute.name,t)}}_editingCancelled(){this._removePrompt(),this._populateListItem()}_editingValueKeyDown(t,e){if(!e.handled){if("Enter"===e.key){const i=e.target;return this._editingCommitted(i.textContent||"",t),void e.consume()}return isEscKey(e)?(this._editingCancelled(),void e.consume()):void 0}}}export class ARIAAttributePrompt extends UI.TextPrompt.TextPrompt{constructor(t,e){super(),this.initialize(this._buildPropertyCompletions.bind(this)),this._ariaCompletions=t,this._treeElement=e}async _buildPropertyCompletions(t,e,i){return(e=e.toLowerCase())||i||!t?this._ariaCompletions.filter((t=>t.startsWith(e))).map((t=>({text:t,title:void 0,subtitle:void 0,iconType:void 0,priority:void 0,isSecondary:void 0,subtitleRenderer:void 0,selectionRange:void 0,hideGhostText:void 0,iconElement:void 0}))):[]}}const ATTRIBUTES=new Set(["role","aria-activedescendant","aria-atomic","aria-autocomplete","aria-brailleroledescription","aria-busy","aria-checked","aria-colcount","aria-colindex","aria-colindextext","aria-colspan","aria-controls","aria-current","aria-describedby","aria-details","aria-disabled","aria-dropeffect","aria-errormessage","aria-expanded","aria-flowto","aria-grabbed","aria-haspopup","aria-hidden","aria-invalid","aria-keyshortcuts","aria-label","aria-labelledby","aria-level","aria-live","aria-modal","aria-multiline","aria-multiselectable","aria-orientation","aria-owns","aria-placeholder","aria-posinset","aria-pressed","aria-readonly","aria-relevant","aria-required","aria-roledescription","aria-rowcount","aria-rowindex","aria-rowindextext","aria-rowspan","aria-selected","aria-setsize","aria-sort","aria-valuemax","aria-valuemin","aria-valuenow","aria-valuetext"]);