import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as UI from"../ui/ui.js";import{IsolateSelector}from"./IsolateSelector.js";const UIStrings={selectJavascriptVmInstance:"Select JavaScript VM instance",load:"Load",takeSnapshot:"Take snapshot",stop:"Stop",start:"Start",selectProfilingType:"Select profiling type"},str_=i18n.i18n.registerUIStrings("profiler/ProfileLauncherView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class ProfileLauncherView extends UI.Widget.VBox{constructor(t){super(),this.registerRequiredCSS("profiler/profileLauncherView.css",{enableLegacyPatching:!0}),this._panel=t,this.element.classList.add("profile-launcher-view"),this._contentElement=this.element.createChild("div","profile-launcher-view-content vbox");const e=this._contentElement.createChild("div","vbox");this._selectedProfileTypeSetting=Common.Settings.Settings.instance().createSetting("selectedProfileType","CPU"),this._profileTypeHeaderElement=e.createChild("h1"),this._profileTypeSelectorForm=e.createChild("form"),UI.ARIAUtils.markAsRadioGroup(this._profileTypeSelectorForm);const i=this._contentElement.createChild("div","vbox profile-isolate-selector-block");i.createChild("h1").textContent=i18nString(UIStrings.selectJavascriptVmInstance);const o=new IsolateSelector;o.show(i.createChild("div","vbox profile-launcher-target-list")),i.appendChild(o.totalMemoryElement());const n=this._contentElement.createChild("div","hbox profile-launcher-buttons");this._controlButton=UI.UIUtils.createTextButton("",this._controlButtonClicked.bind(this),"",!0),this._loadButton=UI.UIUtils.createTextButton(i18nString(UIStrings.load),this._loadButtonClicked.bind(this),""),n.appendChild(this._controlButton),n.appendChild(this._loadButton),this._recordButtonEnabled=!0,this._typeIdToOptionElementAndProfileType=new Map}_loadButtonClicked(){this._panel.showLoadFromFileDialog()}_updateControls(){this._isEnabled&&this._recordButtonEnabled?this._controlButton.removeAttribute("disabled"):this._controlButton.setAttribute("disabled",""),UI.Tooltip.Tooltip.install(this._controlButton,this._recordButtonEnabled?"":UI.UIUtils.anotherProfilerActiveLabel()),this._isInstantProfile?(this._controlButton.classList.remove("running"),this._controlButton.classList.add("primary-button"),this._controlButton.textContent=i18nString(UIStrings.takeSnapshot)):this._isProfiling?(this._controlButton.classList.add("running"),this._controlButton.classList.remove("primary-button"),this._controlButton.textContent=i18nString(UIStrings.stop)):(this._controlButton.classList.remove("running"),this._controlButton.classList.add("primary-button"),this._controlButton.textContent=i18nString(UIStrings.start));for(const{optionElement:t}of this._typeIdToOptionElementAndProfileType.values())t.disabled=Boolean(this._isProfiling)}profileStarted(){this._isProfiling=!0,this._updateControls()}profileFinished(){this._isProfiling=!1,this._updateControls()}updateProfileType(t,e){this._isInstantProfile=t.isInstantProfile(),this._recordButtonEnabled=e,this._isEnabled=t.isEnabled(),this._updateControls()}addProfileType(t){const e=UI.UIUtils.createRadioLabel("profile-type",t.name);this._profileTypeSelectorForm.appendChild(e);const i=e.radioElement;this._typeIdToOptionElementAndProfileType.set(t.id,{optionElement:i,profileType:t}),i.addEventListener("change",this._profileTypeChanged.bind(this,t),!1);this._profileTypeSelectorForm.createChild("p").textContent=t.description,UI.ARIAUtils.setDescription(i,t.description);const o=t.customContent();o&&(this._profileTypeSelectorForm.createChild("p").appendChild(o),t.setCustomContentEnabled(!1));const n=this._typeIdToOptionElementAndProfileType.size>1?i18nString(UIStrings.selectProfilingType):t.name;this._profileTypeHeaderElement.textContent=n,UI.ARIAUtils.setAccessibleName(this._profileTypeSelectorForm,n)}restoreSelectedProfileType(){let t=this._selectedProfileTypeSetting.get();this._typeIdToOptionElementAndProfileType.has(t)||(t=this._typeIdToOptionElementAndProfileType.keys().next().value,this._selectedProfileTypeSetting.set(t));const e=this._typeIdToOptionElementAndProfileType.get(t);e.optionElement.checked=!0;const i=e.profileType;for(const[o,{profileType:n}]of this._typeIdToOptionElementAndProfileType){const e=o===t;n.setCustomContentEnabled(e)}this.dispatchEventToListeners(Events.ProfileTypeSelected,i)}_controlButtonClicked(){this._panel.toggleRecord()}_profileTypeChanged(t){const e=this._selectedProfileTypeSetting.get();this._typeIdToOptionElementAndProfileType.get(e).profileType.setCustomContentEnabled(!1),t.setCustomContentEnabled(!0),this.dispatchEventToListeners(Events.ProfileTypeSelected,t),this._isInstantProfile=t.isInstantProfile(),this._isEnabled=t.isEnabled(),this._updateControls(),this._selectedProfileTypeSetting.set(t.id)}}export var Events;!function(t){t.ProfileTypeSelected="ProfileTypeSelected"}(Events||(Events={}));