import*as Common from"../../../../core/common/common.js";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 Logs from"../../../../models/logs/logs.js";import*as Buttons from"../../../../ui/components/buttons/buttons.js";import*as ComponentHelpers from"../../../../ui/components/helpers/helpers.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 RequestLinkIcon from"../../../../ui/components/request_link_icon/request_link_icon.js";import*as UI from"../../../../ui/legacy/legacy.js";import*as LitHtml from"../../../../ui/lit-html/lit-html.js";import*as PreloadingHelper from"../helper/helper.js";import preloadingDetailsReportViewStyles from"./preloadingDetailsReportView.css.js";import{prefetchFailureReason,prerenderFailureReason,ruleSetLocationShort}from"./PreloadingString.js";const UIStrings={selectAnElementForMoreDetails:"Select an element for more details",detailsDetailedInformation:"Detailed information",detailsAction:"Action",detailsStatus:"Status",detailsFailureReason:"Failure reason",detailsRuleSet:"Rule set",detailedStatusNotTriggered:"Preloading attempt is not yet triggered.",detailedStatusPending:"Preloading attempt is eligible but pending.",detailedStatusRunning:"Preloading is running.",detailedStatusReady:"Preloading finished and the result is ready for the next navigation.",detailedStatusSuccess:"Preloading finished and used for a navigation.",detailedStatusFailure:"Preloading failed.",buttonInspect:"Inspect",buttonClickToInspect:"Click to inspect prerendered page",buttonClickToRevealRuleSet:"Click to reveal rule set"},str_=i18n.i18n.registerUIStrings("panels/application/preloading/components/PreloadingDetailsReportView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);class PreloadingUIUtils{static action({key:e}){switch(e.action){case"Prefetch":return i18n.i18n.lockedString("prefetch");case"Prerender":return i18n.i18n.lockedString("prerender")}}static detailedStatus({status:e}){switch(e){case"NotTriggered":return i18nString(UIStrings.detailedStatusNotTriggered);case"Pending":return i18nString(UIStrings.detailedStatusPending);case"Running":return i18nString(UIStrings.detailedStatusRunning);case"Ready":return i18nString(UIStrings.detailedStatusReady);case"Success":return i18nString(UIStrings.detailedStatusSuccess);case"Failure":return i18nString(UIStrings.detailedStatusFailure);case"NotSupported":return i18n.i18n.lockedString("Internal error")}}}const coordinator=Coordinator.RenderCoordinator.RenderCoordinator.instance();export class PreloadingDetailsReportView extends LegacyWrapper.LegacyWrapper.WrappableComponent{static litTagName=LitHtml.literal`devtools-resources-preloading-details-report-view`;#e=this.attachShadow({mode:"open"});#t=null;connectedCallback(){this.#e.adoptedStyleSheets=[preloadingDetailsReportViewStyles]}set data(e){this.#t=e,this.#i()}async#i(){await coordinator.write("PreloadingDetailsReportView render",(()=>{if(null===this.#t)return void LitHtml.render(LitHtml.html`
          <div class="preloading-noselected">
            <div>
              <p>${i18nString(UIStrings.selectAnElementForMoreDetails)}</p>
            </div>
          </div>
        `,this.#e,{host:this});const e=PreloadingUIUtils.detailedStatus(this.#t.preloadingAttempt),t=this.#t.pageURL;LitHtml.render(LitHtml.html`
        <${ReportView.ReportView.Report.litTagName} .data=${{reportTitle:"Preloading Attempt"}}>
          <${ReportView.ReportView.ReportSectionHeader.litTagName}>${i18nString(UIStrings.detailsDetailedInformation)}</${ReportView.ReportView.ReportSectionHeader.litTagName}>

          ${this.#r()}
          ${this.#a()}

          <${ReportView.ReportView.ReportKey.litTagName}>${i18nString(UIStrings.detailsStatus)}</${ReportView.ReportView.ReportKey.litTagName}>
          <${ReportView.ReportView.ReportValue.litTagName}>
            ${e}
          </${ReportView.ReportView.ReportValue.litTagName}>

          ${this.#o()}
          ${this.#n()}

          ${this.#t.ruleSets.map((e=>this.#l(e,t)))}
        </${ReportView.ReportView.Report.litTagName}>
      `,this.#e,{host:this})}))}#r(){assertNotNullOrUndefined(this.#t);const e=this.#t.preloadingAttempt;let t;return t="Prefetch"===e.action&&void 0!==e.requestId?LitHtml.html`
          <${RequestLinkIcon.RequestLinkIcon.RequestLinkIcon.litTagName}
            .data=${{affectedRequest:{requestId:e.requestId,url:e.key.url},requestResolver:this.#t.requestResolver||new Logs.RequestResolver.RequestResolver,displayURL:!0,urlToDisplay:e.key.url}}
          >
          </${RequestLinkIcon.RequestLinkIcon.RequestLinkIcon.litTagName}>
      `:LitHtml.html`
          <div class="text-ellipsis" title=${e.key.url}>${e.key.url}</div>
      `,LitHtml.html`
        <${ReportView.ReportView.ReportKey.litTagName}>${i18n.i18n.lockedString("URL")}</${ReportView.ReportView.ReportKey.litTagName}>
        <${ReportView.ReportView.ReportValue.litTagName}>
          ${t}
        </${ReportView.ReportView.ReportValue.litTagName}>
    `}#a(){assertNotNullOrUndefined(this.#t);const e=this.#t.preloadingAttempt,t=PreloadingUIUtils.action(this.#t.preloadingAttempt);let i=LitHtml.nothing;return(()=>{if("Prerender"!==e.action)return;if(null===SDK.TargetManager.TargetManager.instance().primaryPageTarget())return;const t=SDK.TargetManager.TargetManager.instance().targets().find((t=>"prerender"===t.targetInfo()?.subtype&&t.inspectedURL()===e.key.url)),r=void 0===t;i=LitHtml.html`
          <${Buttons.Button.Button.litTagName}
            @click=${()=>{void 0!==t&&UI.Context.Context.instance().setFlavor(SDK.Target.Target,t)}}
            .title=${i18nString(UIStrings.buttonClickToInspect)}
            .size=${"SMALL"}
            .variant=${"secondary"}
            .disabled=${r}
          >
            ${i18nString(UIStrings.buttonInspect)}
          </${Buttons.Button.Button.litTagName}>
      `})(),LitHtml.html`
        <${ReportView.ReportView.ReportKey.litTagName}>${i18nString(UIStrings.detailsAction)}</${ReportView.ReportView.ReportKey.litTagName}>
        <${ReportView.ReportView.ReportValue.litTagName}>
          <div class="text-ellipsis" title="">
            ${t}
            ${i}
          </div>
        </${ReportView.ReportView.ReportValue.litTagName}>
    `}#o(){assertNotNullOrUndefined(this.#t);const e=this.#t.preloadingAttempt;if("Prefetch"!==e.action)return LitHtml.nothing;const t=prefetchFailureReason(e);return null===t?LitHtml.nothing:LitHtml.html`
        <${ReportView.ReportView.ReportKey.litTagName}>${i18nString(UIStrings.detailsFailureReason)}</${ReportView.ReportView.ReportKey.litTagName}>
        <${ReportView.ReportView.ReportValue.litTagName}>
          ${t}
        </${ReportView.ReportView.ReportValue.litTagName}>
    `}#n(){assertNotNullOrUndefined(this.#t);const e=this.#t.preloadingAttempt;if("Prerender"!==e.action)return LitHtml.nothing;const t=prerenderFailureReason(e);return null===t?LitHtml.nothing:LitHtml.html`
        <${ReportView.ReportView.ReportKey.litTagName}>${i18nString(UIStrings.detailsFailureReason)}</${ReportView.ReportView.ReportKey.litTagName}>
        <${ReportView.ReportView.ReportValue.litTagName}>
          ${t}
        </${ReportView.ReportView.ReportValue.litTagName}>
    `}#l(e,t){const i=ruleSetLocationShort(e,t);return LitHtml.html`
      <${ReportView.ReportView.ReportKey.litTagName}>${i18nString(UIStrings.detailsRuleSet)}</${ReportView.ReportView.ReportKey.litTagName}>
      <${ReportView.ReportView.ReportValue.litTagName}>
        <div class="text-ellipsis" title="">
          <button class="link" role="link"
            @click=${()=>{Common.Revealer.reveal(new PreloadingHelper.PreloadingForward.RuleSetView(e.id))}}
            title=${i18nString(UIStrings.buttonClickToRevealRuleSet)}
            style=${LitHtml.Directives.styleMap({color:"var(--color-link)","text-decoration":"underline"})}
          >
            ${i}
          </button>
        </div>
      </${ReportView.ReportView.ReportValue.litTagName}>
    `}}ComponentHelpers.CustomElements.defineComponent("devtools-resources-preloading-details-report-view",PreloadingDetailsReportView);