<!--

    Copyright © 2016-2026 The Thingsboard Authors

    Licensed 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.

-->
<section class="tb-form-panel no-padding no-border">
  <div class="tb-form-panel stroked">
    <div class="flex flex-row items-center justify-between">
      <div class="tb-form-panel-title" translate>filter.key-filters</div>
      <ng-content select="[keyFilterHeaderActions]"></ng-content>
    </div>
    @if (keyFiltersFormArray.length) {
      <section class="tb-form-panel no-padding no-gap stroked">
        <div class="flex flex-row">
          <span class="min-w-16"></span>
          <div class="flex flex-1 flex-row items-center justify-start">
            <div translate class="filter-title flex-1">filter.key-name</div>
            <div translate class="filter-title flex-1">filter.key-type.key-type</div>
            <span [class.!hidden]="disabled" style="min-width: 96px;">&nbsp;</span>
            <span [class.!hidden]="!disabled" style="min-width: 48px;">&nbsp;</span>
          </div>
        </div>
        <mat-divider></mat-divider>
        <div class="filter-list">
          @for (keyFilterControl of keyFiltersFormArray.controls; track keyFilterControl; let index = $index) {
            <div class="flex flex-row items-stretch justify-start"
                 [class.filter-list-divider]="index">
              <div class="filters-operation min-w-16">
                @if (index) {
                  <div class="filters-operation-container">
                    <span class="filters-operation-label">{{ complexOperationTranslationMap.get(operation) | translate }}</span>
                  </div>
                }
              </div>
              <div class="flex flex-1 flex-col">
                <div class="flex flex-row items-center justify-start">
                  <div class="filters-text flex-1" tbTruncateWithTooltip>{{ keyFilterControl.value.key.key }}</div>
                  <div class="filters-text flex-1" tbTruncateWithTooltip>{{ entityKeyTypeTranslations.get(keyFilterControl.value.key.type) | translate }}</div>
                  <button mat-icon-button color="primary"
                          type="button"
                          (click)="editKeyFilter(index)"
                          matTooltip="{{ (disabled ? 'filter.key-filter' : 'filter.edit-key-filter') | translate }}"
                          matTooltipPosition="above">
                    <mat-icon>{{disabled ? 'visibility' : 'edit'}}</mat-icon>
                  </button>
                  <button mat-icon-button color="primary"
                          [class.!hidden]="disabled"
                          type="button"
                          (click)="removeKeyFilter(index)"
                          matTooltip="{{ 'filter.remove-key-filter' | translate }}"
                          matTooltipPosition="above">
                    <mat-icon>close</mat-icon>
                  </button>
                </div>
              </div>
            </div>
          }
        </div>
      </section>
    } @else {
      <span class="no-data-found tb-prompt flex items-center justify-center"
            [class.required]="!disabled"
            translate>filter.no-key-filters</span>
    }
    <div>
      <button mat-button mat-stroked-button color="primary"
              [class.!hidden]="disabled"
              (click)="addKeyFilter()"
              type="button">
        {{ 'filter.add-key-filter' | translate }}
      </button>
    </div>
  </div>
  <div class="tb-form-panel no-gap stroked">
    <mat-expansion-panel class="tb-settings" [expanded]="true">
      <mat-expansion-panel-header>
        {{ 'filter.preview' | translate }}
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        @if (keyFiltersFormArray.length) {
          <tb-filter-text [formControl]="keyFiltersControl" [operation]="operation"></tb-filter-text>
        } @else {
          <span class="tb-prompt justify-center text-base" translate>filter.no-filter-text</span>
        }
      </ng-template>
    </mat-expansion-panel>
  </div>
</section>
