<!--

    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="name-value-map flex flex-col" [formGroup]="attributesValueFormGroup">
  <div class="flex flex-row gap-2" style="max-height: 40px; margin-top: 8px;">
    <label class="tb-title no-padding max-w-40% flex-full" style="min-width: 230px;" translate>device-profile.lwm2m.attribute-name</label>
    <label class="tb-title no-padding max-w-60% flex-full" translate>device-profile.lwm2m.attribute-value</label>
    <span [class.!hidden]="disabled" style="width: 40px;"></span>
  </div>
  @for (nameValueControl of attributesValueFormArray().controls; track nameValueControl) {
    <div class="map-list flex flex-col"
         [formGroup]="nameValueControl">
      <div class="flex flex-row items-center justify-start gap-2">
        <mat-form-field class="max-w-40% flex-full" appearance="outline">
          <mat-select formControlName="name" required>
            @for (attributeName of attributeNames; track attributeName) {
              <mat-option [value]="attributeName"
                          [disabled]="isDisabledAttributeName(attributeName, $index)">
                {{ attributeNameTranslationMap.get(attributeName) | translate }}
              </mat-option>
            }
          </mat-select>
          @if (nameValueControl.get('name').hasError('required')) {
            <mat-error>
              {{ 'device-profile.lwm2m.attribute-name-required' | translate }}
            </mat-error>
          }
        </mat-form-field>
        <mat-form-field class="max-w-60% flex-full" appearance="outline">
          <input formControlName="value" matInput required type="number"
                 placeholder="{{ 'key-val.value' | translate }}">
          @if (nameValueControl.get('value').hasError('required')) {
            <mat-error class="flex flex-row">
              {{ 'device-profile.lwm2m.attribute-value-required' | translate }}
            </mat-error>
          }
          @if (nameValueControl.get('value').hasError('min') ||
            nameValueControl.get('value').hasError('pattern')) {
            <mat-error class="flex flex-row">
              {{ 'device-profile.lwm2m.attribute-value-pattern' | translate }}
            </mat-error>
          }
        </mat-form-field>
        @if (!disabled) {
          <button
            mat-icon-button color="primary" style="min-width: 40px;"
            type="button"
            (click)="removeKeyVal($index)"
            matTooltip="{{ 'device-profile.lwm2m.remove-attribute' | translate }}"
            matTooltipPosition="above">
            <mat-icon>close</mat-icon>
          </button>
        }
      </div>
    </div>
  }
  <div [class.!hidden]="attributesValueFormArray().length" class="map-list flex items-center justify-center" translate>device-profile.lwm2m.no-attributes-set</div>
  @if (!disabled && isAddEnabled) {
    <div style="margin-top: 9px;">
      <button mat-stroked-button color="primary"
              [disabled]="isLoading$ | async"
              type="button"
              (click)="addKeyVal()">
        <mat-icon class="button-icon">add_circle_outline</mat-icon>
        {{ 'device-profile.lwm2m.add-attribute' | translate }}
      </button>
    </div>
  }
</section>
