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