import*as BrowserSDK from"../browser_sdk/browser_sdk.js";import*as Common from"../common/common.js";import*as Host from"../host/host.js";import*as i18n from"../i18n/i18n.js";import*as Root from"../root/root.js";import*as SDK from"../sdk/sdk.js";import*as Components from"../ui/components/components.js";import*as UI from"../ui/ui.js";const UIStrings={sErrors:"{n, plural, =1 {# error} other {# errors}}",sWarnings:"{n, plural, =1 {# warning} other {# warnings}}",openConsoleToViewS:"Open Console to view {PH1}",openLighthouseToView:"{n, plural, =1 {Open Lighthouse to view # violation} other {Open Lighthouse to view # violations}}",openIssuesToView:"{n, plural, =1 {Open Issues to view # issue} other {Open Issues to view # issues}}"},str_=i18n.i18n.registerUIStrings("console_counters/WarningErrorCounter.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let warningErrorCounterInstance;export class WarningErrorCounter{constructor(){WarningErrorCounter._instanceForTest=this;const e=document.createElement("div");this._toolbarItem=new UI.Toolbar.ToolbarItem(e),this._toolbarItem.setVisible(!1),this._consoleCounter=new Components.IconButton.IconButton,e.appendChild(this._consoleCounter),this._consoleCounter.data={clickHandler:Common.Console.Console.instance().show.bind(Common.Console.Console.instance()),groups:[{iconName:"error_icon"},{iconName:"warning_icon"}]},this._violationCounter=null,Root.Runtime.experiments.isEnabled("spotlight")&&(this._violationCounter=new Components.IconButton.IconButton,e.appendChild(this._violationCounter),this._violationCounter.data={clickHandler:()=>UI.ViewManager.ViewManager.instance().showView("lighthouse"),groups:[{iconName:"ic_info_black_18dp",iconColor:"#2a53cd"}]}),this._issuesCounter=new Components.IconButton.IconButton,e.appendChild(this._issuesCounter),this._issuesCounter.data={clickHandler:()=>{Host.userMetrics.issuesPanelOpenedFrom(Host.UserMetrics.IssueOpener.StatusBarIssuesCounter),UI.ViewManager.ViewManager.instance().showView("issues-pane")},groups:[{iconName:"issue-text-icon",iconColor:"#1a73e8"}]},this._throttler=new Common.Throttler.Throttler(100),SDK.ConsoleModel.ConsoleModel.instance().addEventListener(SDK.ConsoleModel.Events.ConsoleCleared,this._update,this),SDK.ConsoleModel.ConsoleModel.instance().addEventListener(SDK.ConsoleModel.Events.MessageAdded,this._update,this),SDK.ConsoleModel.ConsoleModel.instance().addEventListener(SDK.ConsoleModel.Events.MessageUpdated,this._update,this),BrowserSDK.IssuesManager.IssuesManager.instance().addEventListener(BrowserSDK.IssuesManager.Events.IssuesCountUpdated,this._update,this),this._update()}static instance(e={forceNew:null}){const{forceNew:o}=e;return warningErrorCounterInstance&&!o||(warningErrorCounterInstance=new WarningErrorCounter),warningErrorCounterInstance}_updatedForTest(){}_update(){this._updatingForTest=!0,this._throttler.schedule(this._updateThrottled.bind(this))}get titlesForTesting(){return this._consoleCounter.getAttribute("aria-label")}async _updateThrottled(){const e=SDK.ConsoleModel.ConsoleModel.instance().errors(),o=SDK.ConsoleModel.ConsoleModel.instance().warnings(),s=this._violationCounter?SDK.ConsoleModel.ConsoleModel.instance().violations():0,t=BrowserSDK.IssuesManager.IssuesManager.instance().numberOfIssues(),n=e=>0===e?void 0:`${e}`,i=i18nString(UIStrings.sErrors,{n:e}),r=i18nString(UIStrings.sWarnings,{n:o});this._consoleCounter.setTexts([n(e),n(o)]);let a="";e&&o?a=`${i}, ${r}`:e?a=i:o&&(a=r);const l=i18nString(UIStrings.openConsoleToViewS,{PH1:a});if(UI.Tooltip.Tooltip.install(this._consoleCounter,l),UI.ARIAUtils.setAccessibleName(this._consoleCounter,l),this._consoleCounter.classList.toggle("hidden",!(e||o)),this._violationCounter){this._violationCounter.setTexts([n(s)]);const e=i18nString(UIStrings.openLighthouseToView,{n:s});UI.Tooltip.Tooltip.install(this._violationCounter,e),UI.ARIAUtils.setAccessibleName(this._violationCounter,e),this._violationCounter.classList.toggle("hidden",!s)}this._issuesCounter.setTexts([n(t)]);const u=i18nString(UIStrings.openIssuesToView,{n:t});UI.Tooltip.Tooltip.install(this._issuesCounter,u),UI.ARIAUtils.setAccessibleName(this._issuesCounter,u),this._issuesCounter.classList.toggle("hidden",!t),this._toolbarItem.setVisible(Boolean(e||o||s||t)),UI.InspectorView.InspectorView.instance().toolbarItemResized(),this._updatingForTest=!1,this._updatedForTest()}item(){return this._toolbarItem}}WarningErrorCounter._instanceForTest=null;