import*as i18n from"../../../core/i18n/i18n.js";import*as SDK from"../../../core/sdk/sdk.js";import*as Buttons from"../../../ui/components/buttons/buttons.js";import*as ChromeLink from"../../../ui/components/chrome_link/chrome_link.js";import*as DataGrid from"../../../ui/components/data_grid/data_grid.js";import*as ComponentHelpers from"../../../ui/components/helpers/helpers.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 bounceTrackingMitigationsViewStyles from"./bounceTrackingMitigationsView.css.js";const UIStrings={bounceTrackingMitigationsTitle:"Bounce tracking mitigations",forceRun:"Force run",runningMitigations:"Running",stateDeletedFor:"State was deleted for the following sites:",checkingPotentialTrackers:"Checking for potential bounce tracking sites.",learnMore:"Learn more: Bounce Tracking Mitigations",noPotentialBounceTrackersIdentified:"State was not cleared for any potential bounce tracking sites. Either none were identified or third-party cookies are not blocked.",featureDisabled:'Bounce tracking mitigations are disabled. To enable them, set the flag at {PH1} to "Enabled With Deletion".',featureFlag:"Bounce Tracking Mitigations Feature Flag"},str_=i18n.i18n.registerUIStrings("panels/application/components/BounceTrackingMitigationsView.ts",UIStrings);export const i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class BounceTrackingMitigationsView extends LegacyWrapper.LegacyWrapper.WrappableComponent{static litTagName=LitHtml.literal`devtools-bounce-tracking-mitigations-view`;#t=this.attachShadow({mode:"open"});#e=[];#i="Result";#n=!1;#r=!1;connectedCallback(){this.#t.adoptedStyleSheets=[bounceTrackingMitigationsViewStyles],this.#a()}async#a(){LitHtml.render(LitHtml.html`
      <${ReportView.ReportView.Report.litTagName} .data=${{reportTitle:i18nString(UIStrings.bounceTrackingMitigationsTitle)}}>
        ${await this.#o()}
      </${ReportView.ReportView.Report.litTagName}>
    `,this.#t,{host:this})}async#o(){if(this.#n||await this.#s(),"Disabled"===this.#i){const t=new ChromeLink.ChromeLink.ChromeLink;return t.href="chrome://flags/#bounce-tracking-mitigations",t.textContent=i18nString(UIStrings.featureFlag),LitHtml.html`
        <${ReportView.ReportView.ReportSection.litTagName}>
          ${i18n.i18n.getFormatLocalizedString(str_,UIStrings.featureDisabled,{PH1:t})}
        </${ReportView.ReportView.ReportSection.litTagName}>
      `}return LitHtml.html`
      <${ReportView.ReportView.ReportSection.litTagName}>
        ${this.#c()}
      </${ReportView.ReportView.ReportSection.litTagName}>
        ${this.#l()}
      <${ReportView.ReportView.ReportSectionDivider.litTagName}>
      </${ReportView.ReportView.ReportSectionDivider.litTagName}>
      <${ReportView.ReportView.ReportSection.litTagName}>
        <x-link href="https://privacycg.github.io/nav-tracking-mitigations/#bounce-tracking-mitigations" class="link">
          ${i18nString(UIStrings.learnMore)}
        </x-link>
      </${ReportView.ReportView.ReportSection.litTagName}>
    `}#c(){const t="Running"===this.#i;return LitHtml.html`
      <${Buttons.Button.Button.litTagName}
        aria-label=${i18nString(UIStrings.forceRun)}
        .disabled=${t}
        .spinner=${t}
        .variant=${"primary"}
        @click=${this.#g}>
        ${t?LitHtml.html`
          ${i18nString(UIStrings.runningMitigations)}`:`\n          ${i18nString(UIStrings.forceRun)}\n        `}
      </${Buttons.Button.Button.litTagName}>
    `}#l(){if(!this.#r)return LitHtml.html``;if(0===this.#e.length)return LitHtml.html`
        <${ReportView.ReportView.ReportSection.litTagName}>
        ${"Running"===this.#i?LitHtml.html`
          ${i18nString(UIStrings.checkingPotentialTrackers)}`:`\n          ${i18nString(UIStrings.noPotentialBounceTrackersIdentified)}\n        `}
        </${ReportView.ReportView.ReportSection.litTagName}>
      `;const t={columns:[{id:"sites",title:i18nString(UIStrings.stateDeletedFor),widthWeighting:10,hideable:!1,visible:!0,sortable:!0}],rows:this.#m(),initialSort:{columnId:"sites",direction:"ASC"}};return LitHtml.html`
      <${ReportView.ReportView.ReportSection.litTagName}>
        <${DataGrid.DataGridController.DataGridController.litTagName} .data=${t}>
        </${DataGrid.DataGridController.DataGridController.litTagName}>
      </${ReportView.ReportView.ReportSection.litTagName}>
    `}async#g(){const t=SDK.TargetManager.TargetManager.instance().primaryPageTarget();if(!t)return;this.#r=!0,this.#i="Running",this.#a();const e=await t.storageAgent().invoke_runBounceTrackingMitigations();this.#e=[],e.deletedSites.forEach((t=>{this.#e.push(t)})),this.#p()}#p(){this.#i="Result",this.#a()}#m(){return this.#e.map((t=>({cells:[{columnId:"sites",value:t}]})))}async#s(){this.#n=!0;const t=SDK.TargetManager.TargetManager.instance().primaryPageTarget();t&&((await t.systemInfo().invoke_getFeatureState({featureState:"DIPS"})).featureEnabled||(this.#i="Disabled"))}}ComponentHelpers.CustomElements.defineComponent("devtools-bounce-tracking-mitigations-view",BounceTrackingMitigationsView);