<ng-container *transloco="let t; prefix: 'book.table'">
<p-table
  [value]="books"
  [columns]="visibleColumns"
  [dataKey]="'id'"
  [resizableColumns]="true"
  [reorderableColumns]="true"
  columnResizeMode="expand"
  [rowTrackBy]="trackByBookId"
  [metaKeySelection]="true"
  showGridlines
  stripedRows
  [scrollable]="true"
  [scrollHeight]="scrollHeight"
  [virtualScroll]="true"
  [virtualScrollItemSize]="46"
  [(selection)]="selectedBooks"
  (onHeaderCheckboxToggle)="onHeaderCheckboxToggle($event)"
  (onRowSelect)="onRowSelect($event)"
  (onRowUnselect)="onRowUnselect($event)"
>
  <ng-template pTemplate="header">
    <tr>
      <th>
        <p-tableHeaderCheckbox/>
      </th>
      <th></th>
      <th class="cover-header-cell"></th>
      @for (col of visibleColumns; track col.field) {
        <th pResizableColumn>{{ col.header }}</th>
      }
    </tr>
  </ng-template>

  <ng-template pTemplate="body" let-book>
    @if (book.metadata; as metadata) {
      <tr style="height:46px">
        <td>
          <p-tableCheckbox [value]="book"/>
        </td>
        <td class="cell-center">
          <p-button
            [icon]="isMetadataFullyLocked(metadata) ? 'pi pi-lock' : 'pi pi-lock-open'"
            [severity]="isMetadataFullyLocked(metadata) ? 'danger' : 'success'"
            [text]="true"
            [title]="isMetadataFullyLocked(metadata) ? t('locked') : t('unlocked')"
            size="small"
            [style]="{ width: '1.5rem', height: '1.5rem', padding: '0', fontSize: '0.75rem' }"
            (click)="toggleMetadataLock(metadata)">
          </p-button>
        </td>
        <td>
          <a [routerLink]="urlHelper.getBookUrl(book)">
            <img
              [attr.src]="urlHelper.getThumbnailUrl(metadata.bookId, metadata.coverUpdatedOn)"
              [alt]="t('bookCoverAlt')"
              class="cover-thumbnail"
              tooltipPosition="left"
              [pTooltip]="tooltipContent"
              tooltipStyleClass="cover-tooltip"
            />
          </a>
          <ng-template #tooltipContent>
            <div class="tooltip-cover-container">
              <img
                [attr.src]="urlHelper.getThumbnailUrl(metadata.bookId, metadata.coverUpdatedOn)"
                [alt]="t('bookCoverAlt')"
                class="tooltip-cover-image"
              />
              <em class="tooltip-cover-title">{{ metadata.title }}</em>
            </div>
          </ng-template>
        </td>

        @for (col of visibleColumns; track col.field) {
          @if (col.field === 'readStatus') {
            <td class="cell-status">
              @if (shouldShowStatusIcon(book.readStatus)) {
                <div class="read-status-indicator"
                     [ngClass]="getReadStatusClass(book.readStatus)"
                     [pTooltip]="t('statusPrefix') + getReadStatusTooltip(book.readStatus)"
                     tooltipPosition="top">
                  <i [class]="getReadStatusIcon(book.readStatus)"></i>
                </div>
              }
            </td>
          } @else if (col.field === 'amazonRating' || col.field === 'goodreadsRating' || col.field === 'hardcoverRating' || col.field == 'ranobedbRating') {
            <td class="cell-rating">
              <span class="rating-wrapper">
                <p-rating
                  [(ngModel)]="metadata[col.field]"
                  readonly
                  stars="5"
                  [style.--p-rating-icon-active-color]="getStarColor(metadata[col.field])"
                ></p-rating>
                @if (metadata[col.field] != null) {
                  <span>({{ metadata[col.field]?.toFixed(1) }})</span>
                }
              </span>
            </td>
          } @else {
            <td
              [title]="getCellValue(metadata, book, col.field)"
              class="cell-text">
              @if(['title', 'authors', 'publisher', 'seriesName', 'categories', 'language'].includes(col.field)) {
                @for (item of getCellClickableValue(metadata, book, col.field); track $index; let isLast = $last) {
                  <a [routerLink]="item.url" class="cell-link">
                    {{ item.anchor }}</a>@if (!isLast) {<span>, </span>}
                }
              } @else {
                {{ getCellValue(metadata, book, col.field) }}
              }
            </td>
          }
        }
      </tr>
    }
  </ng-template>
</p-table>
</ng-container>
