<!--

    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 [formGroup]="relationsQueryFormGroup" class="tb-form-panel stroked">
  <div class="tb-form-panel-title tb-required" translate>rule-node-config.relations-query</div>
  <section>
    <div class="flex flex-row gap-4">
      <mat-form-field class="mat-block max-w-50% flex-full" style="min-width: 100px;" hideRequiredMarker appearance="outline">
        <mat-label translate>relation.direction</mat-label>
        <mat-select required formControlName="direction">
          @for (type of directionTypes; track type) {
            <mat-option [value]="type">
              {{ directionTypeTranslations.get(type) | translate }} <span translate>rule-node-config.relations-query-config-direction-suffix</span>
            </mat-option>
          }
        </mat-select>
      </mat-form-field>
      <mat-form-field floatLabel="always" class="mat-block max-w-50% flex-full" appearance="outline">
        <mat-label translate>rule-node-config.max-relation-level</mat-label>
        <input matInput
               type="text"
               pattern="[0-9]*"
               min="1"
               inputmode="numeric"
               placeholder="{{ 'rule-node-config.unlimited-level' | translate }}"
               formControlName="maxLevel">
        @if (relationsQueryFormGroup.get('maxLevel').hasError('min')) {
          <mat-error>
            {{ 'rule-node-config.max-relation-level-error' | translate }}
          </mat-error>
        }
        @if (relationsQueryFormGroup.get('maxLevel').invalid) {
          <mat-error>
            {{ 'rule-node-config.max-relation-level-invalid' | translate }}
          </mat-error>
        }
      </mat-form-field>
    </div>
    @if (relationsQueryFormGroup.get('maxLevel').value > 1) {
      <div tb-hint-tooltip-icon="{{ 'rule-node-config.last-level-relation-tooltip' | translate }}"
           class="tb-form-row no-border no-padding last-level-slide-toggle">
        <mat-slide-toggle class="mat-slide" formControlName="fetchLastLevelOnly">
          {{ 'alias.last-level-relation' | translate }}
        </mat-slide-toggle>
      </div>
    }
  </section>
  <section [formGroup]="relationsQueryFormGroup" class="tb-form-panel stroked">
    <div class="tb-form-panel-title" translate>relation.relation-filters</div>
    <tb-relation-filters
      formControlName="filters">
    </tb-relation-filters>
  </section>
</section>
