import*as Common from"../../../../core/common/common.js";import*as i18n from"../../../../core/i18n/i18n.js";import{assertNotNullOrUndefined}from"../../../../core/platform/platform.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 LitHtml from"../../../../ui/lit-html/lit-html.js";import*as NetworkForward from"../../../network/forward/forward.js";import*as PreloadingHelper from"../helper/helper.js";import ruleSetGridStyles from"./ruleSetGrid.css.js";import*as PreloadingString from"./PreloadingString.js";const UIStrings={ruleSet:"Rule set",status:"Status",buttonClickToRevealInElementsPanel:"Click to reveal in Elements panel",buttonClickToRevealInNetworkPanel:"Click to reveal in Network panel",errors:"{errorCount, plural, =1 {# error} other {# errors}}",buttonRevealPreloadsAssociatedWithRuleSet:"Reveal preloads associated with this rule set"},str_=i18n.i18n.registerUIStrings("panels/application/preloading/components/RuleSetGrid.ts",UIStrings);export const i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class RuleSetGrid extends LegacyWrapper.LegacyWrapper.WrappableComponent{static litTagName=LitHtml.literal`devtools-resources-ruleset-grid`;#e=this.attachShadow({mode:"open"});#t=null;connectedCallback(){this.#e.adoptedStyleSheets=[ruleSetGridStyles],this.#r()}update(e){this.#t=e,this.#r()}#r(){if(null===this.#t)return;const e={columns:[{id:"ruleSet",title:i18nString(UIStrings.ruleSet),widthWeighting:20,hideable:!1,visible:!0,sortable:!0},{id:"status",title:i18nString(UIStrings.status),widthWeighting:80,hideable:!1,visible:!0,sortable:!0}],rows:this.#n(),striped:!0};LitHtml.render(LitHtml.html`
      <div class="ruleset-container">
        <${DataGrid.DataGridController.DataGridController.litTagName} .data=${e}>
        </${DataGrid.DataGridController.DataGridController.litTagName}>
      </div>
    `,this.#e,{host:this})}#n(){assertNotNullOrUndefined(this.#t);const e=this.#t.pageURL;return this.#t.rows.map((t=>({cells:[{columnId:"id",value:t.ruleSet.id},{columnId:"ruleSet",value:"",renderer:()=>ruleSetRenderer(t.ruleSet,e)},{columnId:"status",value:t.preloadsStatusSummary,renderer:e=>statusRenderer(e,t.ruleSet)}]})))}}function ruleSetRenderer(e,t){const r=PreloadingString.ruleSetLocationShort(e,t);return void 0!==e.backendNodeId?function(e,t){return assertNotNullOrUndefined(e.backendNodeId),LitHtml.html`
      <button class="link" role="link"
        @click=${async()=>{assertNotNullOrUndefined(e.backendNodeId);const t=SDK.TargetManager.TargetManager.instance().scopeTarget();null!==t&&await Common.Revealer.reveal(new SDK.DOMModel.DeferredDOMNode(t,e.backendNodeId))}}
        title=${i18nString(UIStrings.buttonClickToRevealInElementsPanel)}
        style=${LitHtml.Directives.styleMap({border:"none",background:"none","padding-inline-start":"0","padding-inline-end":"0"})}
      >
        <${IconButton.Icon.Icon.litTagName}
          .data=${{iconName:"code-circle",color:"var(--icon-link)",width:"16px",height:"16px"}}
          style=${LitHtml.Directives.styleMap({"vertical-align":"sub"})}
        >
        </${IconButton.Icon.Icon.litTagName}>
        ${t}
      </button>
    `}(e,r):void 0!==e.url&&e.requestId?function(e,t){return assertNotNullOrUndefined(e.url),assertNotNullOrUndefined(e.requestId),LitHtml.html`
      <button class="link" role="link"
        @click=${async()=>{assertNotNullOrUndefined(e.requestId);const t=SDK.TargetManager.TargetManager.instance().scopeTarget()?.model(SDK.NetworkManager.NetworkManager)?.requestForId(e.requestId)||null;if(null===t)return;const r=NetworkForward.UIRequestLocation.UIRequestLocation.tab(t,NetworkForward.UIRequestLocation.UIRequestTabs.Preview,{clearFilter:!1});await Common.Revealer.reveal(r)}}
        title=${i18nString(UIStrings.buttonClickToRevealInNetworkPanel)}
        style=${LitHtml.Directives.styleMap({border:"none",background:"none","padding-inline-start":"0","padding-inline-end":"0"})}
      >
        <${IconButton.Icon.Icon.litTagName}
         .data=${{iconName:"arrow-up-down-circle",color:"var(--icon-link)",width:"16px",height:"16px"}}
          style=${LitHtml.Directives.styleMap({"vertical-align":"sub"})}
        >
        </${IconButton.Icon.Icon.litTagName}>
        ${t}
      </button>
    `}(e,r):LitHtml.html`${r}`}function statusRenderer(e,t){function r(e,t){return LitHtml.html`
      <button class="link" role="link"
        @click=${async()=>{await Common.Revealer.reveal(new PreloadingHelper.PreloadingForward.AttemptViewWithFilter(t.id))}}
        title=${i18nString(UIStrings.buttonRevealPreloadsAssociatedWithRuleSet)}
        style=${LitHtml.Directives.styleMap({color:"var(--color-link)","text-decoration":"underline",border:"none",background:"none","padding-inline-start":"0","padding-inline-end":"0"})}
      >
        ${e}
      </button>
    `}function n(){const e=i18nString(UIStrings.errors,{errorCount:1});return LitHtml.html`
      <span
        style=${LitHtml.Directives.styleMap({color:"var(--color-error)"})}
      >
        ${e}
      </span>
    `}switch(t.errorType){case void 0:return r(e,t);case"SourceIsNotJsonObject":return n();case"InvalidRulesSkipped":return LitHtml.html`${n()} ${r(e,t)}`}}ComponentHelpers.CustomElements.defineComponent("devtools-resources-ruleset-grid",RuleSetGrid);