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