import*as Host from"../host/host.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";import{CSSOverviewCompletedView}from"./CSSOverviewCompletedView.js";import{Events,OverviewController}from"./CSSOverviewController.js";import{CSSOverviewModel}from"./CSSOverviewModel.js";import{CSSOverviewProcessingView}from"./CSSOverviewProcessingView.js";import{CSSOverviewStartView}from"./CSSOverviewStartView.js";let CSSOverviewPanelInstance;export class CSSOverviewPanel extends UI.Panel.Panel{constructor(){super("css_overview"),this.registerRequiredCSS("css_overview/cssOverview.css",{enableLegacyPatching:!1}),this.element.classList.add("css-overview-panel");const[e]=SDK.SDKModel.TargetManager.instance().models(CSSOverviewModel);this._model=e,this._controller=new OverviewController,this._startView=new CSSOverviewStartView(this._controller),this._processingView=new CSSOverviewProcessingView(this._controller),this._completedView=new CSSOverviewCompletedView(this._controller,e.target()),this._controller.addEventListener(Events.RequestOverviewStart,(e=>{Host.userMetrics.actionTaken(Host.UserMetrics.Action.CaptureCssOverviewClicked),this._startOverview()}),this),this._controller.addEventListener(Events.RequestOverviewCancel,this._cancelOverview,this),this._controller.addEventListener(Events.OverviewCompleted,this._overviewCompleted,this),this._controller.addEventListener(Events.Reset,this._reset,this),this._controller.addEventListener(Events.RequestNodeHighlight,this._requestNodeHighlight,this),this._reset()}static instance(){return CSSOverviewPanelInstance||(CSSOverviewPanelInstance=new CSSOverviewPanel),CSSOverviewPanelInstance}_reset(){this._backgroundColors=new Map,this._textColors=new Map,this._fillColors=new Map,this._borderColors=new Map,this._fontInfo=new Map,this._mediaQueries=new Map,this._unusedDeclarations=new Map,this._elementCount=0,this._cancelled=!1,this._globalStyleStats={styleRules:0,inlineStyles:0,externalSheets:0,stats:{type:0,class:0,id:0,universal:0,attribute:0,nonSimple:0}},this._renderInitialView()}_requestNodeHighlight(e){this._model.highlightNode(e.data)}_renderInitialView(){this._processingView.hideWidget(),this._completedView.hideWidget(),this._startView.show(this.contentElement)}_renderOverviewStartedView(){this._startView.hideWidget(),this._completedView.hideWidget(),this._processingView.show(this.contentElement)}_renderOverviewCompletedView(){this._startView.hideWidget(),this._processingView.hideWidget(),this._completedView.show(this.contentElement),this._completedView.setOverviewData({backgroundColors:this._backgroundColors,textColors:this._textColors,textColorContrastIssues:this._textColorContrastIssues,fillColors:this._fillColors,borderColors:this._borderColors,globalStyleStats:this._globalStyleStats,fontInfo:this._fontInfo,elementCount:this._elementCount,mediaQueries:this._mediaQueries,unusedDeclarations:this._unusedDeclarations})}async _startOverview(){this._renderOverviewStartedView();const[e,{elementCount:t,backgroundColors:s,textColors:i,textColorContrastIssues:r,fillColors:o,borderColors:l,fontInfo:n,unusedDeclarations:a},h]=await Promise.all([this._model.getGlobalStylesheetStats(),this._model.getNodeStyleStats(),this._model.getMediaQueries()]);t&&(this._elementCount=t),e&&(this._globalStyleStats=e),h&&(this._mediaQueries=h),s&&(this._backgroundColors=s),i&&(this._textColors=i),r&&(this._textColorContrastIssues=r),o&&(this._fillColors=o),l&&(this._borderColors=l),n&&(this._fontInfo=n),a&&(this._unusedDeclarations=a),this._controller.dispatchEventToListeners(Events.OverviewCompleted)}_getStyleValue(e,t){const s=e.filter((e=>e.name===t));if(s.length)return s[0].value}_cancelOverview(){this._cancelled=!0}_overviewCompleted(){this._renderOverviewCompletedView()}}