{{!
  Copyright IBM Corp. 2016, 2025
  SPDX-License-Identifier: BUSL-1.1
}}

<Hds::AdvancedTable
  @model={{this.paginatedTableData}}
  @columns={{@columns}}
  @isSelectable={{if @selectionKeyField true false}}
  class="has-bottom-margin-s"
  @onSelectionChange={{@onSelectionChange}}
  {{did-update this.resetPagination @data}}
>
  <:body as |B|>
    <B.Tr
      {{! Pass value of selectionKeyField because it should be the unique identifier for that object }}
      @selectionKey={{this.identifier B.data}}
      @selectionAriaLabelSuffix="row {{this.identifier B.data}}"
      data-test-table-row={{B.rowIndex}}
      data-test-list-item={{this.identifier B.data}}
    >
      {{#each @columns as |column|}}
        {{#if (and (eq column.key "popupMenu") (has-block "popupMenu"))}}
          <B.Td data-test-table-data="popupMenu">
            {{yield B.data to="popupMenu"}}
          </B.Td>
        {{else}}
          <B.Td data-test-table-data={{column.key}} class="is-inline text-overflow-ellipsis">
            {{#if column.customTableItem}}
              {{yield B.data to="customTableItem"}}
            {{else}}
              {{#let (get B.data column.key) as |value|}}
                {{! stringify value if it is an array or object, otherwise render directly }}
                {{if (this.isObject value) (stringify value) value}}
              {{/let}}
            {{/if}}
          </B.Td>
        {{/if}}
      {{/each}}
    </B.Tr>
  </:body>
</Hds::AdvancedTable>
{{! WORKAROUND to manually re-render Hds::Pagination::Numbered to force update @currentPage }}
{{#if this.renderPagination}}
  <Hds::Pagination::Numbered
    class="has-bottom-margin-m is-fullwidth"
    @currentPage={{this.currentPage}}
    @currentPageSize={{this.pageSize}}
    @onPageChange={{fn this.handlePaginationChange "currentPage"}}
    @onPageSizeChange={{fn this.handlePaginationChange "pageSize"}}
    @pageSizes={{array 5 10 25 50 100}}
    data-test-pagination
    @totalItems={{@data.length}}
  />
{{/if}}