import{IssuesManager as t}from"../browser_sdk/browser_sdk.js";import{Console as e,Throttler as s}from"../common/common.js";import{userMetrics as i,UserMetrics as n}from"../host/host.js";import{ConsoleModel as o}from"../sdk/sdk.js";import{Toolbar as r,Utils as a,ViewManager as l,ARIAUtils as h}from"../ui/ui.js";class u{constructor(){u._instanceForTest=this;const h=createElement("div");this._toolbarItem=new r.ToolbarItem(h),this._counter=createElement("div"),this._counter.addEventListener("click",e.Console.instance().show.bind(e.Console.instance()),!1);const d=a.createShadowRootWithCoreStyles(this._counter,"console_counters/errorWarningCounter.css");h.appendChild(this._counter),this._violationCounter=createElement("div"),this._violationCounter.addEventListener("click",()=>{l.ViewManager.instance().showView("lighthouse")});const c=a.createShadowRootWithCoreStyles(this._violationCounter,"console_counters/errorWarningCounter.css");Root.Runtime.experiments.isEnabled("spotlight")&&h.appendChild(this._violationCounter),this._issuesCounter=createElement("div"),this._issuesCounter.addEventListener("click",()=>{i.issuesPanelOpenedFrom(n.IssueOpener.StatusBarIssuesCounter),l.ViewManager.instance().showView("issues-pane")});const _=a.createShadowRootWithCoreStyles(this._issuesCounter,"console_counters/errorWarningCounter.css");Root.Runtime.experiments.isEnabled("issuesPane")&&h.appendChild(this._issuesCounter),this._errors=this._createItem(d,"smallicon-error"),this._warnings=this._createItem(d,"smallicon-warning"),Root.Runtime.experiments.isEnabled("spotlight")&&(this._violations=this._createItem(c,"smallicon-info")),Root.Runtime.experiments.isEnabled("issuesPane")&&(this._issues=this._createItem(_,"smallicon-issue-blue-text")),this._titles="",this._errorCount=-1,this._warningCount=-1,this._violationCount=-1,this._issuesCount=-1,this._throttler=new s.Throttler(100),o.ConsoleModel.instance().addEventListener(o.Events.ConsoleCleared,this._update,this),o.ConsoleModel.instance().addEventListener(o.Events.MessageAdded,this._update,this),o.ConsoleModel.instance().addEventListener(o.Events.MessageUpdated,this._update,this),t.IssuesManager.instance().addEventListener(t.Events.IssuesCountUpdated,this._update,this),this._update()}_updatedForTest(){}_createItem(t,e){const s=document.createElement("span");s.classList.add("counter-item"),h.markAsHidden(s);const i=s.createChild("span","","dt-icon-label");i.type=e;const n=i.createChild("span");return t.appendChild(s),{item:s,text:n}}_updateItem(t,e,s){t.item.classList.toggle("hidden",!e),t.item.classList.toggle("counter-item-first",s),t.text.textContent=e}_update(){this._updatingForTest=!0,this._throttler.schedule(this._updateThrottled.bind(this))}_updateThrottled(){const e=o.ConsoleModel.instance().errors(),s=o.ConsoleModel.instance().warnings(),i=o.ConsoleModel.instance().violations(),n=t.IssuesManager.instance().numberOfIssues();if(e===this._errorCount&&s===this._warningCount&&i===this._violationCount&&n===this._issuesCount)return Promise.resolve();this._errorCount=e,this._warningCount=s,this._violationCount=i,this._issuesCount=n,this._counter.classList.toggle("hidden",!(e||s)),this._violationCounter.classList.toggle("hidden",!i),this._toolbarItem.setVisible(!!(e||s||i));let r="";r=1===e?ls`${e} error`:ls`${e} errors`,this._updateItem(this._errors,e,!1);let a="";if(a=1===s?ls`${s} warning`:ls`${s} warnings`,this._updateItem(this._warnings,s,!e),Root.Runtime.experiments.isEnabled("spotlight")){let t="";t=1===i?ls`${i} violation`:ls`${i} violations`,this._updateItem(this._violations,i,!0),this._violationCounter.title=t}if(Root.Runtime.experiments.isEnabled("issuesPane")){let t="";t=1===n?ls`Issues pertaining to ${n} operation detected.`:ls`Issues pertaining to ${n} operations detected.`,this._updateItem(this._issues,n,!0),this._issuesCounter.title=t}return this._titles="",e&&s?this._titles=ls`${r}, ${a}`:e?this._titles=r:s&&(this._titles=a),this._counter.title=this._titles,h.setAccessibleName(this._counter,this._titles),self.UI.inspectorView.toolbarItemResized(),this._updatingForTest=!1,this._updatedForTest(),Promise.resolve()}item(){return this._toolbarItem}}var d=Object.freeze({__proto__:null,WarningErrorCounter:u});export{d as WarningErrorCounter};
