import{assertNotNullOrUndefined}from"../../../core/platform/platform.js";import*as Platform from"../../../core/platform/platform.js";import*as SplitView from"../../../ui/components/split_view/split_view.js";import*as i18n from"../../../core/i18n/i18n.js";import*as UI from"../../../ui/legacy/legacy.js";import*as SDK from"../../../core/sdk/sdk.js";import*as LitHtml from"../../../ui/lit-html/lit-html.js";import*as Bindings from"../../../models/bindings/bindings.js";import*as PreloadingComponents from"./components/components.js";import emptyWidgetStyles from"../../../ui/legacy/emptyWidget.css.js";import preloadingViewStyles from"./preloadingView.css.js";const UIStrings={filterFilterByRuleSet:"Filter by rule set",filterAllPreloads:"All preloads",validityValid:"Valid",validityInvalid:"Invalid",validitySomeRulesInvalid:"Some rules invalid",statusNotTriggered:"Not triggered",statusPending:"Pending",statusRunning:"Running",statusReady:"Ready",statusSuccess:"Success",statusFailure:"Failure"},str_=i18n.i18n.registerUIStrings("panels/application/preloading/PreloadingView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);class PreloadingUIUtils{static status(e){switch(e){case"NotTriggered":return i18nString(UIStrings.statusNotTriggered);case"Pending":return i18nString(UIStrings.statusPending);case"Running":return i18nString(UIStrings.statusRunning);case"Ready":return i18nString(UIStrings.statusReady);case"Success":return i18nString(UIStrings.statusSuccess);case"Failure":return i18nString(UIStrings.statusFailure);case"NotSupported":return i18n.i18n.lockedString("Internal error")}}static preloadsStatusSummary(e){return["NotTriggered","Pending","Running","Ready","Success","Failure"].filter((t=>(e?.get(t)||0)>0)).map((t=>(e?.get(t)||0)+" "+this.status(t))).join(", ")}static validity({errorType:e}){switch(e){case void 0:return i18nString(UIStrings.validityValid);case"SourceIsNotJsonObject":return i18nString(UIStrings.validityInvalid);case"InvalidRulesSkipped":return i18nString(UIStrings.validitySomeRulesInvalid)}}static location(e){if(void 0!==e.backendNodeId)return i18n.i18n.lockedString("<script>");if(void 0!==e.url)return e.url;throw Error("unreachable")}static processLocalId(e){const t=e.indexOf(".");return-1===t?e:e.slice(t+1)}static ruleSetLocationShort(e,t){const i=void 0===e.url?t:e.url;return Bindings.ResourceUtils.displayNameForURL(i)}}function pageURL(){return SDK.TargetManager.TargetManager.instance().scopeTarget()?.inspectedURL()||""}export class PreloadingRuleSetView extends UI.Widget.VBox{model;focusedRuleSetId=null;focusedPreloadingAttemptId=null;warningsContainer;warningsView=new PreloadingWarningsView;hsplit;ruleSetGrid=new PreloadingComponents.RuleSetGrid.RuleSetGrid;ruleSetDetails=new PreloadingComponents.RuleSetDetailsView.RuleSetDetailsView;constructor(e){super(!0,!1),this.model=e,SDK.TargetManager.TargetManager.instance().addScopeChangeListener(this.onScopeChange.bind(this)),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.PreloadingModel.PreloadingModel,SDK.PreloadingModel.Events.ModelUpdated,this.render,this,{scoped:!0}),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.PreloadingModel.PreloadingModel,SDK.PreloadingModel.Events.WarningsUpdated,this.warningsView.onWarningsUpdated,this.warningsView,{scoped:!0}),this.warningsContainer=document.createElement("div"),this.warningsContainer.classList.add("flex-none"),this.contentElement.insertBefore(this.warningsContainer,this.contentElement.firstChild),this.warningsView.show(this.warningsContainer),this.ruleSetGrid.addEventListener("cellfocused",this.onRuleSetsGridCellFocused.bind(this)),LitHtml.render(LitHtml.html`
        <${SplitView.SplitView.SplitView.litTagName} .horizontal=${!0} style="--min-sidebar-size: 0px">
          <div slot="main" class="overflow-auto" style="height: 100%">
            ${this.ruleSetGrid}
          </div>
          <div slot="sidebar" class="overflow-auto" style="height: 100%">
            ${this.ruleSetDetails}
          </div>
        </${SplitView.SplitView.SplitView.litTagName}>`,this.contentElement,{host:this}),this.hsplit=this.contentElement.querySelector("devtools-split-view")}wasShown(){super.wasShown(),this.registerCSSFiles([emptyWidgetStyles,preloadingViewStyles]),this.warningsView.wasShown(),this.render()}onScopeChange(){const e=SDK.TargetManager.TargetManager.instance().scopeTarget()?.model(SDK.PreloadingModel.PreloadingModel);assertNotNullOrUndefined(e),this.model=e,this.render()}revealRuleSet(e){this.focusedRuleSetId=e.ruleSetId,this.render()}updateRuleSetDetails(){const e=this.focusedRuleSetId,t=null===e?null:this.model.getRuleSetById(e);this.ruleSetDetails.data=t,null===t?this.hsplit.style.setProperty("--current-main-area-size","100%"):this.hsplit.style.setProperty("--current-main-area-size","60%")}render(){const e=this.model.getPreloadCountsByRuleSetId(),t=this.model.getAllRuleSets().map((({id:t,value:i})=>{const n=e.get(t)||new Map;return{ruleSet:i,preloadsStatusSummary:PreloadingUIUtils.preloadsStatusSummary(n)}}));this.ruleSetGrid.update({rows:t,pageURL:pageURL()}),this.updateRuleSetDetails()}onRuleSetsGridCellFocused(e){const t=e;this.focusedRuleSetId=t.data.row.cells.find((e=>"id"===e.columnId))?.value,this.render()}getInfobarContainerForTest(){return this.warningsView.contentElement}getRuleSetGridForTest(){return this.ruleSetGrid}getRuleSetDetailsForTest(){return this.ruleSetDetails}}export class PreloadingAttemptView extends UI.Widget.VBox{model;focusedPreloadingAttemptId=null;warningsContainer;warningsView=new PreloadingWarningsView;preloadingGrid=new PreloadingComponents.PreloadingGrid.PreloadingGrid;preloadingDetails=new PreloadingComponents.PreloadingDetailsReportView.PreloadingDetailsReportView;ruleSetSelector;constructor(e){super(!0,!1),this.model=e,SDK.TargetManager.TargetManager.instance().addScopeChangeListener(this.onScopeChange.bind(this)),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.PreloadingModel.PreloadingModel,SDK.PreloadingModel.Events.ModelUpdated,this.render,this,{scoped:!0}),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.PreloadingModel.PreloadingModel,SDK.PreloadingModel.Events.WarningsUpdated,this.warningsView.onWarningsUpdated,this.warningsView,{scoped:!0}),this.warningsContainer=document.createElement("div"),this.warningsContainer.classList.add("flex-none"),this.contentElement.insertBefore(this.warningsContainer,this.contentElement.firstChild),this.warningsView.show(this.warningsContainer);const t=new UI.Widget.VBox,i=new UI.Toolbar.Toolbar("preloading-toolbar",t.contentElement);this.ruleSetSelector=new PreloadingRuleSetSelector((()=>this.render())),i.appendToolbarItem(this.ruleSetSelector.item()),this.preloadingGrid.addEventListener("cellfocused",this.onPreloadingGridCellFocused.bind(this)),LitHtml.render(LitHtml.html`
        <${SplitView.SplitView.SplitView.litTagName} .horizontal=${!0} style="--min-sidebar-size: 0px">
          <div slot="main" class="overflow-auto" style="height: 100%">
            ${this.preloadingGrid}
          </div>
          <div slot="sidebar" class="overflow-auto" style="height: 100%">
            ${this.preloadingDetails}
          </div>
        </${SplitView.SplitView.SplitView.litTagName}>`,t.contentElement,{host:this}),t.show(this.contentElement)}wasShown(){super.wasShown(),this.registerCSSFiles([emptyWidgetStyles,preloadingViewStyles]),this.warningsView.wasShown(),this.render()}onScopeChange(){const e=SDK.TargetManager.TargetManager.instance().scopeTarget()?.model(SDK.PreloadingModel.PreloadingModel);assertNotNullOrUndefined(e),this.model=e,this.render()}setFilter(e){const t=e.ruleSetId;this.model.getRuleSetById(t)&&this.ruleSetSelector.select(t)}updatePreloadingDetails(){const e=this.focusedPreloadingAttemptId,t=null===e?null:this.model.getPreloadingAttemptById(e);if(null===t)this.preloadingDetails.data=null;else{const e=t.ruleSetIds.map((e=>this.model.getRuleSetById(e))).filter((e=>null!==e));this.preloadingDetails.data={preloadingAttempt:t,ruleSets:e,pageURL:pageURL()}}}render(){const e=this.ruleSetSelector.getSelected(),t=this.model.getPreloadingAttempts(e).map((({id:e,value:t})=>{const i=t,n=i.ruleSetIds.flatMap((e=>{const t=this.model.getRuleSetById(e);return null===t?[]:[t]}));return{id:e,attempt:i,ruleSets:n}}));this.preloadingGrid.update({rows:t,pageURL:pageURL()}),this.updatePreloadingDetails()}onPreloadingGridCellFocused(e){const t=e;this.focusedPreloadingAttemptId=t.data.row.cells.find((e=>"id"===e.columnId))?.value,this.render()}getRuleSetSelectorToolbarItemForTest(){return this.ruleSetSelector.item()}getPreloadingGridForTest(){return this.preloadingGrid}getPreloadingDetailsForTest(){return this.preloadingDetails}selectRuleSetOnFilterForTest(e){this.ruleSetSelector.select(e)}}export class PreloadingResultView extends UI.Widget.VBox{model;warningsContainer;warningsView=new PreloadingWarningsView;usedPreloading=new PreloadingComponents.UsedPreloadingView.UsedPreloadingView;constructor(e){super(!0,!1),this.model=e,SDK.TargetManager.TargetManager.instance().addScopeChangeListener(this.onScopeChange.bind(this)),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.PreloadingModel.PreloadingModel,SDK.PreloadingModel.Events.ModelUpdated,this.render,this,{scoped:!0}),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.PreloadingModel.PreloadingModel,SDK.PreloadingModel.Events.WarningsUpdated,this.warningsView.onWarningsUpdated,this.warningsView,{scoped:!0}),this.warningsContainer=document.createElement("div"),this.warningsContainer.classList.add("flex-none"),this.contentElement.insertBefore(this.warningsContainer,this.contentElement.firstChild),this.warningsView.show(this.warningsContainer);const t=new UI.Widget.VBox;t.contentElement.appendChild(this.usedPreloading),t.show(this.contentElement)}wasShown(){super.wasShown(),this.registerCSSFiles([emptyWidgetStyles,preloadingViewStyles]),this.warningsView.wasShown(),this.render()}onScopeChange(){const e=SDK.TargetManager.TargetManager.instance().scopeTarget()?.model(SDK.PreloadingModel.PreloadingModel);assertNotNullOrUndefined(e),this.model=e,this.render()}render(){this.usedPreloading.data={pageURL:SDK.TargetManager.TargetManager.instance().scopeTarget()?.inspectedURL()||"",attempts:this.model.getPreloadingAttemptsOfPreviousPage().map((({value:e})=>e))}}getUsedPreloadingForTest(){return this.usedPreloading}}class PreloadingRuleSetSelector{model;onSelectionChanged=()=>{};toolbarItem;listModel;dropDown;constructor(e){const t=SDK.TargetManager.TargetManager.instance().scopeTarget()?.model(SDK.PreloadingModel.PreloadingModel);assertNotNullOrUndefined(t),this.model=t,SDK.TargetManager.TargetManager.instance().addScopeChangeListener(this.onScopeChange.bind(this)),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.PreloadingModel.PreloadingModel,SDK.PreloadingModel.Events.ModelUpdated,this.onModelUpdated,this,{scoped:!0}),this.listModel=new UI.ListModel.ListModel,this.dropDown=new UI.SoftDropDown.SoftDropDown(this.listModel,this),this.dropDown.setRowHeight(36),this.dropDown.setPlaceholderText(i18nString(UIStrings.filterAllPreloads)),this.toolbarItem=new UI.Toolbar.ToolbarItem(this.dropDown.element),this.toolbarItem.setTitle(i18nString(UIStrings.filterFilterByRuleSet)),this.toolbarItem.element.classList.add("toolbar-has-dropdown"),this.onModelUpdated(),this.onSelectionChanged=e}onScopeChange(){const e=SDK.TargetManager.TargetManager.instance().scopeTarget()?.model(SDK.PreloadingModel.PreloadingModel);assertNotNullOrUndefined(e),this.model=e,this.onModelUpdated()}onModelUpdated(){const e=[null,...this.model.getAllRuleSets().map((({id:e})=>e))],t=this.dropDown.getSelectedItem(),i=-1===e.indexOf(t)?null:t;this.listModel.replaceAll(e),this.dropDown.selectItem(i)}getSelected(){return this.dropDown.getSelectedItem()}select(e){this.dropDown.selectItem(e)}item(){return this.toolbarItem}titleFor(e){if(null===e)return i18nString(UIStrings.filterAllPreloads);const t=this.model.getRuleSetById(e);return null===t?i18n.i18n.lockedString("Internal error"):PreloadingUIUtils.ruleSetLocationShort(t,pageURL())}subtitleFor(e){const t=this.model.getPreloadCountsByRuleSetId().get(e)||new Map;return PreloadingUIUtils.preloadsStatusSummary(t)}createElementForItem(e){const t=document.createElement("div"),i=UI.Utils.createShadowRootWithCoreStyles(t,{cssFile:void 0,delegatesFocus:void 0}),n=i.createChild("div","title");UI.UIUtils.createTextChild(n,Platform.StringUtilities.trimEndWithMaxLength(this.titleFor(e),100));const r=i.createChild("div","subtitle");return UI.UIUtils.createTextChild(r,this.subtitleFor(e)),t}isItemSelectable(e){return!0}itemSelected(e){this.onSelectionChanged()}highlightedItemChanged(e,t,i,n){}}export class PreloadingWarningsView extends UI.Widget.VBox{infobar=new PreloadingComponents.PreloadingDisabledInfobar.PreloadingDisabledInfobar;constructor(){super(!1,!1)}wasShown(){super.wasShown(),this.registerCSSFiles([emptyWidgetStyles]),this.contentElement.append(this.infobar)}onWarningsUpdated(e){this.infobar.data=e.data}}