import*as Host from"../host/host.js";import*as ObjectUI from"../object_ui/object_ui.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";let propertiesWidgetInstance;export class PropertiesWidget extends UI.ThrottledWidget.ThrottledWidget{constructor(){super(!0),this.registerRequiredCSS("elements/propertiesWidget.css",{enableLegacyPatching:!1}),SDK.SDKModel.TargetManager.instance().addModelListener(SDK.DOMModel.DOMModel,SDK.DOMModel.Events.AttrModified,this._onNodeChange,this),SDK.SDKModel.TargetManager.instance().addModelListener(SDK.DOMModel.DOMModel,SDK.DOMModel.Events.AttrRemoved,this._onNodeChange,this),SDK.SDKModel.TargetManager.instance().addModelListener(SDK.DOMModel.DOMModel,SDK.DOMModel.Events.CharacterDataModified,this._onNodeChange,this),SDK.SDKModel.TargetManager.instance().addModelListener(SDK.DOMModel.DOMModel,SDK.DOMModel.Events.ChildNodeCountUpdated,this._onNodeChange,this),UI.Context.Context.instance().addFlavorChangeListener(SDK.DOMModel.DOMNode,this._setNode,this),this._node=UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode),this._treeOutline=new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeOutline({readOnly:!0}),this._treeOutline.setShowSelectionOnKeyboardFocus(!0,!1),this._expandController=new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSectionsTreeExpandController(this._treeOutline),this.contentElement.appendChild(this._treeOutline.element),this._treeOutline.addEventListener(UI.TreeOutline.Events.ElementExpanded,(()=>{Host.userMetrics.actionTaken(Host.UserMetrics.Action.DOMPropertiesExpanded)})),this.update()}static instance(e={forceNew:null}){const{forceNew:t}=e;return propertiesWidgetInstance&&!t||(propertiesWidgetInstance=new PropertiesWidget),propertiesWidgetInstance}_setNode(e){this._node=e.data,this.update()}async doUpdate(){if(this._lastRequestedNode&&(this._lastRequestedNode.domModel().runtimeModel().releaseObjectGroup(_objectGroupName),delete this._lastRequestedNode),!this._node)return void this._treeOutline.removeChildren();this._lastRequestedNode=this._node;const e=await this._node.resolveToObject(_objectGroupName);if(!e)return;const t=await e.callFunction((function(){let e=this;const t={__proto__:null};let o=1;for(;e;)t[o++]=e,e=e.__proto__;return t}));if(e.release(),!t.object||t.wasThrown)return;const o=await t.object.getOwnProperties(!1);if(t.object.release(),!o||!o.properties)return;const n=o.properties;this._treeOutline.removeChildren();let s=!1;for(let i=0;i<n.length;++i){if(!parseInt(n[i].name,10))continue;const e=n[i].value;if(!e)continue;let t=e.description;if(!t)continue;t=t.replace(/Prototype$/,"");const o=this._createSectionTreeElement(e,t);this._treeOutline.appendChild(o),s||(o.select(!0,!1),s=!0)}}_createSectionTreeElement(e,t){const o=document.createElement("span");o.classList.add("tree-element-title"),o.textContent=t;const n=new ObjectUI.ObjectPropertiesSection.RootElement(e);return n.title=o,this._expandController.watchSection(t,n),n}_onNodeChange(e){if(!this._node)return;const t=e.data,o=t instanceof SDK.DOMModel.DOMNode?t:t.node;this._node===o&&this.update()}}export const _objectGroupName="properties-sidebar-pane";