import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as UI from"../ui/ui.js";const UIStrings={reports:"Reports",newReport:"(new report)"},str_=i18n.i18n.registerUIStrings("lighthouse/LighthouseReportSelector.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class ReportSelector{constructor(e){this._renderNewLighthouseView=e,this._newLighthouseItem=document.createElement("option"),this._comboBox=new UI.Toolbar.ToolbarComboBox(this._handleChange.bind(this),i18nString(UIStrings.reports),"lighthouse-report"),this._comboBox.setMaxWidth(180),this._comboBox.setMinWidth(140),this._itemByOptionElement=new Map,this._setEmptyState()}_setEmptyState(){this._comboBox.selectElement().removeChildren(),this._comboBox.setEnabled(!1),this._newLighthouseItem=document.createElement("option"),this._newLighthouseItem.label=i18nString(UIStrings.newReport),this._comboBox.selectElement().appendChild(this._newLighthouseItem),this._comboBox.select(this._newLighthouseItem)}_handleChange(e){const t=this._selectedItem();t?t.select():this._renderNewLighthouseView()}_selectedItem(){const e=this._comboBox.selectedOption();return this._itemByOptionElement.get(e)}hasCurrentSelection(){return Boolean(this._selectedItem())}hasItems(){return this._itemByOptionElement.size>0}comboBox(){return this._comboBox}prepend(e){const t=e.optionElement(),o=this._comboBox.selectElement();this._itemByOptionElement.set(t,e),o.insertBefore(t,o.firstElementChild),this._comboBox.setEnabled(!0),this._comboBox.select(t),e.select()}clearAll(){for(const e of this._comboBox.options())e!==this._newLighthouseItem&&(this._itemByOptionElement.get(e)?.delete(),this._itemByOptionElement.delete(e));this._setEmptyState()}selectNewReport(){this._comboBox.select(this._newLighthouseItem)}}export class Item{constructor(e,t,o){this._lighthouseResult=e,this._renderReport=t,this._showLandingCallback=o;const i=new Common.ParsedURL.ParsedURL(e.finalUrl),s=e.fetchTime;this._element=document.createElement("option"),this._element.label=`${new Date(s).toLocaleTimeString()} - ${i.domain()}`}select(){this._renderReport()}optionElement(){return this._element}delete(){this._element&&this._element.remove(),this._showLandingCallback()}}