<!--

    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]="createRelationConfigForm" class="tb-form-panel no-padding no-border">
  <div class="tb-form-panel stroked no-padding-bottom">
    <div class="tb-form-panel-title" translate>rule-node-config.relation-parameters</div>
    <div class="flex flex-col">
      <mat-form-field class="mat-block" hideRequiredMarker appearance="outline">
        <mat-label translate>relation.direction</mat-label>
        <mat-select required matInput formControlName="direction">
          @for (type of directionTypes; track type) {
            <mat-option [value]="type">
              {{ directionTypeTranslations.get(type) | translate }}
            </mat-option>
          }
        </mat-select>
      </mat-form-field>
      <tb-relation-type-autocomplete
        appearance="outline"
        required
        formControlName="relationType">
      </tb-relation-type-autocomplete>
    </div>
  </div>

  <div class="tb-form-panel stroked no-padding-bottom">
    <div class="tb-form-panel-title" translate>rule-node-config.target-entity</div>
    <div class="flex flex-row gap-4">
      <tb-entity-type-select
        appearance="outline"
        class="flex-1"
        showLabel
        required
        [allowedEntityTypes]="allowedEntityTypes"
        formControlName="entityType">
      </tb-entity-type-select>

      @if (createRelationConfigForm.get('entityType').value &&
        createRelationConfigForm.get('entityType').value !== entityType.TENANT) {
        <mat-form-field
          appearance="outline"
          class="mat-block flex-1">
          <mat-label>{{ entityTypeNamePatternTranslation.get(createRelationConfigForm.get('entityType').value) | translate }}</mat-label>
          <input required matInput formControlName="entityNamePattern">
        </mat-form-field>
      }

      @if (createRelationConfigForm.get('entityType').value === entityType.DEVICE ||
        createRelationConfigForm.get('entityType').value === entityType.ASSET) {
        <mat-form-field
          appearance="outline"
          class="mat-block flex-1">
          <mat-label translate>rule-node-config.profile-name</mat-label>
          <input matInput formControlName="entityTypePattern">
        </mat-form-field>
      }
    </div>

    @if (createRelationConfigForm.get('entityType').value === entityType.CUSTOMER ||
      createRelationConfigForm.get('entityType').value === entityType.DEVICE ||
      createRelationConfigForm.get('entityType').value === entityType.ASSET) {
      <tb-example-hint [hintText]="'rule-node-config.kv-map-pattern-hint'"
      ></tb-example-hint>
    }

    @if (createRelationConfigForm.get('entityType').value === entityType.CUSTOMER ||
      createRelationConfigForm.get('entityType').value === entityType.DEVICE ||
      createRelationConfigForm.get('entityType').value === entityType.ASSET) {
      <div tb-hint-tooltip-icon="{{ 'rule-node-config.create-entity-if-not-exists-hint' | translate }}"
           style="margin-bottom: 18px"
           class="tb-form-row no-border no-padding">
        <mat-slide-toggle class="mat-slide" formControlName="createEntityIfNotExists">
          {{ 'rule-node-config.create-entity-if-not-exists' | translate }}
        </mat-slide-toggle>
      </div>
    }
  </div>
  <section class="tb-form-panel stroked no-padding">
    <mat-expansion-panel class="tb-settings">
      <mat-expansion-panel-header style="padding: 16px">
        <mat-panel-title translate>rule-node-config.advanced-settings</mat-panel-title>
      </mat-expansion-panel-header>
      <div class="tb-form-panel no-border no-padding-top">
        <div tb-hint-tooltip-icon="{{ 'rule-node-config.remove-current-relations-hint' | translate }}"
             class="tb-form-row no-border no-padding">
          <mat-slide-toggle class="mat-slide" formControlName="removeCurrentRelations">
            {{ 'rule-node-config.remove-current-relations' | translate }}
          </mat-slide-toggle>
        </div>
        <div tb-hint-tooltip-icon="{{ 'rule-node-config.change-originator-to-related-entity-hint' | translate }}"
             class="tb-form-row no-border no-padding">
          <mat-slide-toggle class="mat-slide" formControlName="changeOriginatorToRelatedEntity">
            {{ 'rule-node-config.change-originator-to-related-entity' | translate }}
          </mat-slide-toggle>
        </div>
      </div>
    </mat-expansion-panel>
  </section>
</section>
