<!--

    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]="getTelemetryFromDatabaseConfigForm" class="tb-form-panel no-padding no-border">
  <tb-string-items-list class="mat-block" editable [placeholder]="'rule-node-config.add-timeseries-key' | translate"
                        subscriptSizing="dynamic" required
                        appearance="outline"
                        [requiredText]="'rule-node-config.timeseries-keys-required' | translate"
                        [label]="'rule-node-config.timeseries-keys' | translate" formControlName="latestTsKeyNames"
                        [hint]="'rule-node-config.general-pattern-hint' | translate">
    <div matHintEnd class="see-example" tb-help-popup="{{'rulenode/originator_telemetry_node_fields_templatization'}}"
         hintMode
         tb-help-popup-placement="right"
         trigger-style="letter-spacing:0.25px; font-size:12px"
         [tb-help-popup-style]="{maxWidth: '820px'}"
    trigger-text="{{ 'rule-node-config.key-val.see-examples' | translate }}"></div>
    <mat-icon matSuffix class="help-icon margin-8 cursor-pointer" aria-hidden="false" aria-label="help-icon" color="primary"
              matTooltip="{{ 'rule-node-config.chip-help' | translate: { inputName: 'rule-node-config.timeseries-key' | translate } }}">help
    </mat-icon>
  </tb-string-items-list>
  <div class="tb-form-panel stroked">
    <div class="tb-form-panel-title" translate>rule-node-config.fetch-interval</div>
    <div tb-hint-tooltip-icon="{{ 'rule-node-config.use-metadata-dynamic-interval-tooltip' | translate }}"
         class="tb-form-row no-border no-padding">
      <mat-slide-toggle class="mat-slide" formControlName="useMetadataIntervalPatterns">
        {{ 'rule-node-config.use-metadata-dynamic-interval' | translate }}
      </mat-slide-toggle>
    </div>
    @if (getTelemetryFromDatabaseConfigForm.get('useMetadataIntervalPatterns').value === false) {
      <div formGroupName="interval" class="flex flex-col">
        <div class="flex flex-col gap-0 gt-sm:flex-row gt-sm:gap-4">
          <mat-form-field class="mat-block gt-sm:max-w-50% gt-sm:flex-full" appearance="outline">
            <mat-label>{{ 'rule-node-config.interval-start' | translate }}</mat-label>
            <input type="number" step="1" min="1" max="2147483647" matInput formControlName="startInterval" required>
            @if (getTelemetryFromDatabaseConfigForm.get('interval.startInterval').hasError('required')) {
              <mat-error>
                {{ 'rule-node-config.start-interval-value-required' | translate }}
              </mat-error>
            }
            @if (getTelemetryFromDatabaseConfigForm.get('interval.startInterval').hasError('min')) {
              <mat-error>
                {{ 'rule-node-config.time-value-range' | translate }}
              </mat-error>
            }
            @if (getTelemetryFromDatabaseConfigForm.get('interval.startInterval').hasError('max')) {
              <mat-error>
                {{ 'rule-node-config.time-value-range' | translate }}
              </mat-error>
            }
          </mat-form-field>
          <mat-form-field class="mat-block gt-sm:max-w-50% gt-sm:flex-full" hideRequiredMarker appearance="outline">
            <mat-label>{{ 'rule-node-config.time-unit' | translate }}</mat-label>
            <mat-select formControlName="startIntervalTimeUnit" required>
              @for (timeUnit of timeUnits; track timeUnit) {
                <mat-option [value]="timeUnit">
                  {{ timeUnitsTranslationMap.get(timeUnit) | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
        </div>
        <div class="flex flex-col gap-0 gt-sm:flex-row gt-sm:gap-4">
          <mat-form-field class="mat-block gt-sm:max-w-50% gt-sm:flex-full" appearance="outline">
            <mat-label>{{ 'rule-node-config.interval-end' | translate }}</mat-label>
            <input type="number" step="1" min="1" max="2147483647" matInput formControlName="endInterval" required>
            @if (getTelemetryFromDatabaseConfigForm.get('interval.endInterval').hasError('required')) {
              <mat-error>
                {{ 'rule-node-config.end-interval-value-required' | translate }}
              </mat-error>
            }
            @if (getTelemetryFromDatabaseConfigForm.get('interval.endInterval').hasError('min')) {
              <mat-error>
                {{ 'rule-node-config.time-value-range' | translate }}
              </mat-error>
            }
            @if (getTelemetryFromDatabaseConfigForm.get('interval.endInterval').hasError('max')) {
              <mat-error>
                {{ 'rule-node-config.time-value-range' | translate }}
              </mat-error>
            }
          </mat-form-field>
          <mat-form-field class="mat-block gt-sm:max-w-50% gt-sm:flex-full" hideRequiredMarker appearance="outline">
            <mat-label>{{ 'rule-node-config.time-unit' | translate }}</mat-label>
            <mat-select formControlName="endIntervalTimeUnit" required>
              @for (timeUnit of timeUnits; track timeUnit) {
                <mat-option [value]="timeUnit">
                  {{ timeUnitsTranslationMap.get(timeUnit) | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
        </div>
        <div class="description-block tb-primary-fill" [class.error]="getTelemetryFromDatabaseConfigForm.get('interval').invalid">
          <mat-icon class="description-icon">error_outline</mat-icon>
          <div class="description-text">
            @if (getTelemetryFromDatabaseConfigForm.get('interval').valid) {
              {{ 'rule-node-config.fetch-timeseries-from-to' | translate:
              {
              startInterval: getTelemetryFromDatabaseConfigForm.get('interval.startInterval').value,
              endInterval: getTelemetryFromDatabaseConfigForm.get('interval.endInterval').value,
              startIntervalTimeUnit: getTelemetryFromDatabaseConfigForm.get('interval.startIntervalTimeUnit').value.toLowerCase(),
              endIntervalTimeUnit: getTelemetryFromDatabaseConfigForm.get('interval.endIntervalTimeUnit').value.toLowerCase()
              } }}
            } @else {
              {{ "rule-node-config.fetch-timeseries-from-to-invalid" | translate }}
            }
          </div>
        </div>
      </div>
    } @else {
      <div class="input-block flex flex-col">
        <mat-form-field class="mat-block flex-1" appearance="outline">
          <mat-label>{{ 'rule-node-config.start-interval' | translate }}</mat-label>
          <input matInput formControlName="startIntervalPattern" required>
          @if (getTelemetryFromDatabaseConfigForm.get('startIntervalPattern').hasError('required') ||
            getTelemetryFromDatabaseConfigForm.get('startIntervalPattern').hasError('pattern')) {
            <mat-error>
              {{ 'rule-node-config.start-interval-required' | translate }}
            </mat-error>
          }
        </mat-form-field>
        <mat-form-field class="mat-block flex-1" appearance="outline">
          <mat-label>{{ 'rule-node-config.end-interval' | translate }}</mat-label>
          <input matInput formControlName="endIntervalPattern" required>
          @if (getTelemetryFromDatabaseConfigForm.get('endIntervalPattern').hasError('required') ||
            getTelemetryFromDatabaseConfigForm.get('endIntervalPattern').hasError('pattern')) {
            <mat-error>
              {{ 'rule-node-config.end-interval-required' | translate }}
            </mat-error>
          }
        </mat-form-field>
        <tb-example-hint [hintText]="'rule-node-config.metadata-dynamic-interval-hint'"
                         popupHelpLink="rulenode/originator_telemetry_node_fields_templatization">
        </tb-example-hint>
      </div>
    }
  </div>
  <div class="tb-form-panel stroked">
    <div class="tb-form-panel-title" translate>rule-node-config.fetch-strategy</div>
    <div class="tb-form-panel no-border no-padding item-center">
      <div class="fetch-mod-toggle">
        <tb-toggle-select formControlName="fetchMode" appearance="fill">
          @for (data of headerOptions; track data) {
            <tb-toggle-option [value]="data.value">
              {{ data.name }}
            </tb-toggle-option>
          }
        </tb-toggle-select>
      </div>
      <div class="tb-form-hint tb-primary-fill hint-container">
        {{ deduplicationStrategiesHintTranslations.get(getTelemetryFromDatabaseConfigForm.get('fetchMode').value) | translate }}
      </div>
    </div>
    @if (getTelemetryFromDatabaseConfigForm.get('fetchMode').value === fetchMode.ALL) {
      <div>
        <mat-form-field class="mat-block" hideRequiredMarker appearance="outline"
                        [subscriptSizing]="defaultPaddingEnable() ? 'fixed' : 'dynamic'">
          <mat-label>{{ 'aggregation.function' | translate }}</mat-label>
          <mat-select formControlName="aggregation" required>
            @for (aggregation of aggregations; track aggregation) {
              <mat-option [value]="aggregation">
                {{ aggregationTypesTranslations.get(aggregationTypes[aggregation]) | translate }}
              </mat-option>
            }
          </mat-select>
        </mat-form-field>
        @if (getTelemetryFromDatabaseConfigForm.get('aggregation').value === aggregationTypes.NONE) {
          <div>
            <mat-form-field class="mat-block" hideRequiredMarker appearance="outline">
              <mat-label> {{ "rule-node-config.order-by-timestamp" | translate }} </mat-label>
              <mat-select formControlName="orderBy" required>
                @for (order of samplingOrders; track order) {
                  <mat-option [value]="order">
                    {{ samplingOrdersTranslate.get(order) | translate }}
                  </mat-option>
                }
              </mat-select>
            </mat-form-field>
            <mat-form-field class="mat-block" appearance="outline">
              <mat-label>{{ "rule-node-config.limit" | translate }}</mat-label>
              <input type="number" min="2" max="1000" step="1" matInput formControlName="limit" required>
              <mat-hint>{{ "rule-node-config.limit-hint" | translate }}</mat-hint>
              @if (getTelemetryFromDatabaseConfigForm.get('limit').hasError('required')) {
                <mat-error>
                  {{ 'rule-node-config.limit-required' | translate }}
                </mat-error>
              }
              @if (getTelemetryFromDatabaseConfigForm.get('limit').hasError('min')) {
                <mat-error>
                  {{ 'rule-node-config.limit-range' | translate }}
                </mat-error>
              }
              @if (getTelemetryFromDatabaseConfigForm.get('limit').hasError('max')) {
                <mat-error>
                  {{ 'rule-node-config.limit-range' | translate }}
                </mat-error>
              }
            </mat-form-field>
          </div>
        }
      </div>
    }
  </div>
</section>
