import*as i18n from"../../../core/i18n/i18n.js";import*as Adorners from"../../../ui/components/adorners/adorners.js";import*as ComponentHelpers from"../../../ui/components/helpers/helpers.js";import*as TreeOutline from"../../../ui/components/tree_outline/tree_outline.js";import*as LitHtml from"../../../ui/lit-html/lit-html.js";import badgeStyles from"./badge.css.js";import originTrialTokenRowsStyles from"./originTrialTokenRows.css.js";const UIStrings={origin:"Origin",trialName:"Trial Name",expiryTime:"Expiry Time",usageRestriction:"Usage Restriction",isThirdParty:"Third Party",matchSubDomains:"Subdomain Matching",rawTokenText:"Raw Token",status:"Token Status",token:"Token",tokens:"{PH1} tokens"},str_=i18n.i18n.registerUIStrings("panels/application/components/OriginTrialTreeView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class Badge extends HTMLElement{static litTagName=LitHtml.literal`devtools-resources-origin-trial-tree-view-badge`;#e=this.attachShadow({mode:"open"});#t=new Adorners.Adorner.Adorner;set data(e){this.#i(e)}connectedCallback(){this.#e.adoptedStyleSheets=[badgeStyles]}#i(e){const t=document.createElement("span");t.textContent=e.badgeContent,this.#t.data={name:"badge",content:t},this.#t.classList.add(`badge-${e.style}`),LitHtml.render(LitHtml.html`
      ${this.#t}
    `,this.#e,{host:this})}}function constructOriginTrialTree(e){return{treeNodeData:e,id:"OriginTrialTreeNode#"+e.trialName,children:async()=>e.tokensWithStatus.length>1?e.tokensWithStatus.map(constructTokenNode):constructTokenDetailsNodes(e.tokensWithStatus[0]),renderer:e=>{const t=e.treeNodeData,i=LitHtml.html`
        <${Badge.litTagName} .data=${{badgeContent:i18nString(UIStrings.tokens,{PH1:t.tokensWithStatus.length}),style:"secondary"}}></${Badge.litTagName}>
      `;return LitHtml.html`
        ${t.trialName}
        <${Badge.litTagName} .data=${{badgeContent:t.status,style:"Enabled"===t.status?"success":"error"}}></${Badge.litTagName}>
        ${t.tokensWithStatus.length>1?i:LitHtml.nothing}
      `}}}function constructTokenNode(e){return{treeNodeData:e.status,id:"TokenNode#"+e.rawTokenText,children:async()=>constructTokenDetailsNodes(e),renderer:(e,t)=>{const i=e.treeNodeData,n=LitHtml.html`
        <${Badge.litTagName} .data=${{badgeContent:i,style:"Success"===i?"success":"error"}}></${Badge.litTagName}>
      `;return LitHtml.html`${i18nString(UIStrings.token)} ${t.isExpanded?LitHtml.nothing:n}`}}}function renderTokenDetails(e){return LitHtml.html`
    <${OriginTrialTokenRows.litTagName} .data=${{node:e}}>
    </${OriginTrialTokenRows.litTagName}>
    `}function constructTokenDetailsNodes(e){return[{treeNodeData:e,id:"TokenDetailsNode#"+e.rawTokenText,renderer:renderTokenDetails},constructRawTokenTextNode(e.rawTokenText)]}function constructRawTokenTextNode(e){return{treeNodeData:i18nString(UIStrings.rawTokenText),id:"TokenRawTextContainerNode#"+e,children:async()=>[{treeNodeData:e,id:"TokenRawTextNode#"+e,renderer:e=>{const t=e.treeNodeData;return LitHtml.html`
        <div style="overflow-wrap: break-word;">
          ${t}
        </div>
        `}}]}}function defaultRenderer(e){return LitHtml.html`${String(e.treeNodeData)}`}ComponentHelpers.CustomElements.defineComponent("devtools-resources-origin-trial-tree-view-badge",Badge);export class OriginTrialTokenRows extends HTMLElement{static litTagName=LitHtml.literal`devtools-resources-origin-trial-token-rows`;#e=this.attachShadow({mode:"open"});#n=null;#r=[];#a=new Intl.DateTimeFormat(i18n.DevToolsLocale.DevToolsLocale.instance().locale,{dateStyle:"long",timeStyle:"long"});set data(e){this.#n=e.node.treeNodeData,this.#s()}connectedCallback(){this.#e.adoptedStyleSheets=[originTrialTokenRowsStyles],this.#i()}#o=(e,t)=>LitHtml.html`
        <div class=${LitHtml.Directives.ifDefined(t?"error-text":void 0)}>
          ${e}
        </div>`;#s(){this.#n?.parsedToken&&(this.#r=[{name:i18nString(UIStrings.origin),value:this.#o(this.#n.parsedToken.origin,"WrongOrigin"===this.#n.status)},{name:i18nString(UIStrings.expiryTime),value:this.#o(this.#a.format(1e3*this.#n.parsedToken.expiryTime),"Expired"===this.#n.status)},{name:i18nString(UIStrings.usageRestriction),value:this.#o(this.#n.parsedToken.usageRestriction)},{name:i18nString(UIStrings.isThirdParty),value:this.#o(this.#n.parsedToken.isThirdParty.toString())},{name:i18nString(UIStrings.matchSubDomains),value:this.#o(this.#n.parsedToken.matchSubDomains.toString())}],"UnknownTrial"===this.#n.status&&(this.#r=[{name:i18nString(UIStrings.trialName),value:this.#o(this.#n.parsedToken.trialName)},...this.#r]))}#i(){if(!this.#n)return;const e=[{name:i18nString(UIStrings.status),value:LitHtml.html`
          <${Badge.litTagName} .data=${{badgeContent:this.#n.status,style:"Success"===this.#n.status?"success":"error"}}></${Badge.litTagName}>`},...this.#r].map((e=>LitHtml.html`
          <div class="key">${e.name}</div>
          <div class="value">${e.value}</div>
          `));LitHtml.render(LitHtml.html`
      <div class="content">
        ${e}
      </div>
    `,this.#e,{host:this})}}ComponentHelpers.CustomElements.defineComponent("devtools-resources-origin-trial-token-rows",OriginTrialTokenRows);export class OriginTrialTreeView extends HTMLElement{static litTagName=LitHtml.literal`devtools-resources-origin-trial-tree-view`;#e=this.attachShadow({mode:"open"});set data(e){this.#i(e.trials)}#i(e){e.length&&LitHtml.render(LitHtml.html`
      <${TreeOutline.TreeOutline.TreeOutline.litTagName} .data=${{tree:e.map(constructOriginTrialTree),defaultRenderer:defaultRenderer}}>
      </${TreeOutline.TreeOutline.TreeOutline.litTagName}>
    `,this.#e,{host:this})}}ComponentHelpers.CustomElements.defineComponent("devtools-resources-origin-trial-tree-view",OriginTrialTreeView);