import*as i18n from"../../../core/i18n/i18n.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 ReportView from"../../../ui/components/report_view/report_view.js";import*as LitHtml from"../../../ui/lit-html/lit-html.js";import requestTrustTokensViewStyles from"./RequestTrustTokensView.css.js";const UIStrings={parameters:"Parameters",type:"Type",refreshPolicy:"Refresh policy",issuers:"Issuers",topLevelOrigin:"Top level origin",issuer:"Issuer",result:"Result",status:"Status",numberOfIssuedTokens:"Number of issued tokens",success:"Success",failure:"Failure",theOperationsResultWasServedFrom:"The operations result was served from cache.",theOperationWasFulfilledLocally:"The operation was fulfilled locally, no request was sent.",theKeysForThisPSTIssuerAreUnavailable:"The keys for this PST issuer are unavailable. The issuer may need to be registered via the Chrome registration process.",aClientprovidedArgumentWas:"A client-provided argument was malformed or otherwise invalid.",eitherNoInputsForThisOperation:"Either no inputs for this operation are available or the output exceeds the operations quota.",theServersResponseWasMalformedOr:"The servers response was malformed or otherwise invalid.",theOperationFailedForAnUnknown:"The operation failed for an unknown reason."},str_=i18n.i18n.registerUIStrings("panels/network/components/RequestTrustTokensView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class RequestTrustTokensView extends LegacyWrapper.LegacyWrapper.WrappableComponent{static litTagName=LitHtml.literal`devtools-trust-token-report`;#e=this.attachShadow({mode:"open"});#t;constructor(e){super(),this.#t=e}wasShown(){this.#t.addEventListener(SDK.NetworkRequest.Events.TrustTokenResultAdded,this.render,this),this.render()}willHide(){this.#t.removeEventListener(SDK.NetworkRequest.Events.TrustTokenResultAdded,this.render,this)}connectedCallback(){this.#e.adoptedStyleSheets=[requestTrustTokensViewStyles]}async render(){if(!this.#t)throw new Error("Trying to render a Trust Token report without providing data");LitHtml.render(LitHtml.html`<${ReportView.ReportView.Report.litTagName}>
        ${this.#r()}
        ${this.#i()}
      </${ReportView.ReportView.Report.litTagName}>
    `,this.#e,{host:this})}#r(){const e=this.#t.trustTokenParams();return e?LitHtml.html`
      <${ReportView.ReportView.ReportSectionHeader.litTagName}>${i18nString(UIStrings.parameters)}</${ReportView.ReportView.ReportSectionHeader.litTagName}>
      ${renderRowWithCodeValue(i18nString(UIStrings.type),e.operation.toString())}
      ${this.#s(e)}
      ${this.#o(e)}
      ${this.#n()}
      <${ReportView.ReportView.ReportSectionDivider.litTagName}></${ReportView.ReportView.ReportSectionDivider.litTagName}>
    `:LitHtml.nothing}#s(e){return"Redemption"!==e.operation?LitHtml.nothing:renderRowWithCodeValue(i18nString(UIStrings.refreshPolicy),e.refreshPolicy.toString())}#o(e){return e.issuers&&0!==e.issuers.length?LitHtml.html`
      <${ReportView.ReportView.ReportKey.litTagName}>${i18nString(UIStrings.issuers)}</${ReportView.ReportView.ReportKey.litTagName}>
      <${ReportView.ReportView.ReportValue.litTagName}>
        <ul class="issuers-list">
          ${e.issuers.map((e=>LitHtml.html`<li>${e}</li>`))}
        </ul>
      </${ReportView.ReportView.ReportValue.litTagName}>
    `:LitHtml.nothing}#n(){const e=this.#t.trustTokenOperationDoneEvent();return e?LitHtml.html`
      ${renderSimpleRowIfValuePresent(i18nString(UIStrings.topLevelOrigin),e.topLevelOrigin)}
      ${renderSimpleRowIfValuePresent(i18nString(UIStrings.issuer),e.issuerOrigin)}`:LitHtml.nothing}#i(){const e=this.#t.trustTokenOperationDoneEvent();return e?LitHtml.html`
      <${ReportView.ReportView.ReportSectionHeader.litTagName}>${i18nString(UIStrings.result)}</${ReportView.ReportView.ReportSectionHeader.litTagName}>
      <${ReportView.ReportView.ReportKey.litTagName}>${i18nString(UIStrings.status)}</${ReportView.ReportView.ReportKey.litTagName}>
      <${ReportView.ReportView.ReportValue.litTagName}>
        <span>
          <${IconButton.Icon.Icon.litTagName} class="status-icon"
            .data=${getIconForStatusCode(e.status)}>
          </${IconButton.Icon.Icon.litTagName}>
          <strong>${getSimplifiedStatusTextForStatusCode(e.status)}</strong>
          ${getDetailedTextForStatusCode(e.status)}
        </span>
      </${ReportView.ReportView.ReportValue.litTagName}>
      ${this.#a(e)}
      <${ReportView.ReportView.ReportSectionDivider.litTagName}></${ReportView.ReportView.ReportSectionDivider.litTagName}>
      `:LitHtml.nothing}#a(e){return"Issuance"!==e.type?LitHtml.nothing:renderSimpleRowIfValuePresent(i18nString(UIStrings.numberOfIssuedTokens),e.issuedTokenCount)}}const SUCCESS_ICON_DATA={color:"var(--icon-checkmark-green)",iconName:"check-circle",width:"16px",height:"16px"},FAILURE_ICON_DATA={color:"var(--icon-error)",iconName:"cross-circle-filled",width:"16px",height:"16px"};export function statusConsideredSuccess(e){return"Ok"===e||"AlreadyExists"===e||"FulfilledLocally"===e}function getIconForStatusCode(e){return statusConsideredSuccess(e)?SUCCESS_ICON_DATA:FAILURE_ICON_DATA}function getSimplifiedStatusTextForStatusCode(e){return statusConsideredSuccess(e)?i18nString(UIStrings.success):i18nString(UIStrings.failure)}function getDetailedTextForStatusCode(e){switch(e){case"Ok":return null;case"AlreadyExists":return i18nString(UIStrings.theOperationsResultWasServedFrom);case"FulfilledLocally":return i18nString(UIStrings.theOperationWasFulfilledLocally);case"InvalidArgument":return i18nString(UIStrings.aClientprovidedArgumentWas);case"ResourceExhausted":return i18nString(UIStrings.eitherNoInputsForThisOperation);case"BadResponse":return i18nString(UIStrings.theServersResponseWasMalformedOr);case"MissingIssuerKeys":return i18nString(UIStrings.theKeysForThisPSTIssuerAreUnavailable);case"FailedPrecondition":case"Unavailable":case"InternalError":case"Unauthorized":case"UnknownError":return i18nString(UIStrings.theOperationFailedForAnUnknown)}}function renderSimpleRowIfValuePresent(e,t){return void 0===t?LitHtml.nothing:LitHtml.html`
    <${ReportView.ReportView.ReportKey.litTagName}>${e}</${ReportView.ReportView.ReportKey.litTagName}>
    <${ReportView.ReportView.ReportValue.litTagName}>${t}</${ReportView.ReportView.ReportValue.litTagName}>
  `}function renderRowWithCodeValue(e,t){return LitHtml.html`
    <${ReportView.ReportView.ReportKey.litTagName}>${e}</${ReportView.ReportView.ReportKey.litTagName}>
    <${ReportView.ReportView.ReportValue.litTagName} class="code">${t}</${ReportView.ReportView.ReportValue.litTagName}>
  `}ComponentHelpers.CustomElements.defineComponent("devtools-trust-token-report",RequestTrustTokensView);