<!--

    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-rate-limits-form" [formGroup]="rateLimitsListFormGroup">
  @for (rateLimit of rateLimitsFormArray.controls; track rateLimit) {
    <div class="flex-1" [formGroup]="rateLimit">
      @if($index > 0) {
        <div class="tb-rate-limits-operation" translate>tenant-profile.rate-limits.and-also-less-than</div>
      }
      <div class="flex flex-1 flex-row gap-2">
        <mat-form-field hideRequiredMarker appearance="outline" class="mat-block flex-1" subscriptSizing="dynamic">
          <mat-label translate>tenant-profile.rate-limits.number-of-messages</mat-label>
          <input matInput placeholder="{{ 'tenant-profile.rate-limits.number-of-messages' | translate }}"
                 type="number" min="1" step="1" formControlName="value" required>
          <mat-hint></mat-hint>
          @if (rateLimit.get('value').hasError('required')) {
            <mat-error translate>tenant-profile.rate-limits.number-of-messages-required</mat-error>
          } @else if(rateLimit.get('value').hasError('min')) {
            <mat-error translate>tenant-profile.rate-limits.number-of-messages-min</mat-error>
          }
        </mat-form-field>
        <mat-form-field hideRequiredMarker appearance="outline" class="mat-block flex-1" subscriptSizing="dynamic">
          <mat-label translate>tenant-profile.rate-limits.per-seconds</mat-label>
          <input matInput placeholder="{{ 'tenant-profile.rate-limits.per-seconds' | translate }}"
                 type="number" min="1" step="1" formControlName="time" required>
          <mat-hint></mat-hint>
          @if (rateLimit.get('time').hasError('required')) {
            <mat-error translate>tenant-profile.rate-limits.per-seconds-required</mat-error>
          } @else if (rateLimit.get('time').hasError('min')) {
            <mat-error translate>tenant-profile.rate-limits.per-seconds-min</mat-error>
          } @else if (rateLimit.get('time').hasError('duplicateTime')) {
            <mat-error translate>tenant-profile.rate-limits.per-seconds-duplicate</mat-error>
          }
        </mat-form-field>
        @if (!rateLimitsFormArray.disabled) {
          <button mat-icon-button type="button" color="primary"
                  class="tb-rate-limits-button"
                  (click)="removeRateLimits($index)"
                  [disabled]="disabled"
                  [matTooltip]="'tenant-profile.rate-limits.remove-limit' | translate" matTooltipPosition="above">
            <mat-icon>remove_circle_outline</mat-icon>
          </button>
        }
      </div>
    </div>
  }
  @if (!rateLimitsFormArray.disabled) {
    <button mat-raised-button color="primary"
            (click)="addRateLimits()"
            [disabled]="disabled"
            [matTooltip]="'tenant-profile.rate-limits.add-limit' | translate" matTooltipPosition="above">
      <span translate>tenant-profile.rate-limits.add-limit</span>
    </button>
  }
  <div class="tb-rate-limits-preview flex flex-1 flex-col gap-2" [class.tb-rate-limits-preview-short]="!disabled">
    <span translate>tenant-profile.rate-limits.preview</span>
    <tb-rate-limits-text [rateLimitsArray]="rateLimitsArray" [disabled]="disabled"></tb-rate-limits-text>
  </div>
</section>
