import*as i18n from"../i18n/i18n.js";import*as UI from"../ui/ui.js";import{Events as ProfileHeaderEvents}from"./ProfileHeader.js";const UIStrings={save:"Save",saveWithEllipsis:"Save\u2026",singlePlaceholder:"{PH1}",load:"Load\u2026",delete:"Delete"},str_=i18n.i18n.registerUIStrings("profiler/ProfileSidebarTreeElement.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let sharedFileSelectorElement=null;function getSharedFileSelectorElement(){return sharedFileSelectorElement}export function setSharedFileSelectorElement(e){sharedFileSelectorElement=e}export class ProfileSidebarTreeElement extends UI.TreeOutline.TreeElement{constructor(e,t,i){super("",!1),this._iconElement=document.createElement("div"),this._iconElement.classList.add("icon"),this._titlesElement=document.createElement("div"),this._titlesElement.classList.add("titles"),this._titlesElement.classList.add("no-subtitle"),this._titleContainer=this._titlesElement.createChild("span","title-container"),this.titleElement=this._titleContainer.createChild("span","title"),this._subtitleElement=this._titlesElement.createChild("span","subtitle"),this.titleElement.textContent=t.title,this._className=i,this._small=!1,this._dataDisplayDelegate=e,this.profile=t,t.addEventListener(ProfileHeaderEvents.UpdateStatus,this._updateStatus,this),t.canSaveToFile()?this._createSaveLink():t.addEventListener(ProfileHeaderEvents.ProfileReceived,this._onProfileReceived,this)}_createSaveLink(){this._saveLinkElement=this._titleContainer.createChild("span","save-link"),this._saveLinkElement.textContent=i18nString(UIStrings.save),this._saveLinkElement.addEventListener("click",this._saveProfile.bind(this),!1)}_onProfileReceived(e){this._createSaveLink()}_updateStatus(e){const t=e.data;null!==t.subtitle&&(this._subtitleElement.textContent=t.subtitle||"",this._titlesElement.classList.toggle("no-subtitle",!t.subtitle)),"boolean"===typeof t.wait&&this.listItemElement&&(this._iconElement.classList.toggle("spinner",t.wait),this.listItemElement.classList.toggle("wait",t.wait))}ondblclick(e){return this._editing||this._startEditing(e.target),!1}_startEditing(e){const t=e.enclosingNodeOrSelfWithClass("title");if(!t)return;const i=new UI.InplaceEditor.Config(this._editingCommitted.bind(this),this._editingCancelled.bind(this));this._editing=UI.InplaceEditor.InplaceEditor.startEditing(t,i)}_editingCommitted(e,t){delete this._editing,this.profile.setTitle(t)}_editingCancelled(){delete this._editing}dispose(){this.profile.removeEventListener(ProfileHeaderEvents.UpdateStatus,this._updateStatus,this),this.profile.removeEventListener(ProfileHeaderEvents.ProfileReceived,this._onProfileReceived,this)}onselect(){return this._dataDisplayDelegate.showProfile(this.profile),!0}ondelete(){return this.profile.profileType().removeProfile(this.profile),!0}onattach(){this._className&&this.listItemElement.classList.add(this._className),this._small&&this.listItemElement.classList.add("small"),this.listItemElement.append(this._iconElement,this._titlesElement),this.listItemElement.addEventListener("contextmenu",this._handleContextMenuEvent.bind(this),!0),UI.ARIAUtils.setDescription(this.listItemElement,i18nString(UIStrings.singlePlaceholder,{PH1:this.profile.profileType().name}))}_handleContextMenuEvent(e){const t=this.profile,i=new UI.ContextMenu.ContextMenu(e),s=getSharedFileSelectorElement();if(!s)throw new Error("File selector element shared by ProfilePanel instances is missing");i.headerSection().appendItem(i18nString(UIStrings.load),s.click.bind(s)),t.canSaveToFile()&&i.saveSection().appendItem(i18nString(UIStrings.saveWithEllipsis),t.saveToFile.bind(t)),i.footerSection().appendItem(i18nString(UIStrings.delete),this.ondelete.bind(this)),i.show()}_saveProfile(e){this.profile.saveToFile()}setSmall(e){this._small=e,this.listItemElement&&this.listItemElement.classList.toggle("small",this._small)}setMainTitle(e){this.titleElement.textContent=e}}