<!--

    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="flex flex-col gap-4" [formGroup]="filterListFormGroup">
  @if (predicatesFormArray.length) {
    <div class="tb-form-panel no-padding no-gap stroked">
      <div class="flex flex-row">
        <div class="flex flex-full flex-row items-center justify-start gap-2">
          <div class="flex flex-1 flex-row pl-2">
            <label translate class="filter-title max-w-30% flex-full">filter.operation.operation</label>
            <label translate class="filter-title max-w-70% flex-full">filter.value</label>
          </div>
          @if (displayUserParameters) {
            <span [class.!hidden]="disabled" class="min-w-12">&nbsp;</span>
          }
        </div>
      </div>
      <mat-divider style="padding-bottom: 5px;"></mat-divider>
      <div class="predicate-list">
        @for (predicateControl of predicatesFormArray.controls; track predicateControl) {
          <div class="relative flex flex-row items-stretch justify-start" [class.key-filter-list-divider]="$index">
            @if ($index) {
              <div class="filters-operation-container">
                <span class="filters-operation-label">{{ complexOperationTranslations.get(operation) | translate }}</span>
              </div>
            }
            <div class="flex flex-full flex-col">
              <div class="flex flex-1 flex-row items-center justify-start">
                <tb-filter-predicate
                  class="flex-1"
                  [valueType]="valueType"
                  [displayUserParameters]="displayUserParameters"
                  [allowUserDynamicSource]="allowUserDynamicSource"
                  [onlyUserDynamicSource]="onlyUserDynamicSource"
                  [key]="key"
                  [formControl]="predicateControl">
                </tb-filter-predicate>
                <button mat-icon-button color="primary"
                        [class.!hidden]="disabled"
                        type="button"
                        (click)="removePredicate($index)"
                        matTooltip="{{ 'filter.remove-filter' | translate }}"
                        matTooltipPosition="above">
                  <mat-icon>close</mat-icon>
                </button>
              </div>
            </div>
          </div>
        }
      </div>
    </div>
  } @else {
    <span [class.disabled]="disabled" class="no-data-found tb-prompt flex items-center justify-center" translate>filter.no-filters</span>
  }
  <div class="flex flex-row gap-2" [class.!hidden]="disabled">
      <button mat-button mat-stroked-button color="primary"
              (click)="addPredicate(false)"
              type="button"
              matTooltip="{{ 'filter.add-filter' | translate }}"
              matTooltipPosition="above">
        {{ 'action.add' | translate }}
      </button>
      <button mat-button
              color="primary"
              (click)="addPredicate(true)"
              type="button"
              matTooltip="{{ 'filter.add-complex-filter' | translate }}"
              matTooltipPosition="above">
        {{ 'filter.add-complex' | translate }}
      </button>
    </div>
</section>
