import*as Coverage from"../coverage/coverage.js";import*as Formatter from"../formatter/formatter.js";import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";import{Plugin}from"./Plugin.js";const UIStrings={clickToShowCoveragePanel:"Click to show Coverage Panel",showDetails:"Show Details",coverageS:"Coverage: {PH1} %",coverageNa:"Coverage: n/a"},str_=i18n.i18n.registerUIStrings("sources/CoveragePlugin.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class CoveragePlugin extends Plugin{constructor(e,t){super(),this._textEditor=e,this._uiSourceCode=t,this._originalSourceCode=Formatter.SourceFormatter.SourceFormatter.instance().getOriginalUISourceCode(this._uiSourceCode),this._text=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.clickToShowCoveragePanel)),this._text.setSecondary(),this._text.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,(()=>{UI.ViewManager.ViewManager.instance().showView("coverage")}));const o=SDK.SDKModel.TargetManager.instance().mainTarget();o&&(this._model=o.model(Coverage.CoverageModel.CoverageModel),this._model&&(this._model.addEventListener(Coverage.CoverageModel.Events.CoverageReset,this._handleReset,this),this._coverage=this._model.getCoverageForUrl(this._originalSourceCode.url()),this._coverage&&this._coverage.addEventListener(Coverage.CoverageModel.URLCoverageInfo.Events.SizesChanged,this._handleCoverageSizesChanged,this))),this._updateStats()}dispose(){this._coverage&&this._coverage.removeEventListener(Coverage.CoverageModel.URLCoverageInfo.Events.SizesChanged,this._handleCoverageSizesChanged,this),this._model&&this._model.removeEventListener(Coverage.CoverageModel.Events.CoverageReset,this._handleReset,this)}static accepts(e){return e.contentType().isDocumentOrScriptOrStyleSheet()}_handleReset(){this._coverage=null,this._updateStats()}_handleCoverageSizesChanged(){this._updateStats()}_updateStats(){this._coverage?(this._text.setTitle(i18nString(UIStrings.showDetails)),this._text.setText(i18nString(UIStrings.coverageS,{PH1:this._coverage.usedPercentage().toFixed(1)}))):(this._text.setTitle(i18nString(UIStrings.clickToShowCoveragePanel)),this._text.setText(i18nString(UIStrings.coverageNa)))}async rightToolbarItems(){return[this._text]}}