import*as i18n from"../../../../core/i18n/i18n.js";import{assertNotNullOrUndefined}from"../../../../core/platform/platform.js";import*as SDK from"../../../../core/sdk/sdk.js";import*as ComponentHelpers from"../../../../ui/components/helpers/helpers.js";import*as IconButton from"../../../../ui/components/icon_button/icon_button.js";import*as LegacyWrapper from"../../../../ui/components/legacy_wrapper/legacy_wrapper.js";import*as Coordinator from"../../../../ui/components/render_coordinator/render_coordinator.js";import*as ReportView from"../../../../ui/components/report_view/report_view.js";import*as UI from"../../../../ui/legacy/legacy.js";import*as LitHtml from"../../../../ui/lit-html/lit-html.js";import usedPreloadingStyles from"./usedPreloadingView.css.js";import{prefetchFailureReason,prerenderFailureReason}from"./PreloadingString.js";import*as MismatchedPreloadingGrid from"./MismatchedPreloadingGrid.js";const UIStrings={preloadingStatus:"Preloading status",detailsFailureReason:"Failure reason",downgradedPrefetchUsed:"The initiating page attempted to prerender this page's URL. The prerender failed, but the resulting response body was still used as a prefetch.",prefetchUsed:"This page was successfully prefetched.",prerenderUsed:"This page was successfully prerendered.",prefetchFailed:"The initiating page attempted to prefetch this page's URL, but the prefetch failed, so a full navigation was performed instead.",prerenderFailed:"The initiating page attempted to prerender this page's URL, but the prerender failed, so a full navigation was performed instead.",noPreloads:"The initiating page did not attempt to preload this page's URL.",currentURL:"Current URL",preloadedURLs:"URLs being preloaded by the initiating page",learnMore:"Learn more: Preloading on developer.chrome.com"},str_=i18n.i18n.registerUIStrings("panels/application/preloading/components/UsedPreloadingView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_),coordinator=Coordinator.RenderCoordinator.RenderCoordinator.instance();export var UsedKind;!function(e){e.DowngradedPrerenderToPrefetchAndUsed="DowngradedPrerenderToPrefetchAndUsed",e.PrefetchUsed="PrefetchUsed",e.PrerenderUsed="PrerenderUsed",e.PrefetchFailed="PrefetchFailed",e.PrerenderFailed="PrerenderFailed",e.NoPreloads="NoPreloads"}(UsedKind||(UsedKind={}));export class UsedPreloadingView extends LegacyWrapper.LegacyWrapper.WrappableComponent{static litTagName=LitHtml.literal`devtools-resources-used-preloading-view`;#e=this.attachShadow({mode:"open"});#t={pageURL:"",attempts:[]};connectedCallback(){this.#e.adoptedStyleSheets=[usedPreloadingStyles]}set data(e){this.#t=e,this.#r()}async#r(){await coordinator.write("UsedPreloadingView render",(()=>{LitHtml.render(this.#i(),this.#e,{host:this})}))}#i(){const e=this.#t.attempts.filter((e=>e.key.url===this.#t.pageURL)),t=e.filter((e=>"Prefetch"===e.key.action))[0],r=e.filter((e=>"Prerender"===e.key.action))[0];let i,a,o=UsedKind.NoPreloads;switch(o="Failure"===r?.status&&"Success"===t?.status?UsedKind.DowngradedPrerenderToPrefetchAndUsed:"Success"===t?.status?UsedKind.PrefetchUsed:"Success"===r?.status?UsedKind.PrerenderUsed:"Failure"===t?.status?UsedKind.PrefetchFailed:"Failure"===r?.status?UsedKind.PrerenderFailed:UsedKind.NoPreloads,o){case UsedKind.DowngradedPrerenderToPrefetchAndUsed:i=LitHtml.html`${i18nString(UIStrings.downgradedPrefetchUsed)}`;break;case UsedKind.PrefetchUsed:i=LitHtml.html`${i18nString(UIStrings.prefetchUsed)}`;break;case UsedKind.PrerenderUsed:i=LitHtml.html`${i18nString(UIStrings.prerenderUsed)}`;break;case UsedKind.PrefetchFailed:i=LitHtml.html`${i18nString(UIStrings.prefetchFailed)}`;break;case UsedKind.PrerenderFailed:i=LitHtml.html`${i18nString(UIStrings.prerenderFailed)}`;break;case UsedKind.NoPreloads:i=LitHtml.html`
          <${IconButton.Icon.Icon.litTagName}
            .data=${{iconName:"clear",color:"var(--icon-default)",width:"16px"}}
          >
          </${IconButton.Icon.Icon.litTagName}>
          ${i18nString(UIStrings.noPreloads)}
        `}o===UsedKind.PrefetchFailed?(assertNotNullOrUndefined(t),a=prefetchFailureReason(t)):o!==UsedKind.PrerenderFailed&&o!==UsedKind.DowngradedPrerenderToPrefetchAndUsed||(assertNotNullOrUndefined(r),a=prerenderFailureReason(r));let n=LitHtml.nothing;return void 0!==a&&(n=LitHtml.html`
        <${ReportView.ReportView.ReportSection.litTagName}>
          <${ReportView.ReportView.ReportKey.litTagName}>${i18nString(UIStrings.detailsFailureReason)}</${ReportView.ReportView.ReportKey.litTagName}>
          <${ReportView.ReportView.ReportValue.litTagName}>
            ${a}
          </${ReportView.ReportView.ReportValue.litTagName}>
        </${ReportView.ReportView.ReportSection.litTagName}>
      `),LitHtml.html`
      <${ReportView.ReportView.Report.litTagName}>
        <${ReportView.ReportView.ReportSectionHeader.litTagName}>${i18nString(UIStrings.preloadingStatus)}</${ReportView.ReportView.ReportSectionHeader.litTagName}>
        <${ReportView.ReportView.ReportSection.litTagName}>
          ${i}
        </${ReportView.ReportView.ReportSection.litTagName}>

        ${n}

        ${this.#a(o)}

        <${ReportView.ReportView.ReportSection.litTagName}>
          ${UI.XLink.XLink.create("https://developer.chrome.com/blog/prerender-pages/",i18nString(UIStrings.learnMore),"link")}
        </${ReportView.ReportView.ReportSection.litTagName}>
      </${ReportView.ReportView.Report.litTagName}>
    `}#a(e){if(e!==UsedKind.NoPreloads||0===this.#t.attempts.length)return LitHtml.nothing;const t=this.#t.attempts.map((e=>({url:e.key.url,action:e.key.action,status:e.status}))),r={pageURL:this.#t.pageURL,rows:t};return LitHtml.html`
      <${ReportView.ReportView.ReportSectionHeader.litTagName}>${i18nString(UIStrings.currentURL)}</${ReportView.ReportView.ReportSectionHeader.litTagName}>
      <${ReportView.ReportView.ReportSection.litTagName}>
        ${UI.XLink.XLink.create(this.#t.pageURL)}
      </${ReportView.ReportView.ReportSection.litTagName}>

      <${ReportView.ReportView.ReportSectionHeader.litTagName}>${i18nString(UIStrings.preloadedURLs)}</${ReportView.ReportView.ReportSectionHeader.litTagName}>
      <${ReportView.ReportView.ReportSection.litTagName}>
        <${MismatchedPreloadingGrid.MismatchedPreloadingGrid.litTagName}
          .data=${r}></${MismatchedPreloadingGrid.MismatchedPreloadingGrid.litTagName}>
      </${ReportView.ReportView.ReportSection.litTagName}>
    `}}ComponentHelpers.CustomElements.defineComponent("devtools-resources-used-preloading-view",UsedPreloadingView);