import*as i18n from"../../../core/i18n/i18n.js";import*as SDK from"../../../core/sdk/sdk.js";import*as DataGrid from"../../../ui/components/data_grid/data_grid.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 RenderCoordinator from"../../../ui/components/render_coordinator/render_coordinator.js";import*as LitHtml from"../../../ui/lit-html/lit-html.js";import trustTokensViewStyles from"./trustTokensView.css.js";import trustTokensViewDeleteButtonStyles from"./trustTokensViewDeleteButton.css.js";const UIStrings={issuer:"Issuer",storedTokenCount:"Stored token count",allStoredTrustTokensAvailableIn:"All stored private state tokens available in this browser instance.",noTrustTokensStored:"No private state tokens are currently stored.",deleteTrustTokens:"Delete all stored private state tokens issued by {PH1}.",trustTokens:"Private state tokens"},str_=i18n.i18n.registerUIStrings("panels/application/components/TrustTokensView.ts",UIStrings);export const i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);class TrustTokensDeleteButton extends HTMLElement{static litTagName=LitHtml.literal`devtools-trust-tokens-delete-button`;#t=this.attachShadow({mode:"open"});#e=null;#n=()=>{};connectedCallback(){this.#t.adoptedStyleSheets=[trustTokensViewDeleteButtonStyles]}set data(t){this.#e=t.issuer,this.#n=t.deleteClickHandler,this.#r()}#r(){this.#e&&LitHtml.render(LitHtml.html`
      <!-- Wrap the button in a container, otherwise we can't center it inside the column. -->
      <span class="button-container">
        <button class="delete-button"
          title=${i18nString(UIStrings.deleteTrustTokens,{PH1:this.#e})}
          @click=${()=>this.#n(this.#e)}>
          <${IconButton.Icon.Icon.litTagName} .data=${{iconName:"bin",color:"var(--icon-default)",width:"14px",height:"14px"}}>
          </${IconButton.Icon.Icon.litTagName}>
        </button>
      </span>`,this.#t,{host:this})}}const coordinator=RenderCoordinator.RenderCoordinator.RenderCoordinator.instance(),REFRESH_INTERVAL_MS=1e3;export class TrustTokensView extends LegacyWrapper.LegacyWrapper.WrappableComponent{static litTagName=LitHtml.literal`devtools-trust-tokens-storage-view`;#t=this.attachShadow({mode:"open"});#n(t){const e=SDK.TargetManager.TargetManager.instance().primaryPageTarget();e?.storageAgent().invoke_clearTrustTokens({issuerOrigin:t})}connectedCallback(){this.wrapper?.contentElement.classList.add("vbox"),this.#t.adoptedStyleSheets=[trustTokensViewStyles],this.render()}async render(){const t=SDK.TargetManager.TargetManager.instance().primaryPageTarget();if(!t)return;const{tokens:e}=await t.storageAgent().invoke_getTrustTokens();await coordinator.write("Render TrustTokensView",(()=>{LitHtml.render(LitHtml.html`
        <div>
          <span class="heading">${i18nString(UIStrings.trustTokens)}</span>
          <${IconButton.Icon.Icon.litTagName} class="info-icon" title=${i18nString(UIStrings.allStoredTrustTokensAvailableIn)}
            .data=${{iconName:"info",color:"var(--icon-default)",width:"16px"}}>
          </${IconButton.Icon.Icon.litTagName}>
          ${this.#s(e)}
        </div>
      `,this.#t,{host:this}),this.isConnected&&setTimeout((()=>this.render()),1e3)}))}#s(t){if(0===t.length)return LitHtml.html`<div class="no-tt-message">${i18nString(UIStrings.noTrustTokensStored)}</div>`;const e={columns:[{id:"issuer",title:i18nString(UIStrings.issuer),widthWeighting:10,hideable:!1,visible:!0,sortable:!0},{id:"count",title:i18nString(UIStrings.storedTokenCount),widthWeighting:5,hideable:!1,visible:!0,sortable:!0},{id:"delete-button",title:"",widthWeighting:1,hideable:!1,visible:!0,sortable:!1}],rows:this.#o(t),initialSort:{columnId:"issuer",direction:"ASC"}};return LitHtml.html`
      <${DataGrid.DataGridController.DataGridController.litTagName} .data=${e}></${DataGrid.DataGridController.DataGridController.litTagName}>
    `}#o(t){return t.filter((t=>t.count>0)).map((t=>({cells:[{columnId:"delete-button",value:removeTrailingSlash(t.issuerOrigin),renderer:this.#i.bind(this)},{columnId:"issuer",value:removeTrailingSlash(t.issuerOrigin)},{columnId:"count",value:t.count}]})))}#i(t){return LitHtml.html`<${TrustTokensDeleteButton.litTagName}
     .data=${{issuer:t,deleteClickHandler:this.#n}}
    ></${TrustTokensDeleteButton.litTagName}>`}}function removeTrailingSlash(t){return t.replace(/\/$/,"")}ComponentHelpers.CustomElements.defineComponent("devtools-trust-tokens-delete-button",TrustTokensDeleteButton),ComponentHelpers.CustomElements.defineComponent("devtools-trust-tokens-storage-view",TrustTokensView);