import*as LitHtml from"../../../ui/lit-html/lit-html.js";import*as ComponentHelpers from"../helpers/helpers.js";import*as i18n from"../../../core/i18n/i18n.js";import{getRowEntryForColumnId,getStringifiedCellValues}from"./DataGridUtils.js";import{DataGrid}from"./DataGrid.js";import dataGridControllerStyles from"./dataGridController.css.js";import{alert}from"../../legacy/ARIAUtils.js";const UIStrings={sortInAscendingOrder:"{PH1} sorted in ascending order",sortInDescendingOrder:"{PH1} sorted in descending order",sortingCanceled:"{PH1} sorting canceled"},str_=i18n.i18n.registerUIStrings("ui/components/data_grid/DataGridController.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class DataGridController extends HTMLElement{static litTagName=LitHtml.literal`devtools-data-grid-controller`;#t=this.attachShadow({mode:"open"});#e=!1;#s=[];#o=[];#i=void 0;#r=void 0;#n=!1;#l=!1;#a=[];#d=[];#h=null;#c=[];#u;connectedCallback(){this.#t.adoptedStyleSheets=[dataGridControllerStyles]}get data(){return{columns:this.#a,rows:this.#d,filters:this.#c,contextMenus:this.#i,label:this.#r,paddingRowsCount:this.#u,showScrollbar:this.#n,striped:this.#l}}set data(t){this.#a=t.columns,this.#d=t.rows,this.#i=t.contextMenus,this.#c=t.filters||[],this.#i=t.contextMenus,this.#r=t.label,this.#n=t.showScrollbar,this.#l=t.striped,this.#s=[...this.#a],this.#o=this.#m(t.rows,this.#c),!this.#e&&t.initialSort&&(this.#h=t.initialSort),this.#h&&this.#g(this.#h),this.#u=t.paddingRowsCount,this.#C()}#S(t,e,s){let o=!1;const{key:i,text:r,negative:n,regex:l}=e;let a;return a=getStringifiedCellValues(i?[getRowEntryForColumnId(t,i)]:t.cells.filter((t=>s.has(t.columnId)))),l?o=l.test(a):r&&(o=a.includes(r.toLowerCase())),n?!o:o}#m(t,e){if(0===e.length)return[...t];const s=new Set(this.#s.filter((t=>t.visible)).map((t=>t.id)));return t.map((t=>{let o=!0;for(const i of e){if(!this.#S(t,i,s)){o=!1;break}}return{...t,hidden:!o}}))}#g(t){const{columnId:e,direction:s}=t;this.#o.sort(((t,o)=>{const i=getRowEntryForColumnId(t,e),r=getRowEntryForColumnId(o,e),n="number"==typeof i.value?i.value:String(i.value).toUpperCase(),l="number"==typeof r.value?r.value:String(r.value).toUpperCase();return n<l?"ASC"===s?-1:1:n>l?"ASC"===s?1:-1:0})),this.#C()}#w(t){const{column:e}=t.data;e.sortable&&this.#p(e)}#p(t){if(this.#h&&this.#h.columnId===t.id){const{columnId:t,direction:e}=this.#h;this.#h="DESC"===e?null:{columnId:t,direction:"DESC"}}else this.#h={columnId:t.id,direction:"ASC"};const e=t.title;this.#h?(this.#g(this.#h),alert("ASC"===this.#h.direction?i18nString(UIStrings.sortInAscendingOrder,{PH1:e||""}):i18nString(UIStrings.sortInDescendingOrder,{PH1:e||""}))):(this.#o=this.#m(this.#d,this.#c),this.#C(),alert(i18nString(UIStrings.sortingCanceled,{PH1:e||""})))}#R(t){this.#p(t.data.column)}#f(){this.#h=null,this.#o=[...this.#d],this.#C()}#C(){LitHtml.render(LitHtml.html`
      <${DataGrid.litTagName} .data=${{columns:this.#s,rows:this.#o,activeSort:this.#h,contextMenus:this.#i,label:this.#r,paddingRowsCount:this.#u,showScrollbar:this.#n,striped:this.#l}}
        @columnheaderclick=${this.#w}
        @contextmenucolumnsortclick=${this.#R}
        @contextmenuheaderresetclick=${this.#f}
     ></${DataGrid.litTagName}>
    `,this.#t,{host:this}),this.#e=!0}}ComponentHelpers.CustomElements.defineComponent("devtools-data-grid-controller",DataGridController);