import"./DataGrid.js";import*as LitHtml from"../../third_party/lit-html/lit-html.js";import{getRowEntryForColumnId}from"./DataGridUtils.js";export class DataGridController extends HTMLElement{constructor(){super(...arguments),this.shadow=this.attachShadow({mode:"open"}),this.hasRenderedAtLeastOnce=!1,this.columns=[],this.rows=[],this.contextMenus=void 0,this.originalColumns=[],this.originalRows=[],this.sortState=null,this.filters=[]}get data(){return{columns:this.originalColumns,rows:this.originalRows,filters:this.filters,contextMenus:this.contextMenus}}set data(t){this.originalColumns=t.columns,this.originalRows=t.rows,this.contextMenus=t.contextMenus,this.filters=t.filters||[],this.contextMenus=t.contextMenus,this.columns=[...this.originalColumns],this.rows=this.cloneAndFilterRows(t.rows,this.filters),!this.hasRenderedAtLeastOnce&&t.initialSort&&(this.sortState=t.initialSort),this.sortState&&this.sortRows(this.sortState),this.render()}testRowWithFilter(t,e){let s=!1;const{key:o,text:i,negative:n,regex:r}=e;let l;if(o){const e=getRowEntryForColumnId(t,o);l=JSON.stringify(e.value).toLowerCase()}else l=JSON.stringify(t.cells.map((t=>t.value))).toLowerCase();return r?s=r.test(l):i&&(s=l.includes(i.toLowerCase())),n?!s:s}cloneAndFilterRows(t,e){return 0===e.length?[...t]:t.map((t=>{let s=!0;for(const o of e){if(!this.testRowWithFilter(t,o)){s=!1;break}}return{...t,hidden:!s}}))}sortRows(t){const{columnId:e,direction:s}=t;this.rows.sort(((t,o)=>{const i=getRowEntryForColumnId(t,e),n=getRowEntryForColumnId(o,e),r="number"===typeof i.value?i.value:String(i.value).toUpperCase(),l="number"===typeof n.value?n.value:String(n.value).toUpperCase();return r<l?"ASC"===s?-1:1:r>l?"ASC"===s?1:-1:0})),this.render()}onColumnHeaderClick(t){const{column:e}=t.data;this.applySortOnColumn(e)}applySortOnColumn(t){if(this.sortState&&this.sortState.columnId===t.id){const{columnId:t,direction:e}=this.sortState;this.sortState="DESC"===e?null:{columnId:t,direction:"DESC"}}else this.sortState={columnId:t.id,direction:"ASC"};this.sortState?this.sortRows(this.sortState):(this.rows=[...this.originalRows],this.render())}onContextMenuColumnSortClick(t){this.applySortOnColumn(t.data.column)}onContextMenuHeaderResetClick(){this.sortState=null,this.rows=[...this.originalRows],this.render()}render(){LitHtml.render(LitHtml.html`
      <style>
        :host {
          display: block;
          height: 100%;
          overflow: hidden;
        }
      </style>
      <devtools-data-grid .data=${{columns:this.columns,rows:this.rows,activeSort:this.sortState,contextMenus:this.contextMenus}}
        @column-header-click=${this.onColumnHeaderClick}
        @context-menu-column-sort-click=${this.onContextMenuColumnSortClick}
        @context-menu-header-reset-click=${this.onContextMenuHeaderResetClick}
     ></devtools-data-grid>
    `,this.shadow,{eventContext:this}),this.hasRenderedAtLeastOnce=!0}}customElements.define("devtools-data-grid-controller",DataGridController);