<!--
  ~ 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">
    <h2 mat-dialog-title>Component State</h2>
    <error-banner></error-banner>
    <mat-dialog-content>
        <div class="dialog-content flex flex-col justify-between gap-y-5">
            @if (componentName$ | async; as componentName) {
                <div class="flex flex-col">
                    <div>Name</div>
                    <div class="accent-color font-medium">{{ componentName }}</div>
                </div>
            }
            <div class="flex flex-col">
                <div>Description</div>
                <div class="accent-color font-medium">
                    {{ stateDescription }}
                </div>
            </div>
            <div class="flex flex-1 flex-col">
                <form [formGroup]="filterForm" class="flex flex-col gap-y-2">
                    <div class="accent-color font-medium">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>
                        @if ({ value: (canClear$ | async)! }; as canClear) {
                            @if (canClear.value && totalEntries > 0) {
                                <div>
                                    <a (click)="clearState()">Clear state</a>
                                </div>
                            }
                        }
                    </div>
                </form>
                <div class="listing-table flex-1 relative">
                    <div class="absolute inset-0 overflow-y-auto overflow-x-hidden">
                        <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>

                            <!-- Scope Column -->
                            @if (displayedColumns.includes('scope')) {
                                <ng-container matColumnDef="scope">
                                    <th mat-header-cell *matHeaderCellDef mat-sort-header>Scope</th>
                                    <td mat-cell *matCellDef="let item" [title]="item.scope">
                                        {{ item.scope }}
                                    </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>
            @if (partialResults) {
                <div class="-mt-3">Showing partial results</div>
            }
        </div>
    </mat-dialog-content>
    <mat-dialog-actions align="end">
        <button mat-button mat-dialog-close>Close</button>
    </mat-dialog-actions>
</div>
