import"../ui/components/components.js";import*as LitHtml from"../third_party/lit-html/lit-html.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";import*as i18n from"../i18n/i18n.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.",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("network/RequestTrustTokensView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class RequestTrustTokensView extends UI.Widget.VBox{constructor(e){super(),this.reportView=new RequestTrustTokensReport,this.request=e,this.contentElement.appendChild(this.reportView)}wasShown(){this.request.addEventListener(SDK.NetworkRequest.Events.TrustTokenResultAdded,this.refreshReportView,this),this.refreshReportView()}willHide(){this.request.removeEventListener(SDK.NetworkRequest.Events.TrustTokenResultAdded,this.refreshReportView,this)}refreshReportView(){this.reportView.data={params:this.request.trustTokenParams(),result:this.request.trustTokenOperationDoneEvent()}}}export class RequestTrustTokensReport extends HTMLElement{constructor(){super(...arguments),this.shadow=this.attachShadow({mode:"open"})}set data(e){this.trustTokenData=e,this.render()}render(){if(!this.trustTokenData)throw new Error("Trying to render a Trust Token report without providing data");LitHtml.render(LitHtml.html`
      <style>
        .code {
          font-family: var(--monospace-font-family);
          font-size: var(--monospace-font-size);
        }

        .issuers-list {
          display: flex;
          flex-direction: column;
          list-style-type: none;
          padding: 0;
          margin: 0;
        }

        .status-icon {
          margin: 0 0.3em 2px 0;
          vertical-align: middle;
        }
      </style>
      <devtools-report>
        ${this.renderParameterSection()}
        ${this.renderResultSection()}
      </devtools-report>
    `,this.shadow)}renderParameterSection(){return this.trustTokenData&&this.trustTokenData.params?LitHtml.html`
      <devtools-report-section-header>${i18nString(UIStrings.parameters)}</devtools-report-section-header>
      ${renderRowWithCodeValue(i18nString(UIStrings.type),this.trustTokenData.params.type.toString())}
      ${this.renderRefreshPolicy(this.trustTokenData.params)}
      ${this.renderIssuers(this.trustTokenData.params)}
      ${this.renderIssuerAndTopLevelOriginFromResult()}
      <devtools-report-divider></devtools-report-divider>
    `:LitHtml.nothing}renderRefreshPolicy(e){return e.type!==Protocol.Network.TrustTokenOperationType.Redemption?LitHtml.nothing:renderRowWithCodeValue(i18nString(UIStrings.refreshPolicy),e.refreshPolicy.toString())}renderIssuers(e){return e.issuers&&0!==e.issuers.length?LitHtml.html`
      <devtools-report-key>${i18nString(UIStrings.issuers)}</devtools-report-key>
      <devtools-report-value>
        <ul class="issuers-list">
          ${e.issuers.map((e=>LitHtml.html`<li>${e}</li>`))}
        </ul>
      </devtools-report-value>
    `:LitHtml.nothing}renderIssuerAndTopLevelOriginFromResult(){return this.trustTokenData&&this.trustTokenData.result?LitHtml.html`
      ${renderSimpleRowIfValuePresent(i18nString(UIStrings.topLevelOrigin),this.trustTokenData.result.topLevelOrigin)}
      ${renderSimpleRowIfValuePresent(i18nString(UIStrings.issuer),this.trustTokenData.result.issuerOrigin)}`:LitHtml.nothing}renderResultSection(){return this.trustTokenData&&this.trustTokenData.result?LitHtml.html`
      <devtools-report-section-header>${i18nString(UIStrings.result)}</devtools-report-section-header>
      <devtools-report-key>${i18nString(UIStrings.status)}</devtools-report-key>
      <devtools-report-value>
        <span>
          <devtools-icon class="status-icon"
            .data=${getIconForStatusCode(this.trustTokenData.result.status)}>
          </devtools-icon>
          <strong>${getSimplifiedStatusTextForStatusCode(this.trustTokenData.result.status)}</strong>
          ${getDetailedTextForStatusCode(this.trustTokenData.result.status)}
        </span>
      </devtools-report-value>
      ${this.renderIssuedTokenCount(this.trustTokenData.result)}
      <devtools-report-divider></devtools-report-divider>
      `:LitHtml.nothing}renderIssuedTokenCount(e){return e.type!==Protocol.Network.TrustTokenOperationType.Issuance?LitHtml.nothing:renderSimpleRowIfValuePresent(i18nString(UIStrings.numberOfIssuedTokens),e.issuedTokenCount)}}const SUCCESS_ICON_DATA={color:"rgb(12, 164, 12)",iconName:"ic_checkmark_16x16",width:"12px"},FAILURE_ICON_DATA={color:"",iconName:"error_icon",width:"12px"};export function statusConsideredSuccess(e){return e===Protocol.Network.TrustTokenOperationDoneEventStatus.Ok||e===Protocol.Network.TrustTokenOperationDoneEventStatus.AlreadyExists||e===Protocol.Network.TrustTokenOperationDoneEventStatus.FulfilledLocally}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 Protocol.Network.TrustTokenOperationDoneEventStatus.Ok:return null;case Protocol.Network.TrustTokenOperationDoneEventStatus.AlreadyExists:return i18nString(UIStrings.theOperationsResultWasServedFrom);case Protocol.Network.TrustTokenOperationDoneEventStatus.FulfilledLocally:return i18nString(UIStrings.theOperationWasFulfilledLocally);case Protocol.Network.TrustTokenOperationDoneEventStatus.InvalidArgument:return i18nString(UIStrings.aClientprovidedArgumentWas);case Protocol.Network.TrustTokenOperationDoneEventStatus.ResourceExhausted:return i18nString(UIStrings.eitherNoInputsForThisOperation);case Protocol.Network.TrustTokenOperationDoneEventStatus.BadResponse:return i18nString(UIStrings.theServersResponseWasMalformedOr);case Protocol.Network.TrustTokenOperationDoneEventStatus.FailedPrecondition:case Protocol.Network.TrustTokenOperationDoneEventStatus.Unavailable:case Protocol.Network.TrustTokenOperationDoneEventStatus.InternalError:case Protocol.Network.TrustTokenOperationDoneEventStatus.UnknownError:return i18nString(UIStrings.theOperationFailedForAnUnknown)}}function renderSimpleRowIfValuePresent(e,t){return void 0===t?LitHtml.nothing:LitHtml.html`
    <devtools-report-key>${e}</devtools-report-key>
    <devtools-report-value>${t}</devtools-report-value>
  `}function renderRowWithCodeValue(e,t){return LitHtml.html`
    <devtools-report-key>${e}</devtools-report-key>
    <devtools-report-value class="code">${t}</devtools-report-value>
  `}customElements.define("devtools-trust-token-report",RequestTrustTokensReport);