<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<div class="component-state-dialog" tabindex="0">
    <h2 mat-dialog-title>Component State</h2>
    <mat-dialog-content>
        <div class="flex flex-col justify-between gap-y-5">
            <div class="flex flex-col" *ngIf="componentName$ | async; let componentName">
                <div>Name</div>
                <div class="value">{{ componentName }}</div>
            </div>
            <div class="flex flex-col">
                <div>Description</div>
                <div class="value">
                    {{ stateDescription }}
                </div>
            </div>
            <div class="listing-table">
                <form [formGroup]="filterForm" class="flex flex-col gap-y-2">
                    <div class="value">Displaying {{ filteredEntries }} of {{ totalEntries }}</div>
                    <div class="flex justify-between items-center">
                        <mat-form-field>
                            <mat-label>Filter</mat-label>
                            <input matInput type="text" class="small" formControlName="filterTerm" />
                        </mat-form-field>
                        <ng-container *ngIf="{ value: (canClear$ | async)! } as canClear">
                            <div *ngIf="canClear.value && totalEntries > 0">
                                <a (click)="clearState()">Clear state</a>
                            </div>
                        </ng-container>
                    </div>
                </form>
                <div class="h-72 overflow-y-auto overflow-x-hidden border">
                    <table
                        mat-table
                        [dataSource]="dataSource"
                        matSort
                        matSortDisableClear
                        (matSortChange)="sortData($event)"
                        [matSortActive]="initialSortColumn"
                        [matSortDirection]="initialSortDirection">
                        <!-- Key Column -->
                        <ng-container matColumnDef="key">
                            <th mat-header-cell *matHeaderCellDef mat-sort-header>Key</th>
                            <td mat-cell *matCellDef="let item">
                                {{ item.key }}
                            </td>
                        </ng-container>

                        <!-- Value Column -->
                        <ng-container matColumnDef="value">
                            <th mat-header-cell *matHeaderCellDef mat-sort-header>Value</th>
                            <td mat-cell *matCellDef="let item" [title]="item.value">
                                {{ item.value }}
                            </td>
                        </ng-container>

                        <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
                        <tr
                            mat-row
                            *matRowDef="let row; let even = even; columns: displayedColumns"
                            [class.even]="even"></tr>
                    </table>
                </div>
            </div>
            <div *ngIf="partialResults" class="-mt-3">Showing partial results</div>
        </div>
    </mat-dialog-content>
    <mat-dialog-actions align="end">
        <button color="primary" mat-raised-button mat-dialog-close>Close</button>
    </mat-dialog-actions>
</div>
