<!--

    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.

-->
<div class="tb-dashboard-widget-select">
  @if (selectWidgetMode === 'bundles') {
    @if (widgetsBundle) {
      <ng-container *ngTemplateOutlet="widgets; context:{ fetchFunction: widgetsFetchFunction, filter: widgetsFilter }"></ng-container>
    } @else {
      <tb-scroll-grid
        [columns]="columns"
        [itemSize]="gridWidgetsItemSizeStrategy"
        [fetchFunction]="widgetBundlesFetchFunction"
        [filter]="widgetsBundleFilter"
        [itemCard]="widgetsBundleCard"
        [loadingCell]="widgetLoadingCard"
        [dataLoading]="loadingWidgetBundles"
        [noData]="noWidgetBundles">
      </tb-scroll-grid>
      <ng-template #widgetsBundleCard let-item="item">
        <mat-card class="tb-widget-preview-card flex flex-col gap-2" appearance="raised" (click)="selectBundle($event, item)">
          <div class="title-container">
            <div title="{{item.name}}" class="widget-title">
              {{item.title}}
            </div>
            <div class="title-items">
              @if (isSystem(item)) {
                <div class="widget-type">sys</div>
              }
              <div tb-popover [tbPopoverContent]="item.description" [tbPopoverOverlayStyle]="{maxWidth: '300px'}" tbPopoverShowCloseButton="false" class="info-banner tb-primary-fill"><span>i</span></div>
            </div>
          </div>
          <div class="preview-container">
            <div class="preview-spacer"></div>
            <img class="preview" [src]="item.image | image: {emptyUrl: '/assets/widget-preview-empty.svg'} | async" alt="{{ item.title }}">
          </div>
        </mat-card>
      </ng-template>
      <ng-template #loadingWidgetBundles>
        <div class="tb-absolute-fill flex flex-col items-center justify-center">
          <span class="mat-headline-5" style="padding-bottom: 20px;">
            {{ 'widgets-bundle.loading-widgets-bundles' | translate }}
          </span>
          <mat-spinner strokeWidth="5"></mat-spinner>
        </div>
      </ng-template>
      <ng-template #noWidgetBundles>
        <span translate
        class="mat-headline-5 tb-absolute-fill flex items-center justify-center">widgets-bundle.no-widgets-bundles-text</span>
      </ng-template>
    }
  } @else {
    <ng-container *ngTemplateOutlet="widgets; context:{ fetchFunction: allWidgetsFetchFunction, filter: allWidgetsFilter }"></ng-container>
  }
  <ng-template #widgets let-fetchFunction="fetchFunction" let-filter="filter">
    <tb-scroll-grid
      [columns]="columns"
      [itemSize]="gridWidgetsItemSizeStrategy"
      [fetchFunction]="fetchFunction"
      [filter]="filter"
      [itemCard]="widgetCard"
      [loadingCell]="widgetLoadingCard"
      [dataLoading]="loadingWidgets"
      [noData]="noWidgets">
    </tb-scroll-grid>
    <ng-template #widgetCard let-item="item">
      <mat-card class="tb-widget-preview-card flex flex-col gap-2" appearance="raised" (click)="onWidgetClicked($event, item)">
        <div class="title-container">
          <div title="{{item.name}}" class="widget-title">
            {{item.name}}
          </div>
          <div class="title-items-container">
            <div class="title-items">
              <div class="widget-type">{{ 'widget.' + item.widgetType + '-short' | translate }}</div>
              <div tb-popover [tbPopoverContent]="item.description" [tbPopoverOverlayStyle]="{maxWidth: '300px'}" tbPopoverShowCloseButton="false" class="info-banner tb-primary-fill"><span>i</span></div>
            </div>
            @if (item.deprecated) {
              <div class="widget-deprecated" translate>widget.deprecated</div>
            }
          </div>
        </div>
        <div class="preview-container">
          <div class="preview-spacer"></div>
          <img class="preview" [src]="item.image | image: {emptyUrl: '/assets/widget-preview-empty.svg', preview: item.scada} | async" alt="{{ item.title }}">
        </div>
      </mat-card>
    </ng-template>
    <ng-template #loadingWidgets>
      <div class="tb-absolute-fill flex flex-col items-center justify-center">
        <span class="mat-headline-5" style="padding-bottom: 20px;">
          {{ 'widget.loading-widgets' | translate }}
        </span>
        <mat-spinner color="accent" strokeWidth="5"></mat-spinner>
      </div>
    </ng-template>
    <ng-template #noWidgets>
      <span class="mat-headline-5 tb-absolute-fill flex items-center justify-center">{{(selectWidgetMode === 'bundles' ? 'widgets-bundle.empty' : 'widget.no-widgets-text') | translate}}</span>
    </ng-template>
  </ng-template>

  <ng-template #widgetLoadingCard>
    <mat-card class="tb-widget-preview-card loading-cell flex flex-col gap-2" appearance="raised">
      <div class="title-container">
      </div>
      <div class="preview-container">
        <div class="preview-spacer"></div>
      </div>
    </mat-card>
  </ng-template>
</div>
