{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<SecretListHeader
  @isCertTab={{eq this.tab "cert"}}
  @model={{this.backendModel}}
  @baseKey={{this.baseKey}}
  @backendCrumb={{this.backendCrumb}}
  @filter={{this.filter}}
/>
{{#let (options-for-backend this.backendType this.tab) as |options|}}
  {{#if (or this.model.meta.total (not this.isConfigurableTab))}}
    <Toolbar>
      <ToolbarFilters>
        <NavigateInput
          @enterpriseProduct="vault"
          @filterFocusDidChange={{action "setFilterFocus"}}
          @filterDidChange={{action "setFilter"}}
          @filter={{this.filter}}
          @filterMatchesKey={{this.filterMatchesKey}}
          @firstPartialMatch={{this.firstPartialMatch}}
          @baseKey={{get this.baseKey "id"}}
          @shouldNavigateTree={{options.navigateTree}}
          @placeholder={{options.searchPlaceholder}}
          @mode={{if (eq this.tab "cert") "secrets-cert" "secrets"}}
        />
        {{#if this.filterFocused}}
          {{#if this.filterMatchesKey}}
            {{#unless this.filterIsFolder}}
              <p class="input-hint">
                <kbd>Enter</kbd>
                to view
                {{this.filter}}
              </p>
            {{/unless}}
          {{/if}}
          {{#if this.firstPartialMatch}}
            <p class="input-hint">
              <kbd>Tab</kbd>
              to autocomplete
            </p>
          {{/if}}
        {{/if}}
      </ToolbarFilters>

      <ToolbarActions>
        <ToolbarSecretLink
          @secret=""
          @mode="create"
          @backend={{this.backendModel.id}}
          @type="add"
          @queryParams={{hash initialKey=(or this.filter this.baseKey.id) itemType=this.tab}}
          data-test-secret-create={{true}}
        >
          {{options.create}}
        </ToolbarSecretLink>
      </ToolbarActions>
    </Toolbar>
  {{/if}}

  {{#if this.model.meta.total}}
    {{#each this.model as |item|}}
      {{! Because of the component helper cannot use glimmer nested SecretList::Item }}
      {{#let (component options.listItemPartial) as |Component|}}
        <Component
          @item={{item}}
          @backendModel={{this.backendModel}}
          @backendType={{this.backendType}}
          @delete={{action "delete" item "secret"}}
          @itemPath={{concat options.modelPrefix item.id}}
          @itemType={{options.item}}
          @modelType={{@modelType}}
          @options={{options}}
          @toggleZeroAddress={{action "toggleZeroAddress" item this.backendModel}}
          @loadingToggleZeroAddress={{get this (concat "loading-" item.id)}}
        />
      {{/let}}
    {{else}}
      {{! Empty state here means that a filter was applied on top of the results list }}
      <EmptyState
        @title='There are no {{pluralize options.item}} matching "{{this.filter}}". {{if
          this.filterFocused
          'Press ENTER to add one.'
        }}'
      />

    {{/each}}

    {{#if this.model.length}}
      {{! Only show pagination when there are results on the page }}
      <Hds::Pagination::Numbered
        @currentPage={{this.model.meta.currentPage}}
        @currentPageSize={{this.model.meta.pageSize}}
        @route={{concat "vault.cluster.secrets.backend.list" (unless this.baseKey.id "-root")}}
        @model={{this.backendModel.id}}
        @showSizeSelector={{false}}
        @totalItems={{this.model.meta.total}}
        @queryFunction={{this.paginationQueryParams}}
      />
    {{/if}}
  {{else}}
    {{#if (eq this.baseKey.id "")}}
      {{#if (and options.firstStep (not this.tab))}}
        <EmptyState @title="Get started with {{capitalize this.backendType}}" @message={{options.firstStep}} />
      {{else}}
        <EmptyState
          @title="No {{pluralize options.item}} in this backend"
          @message="{{pluralize (capitalize options.item)}}
            in this backend will be listed here.
            {{or options.message (concat 'Add a ' options.item ' to get started.')}}"
        />
      {{/if}}
    {{else}}
      {{#if this.filterIsFolder}}
        <EmptyState
          @title={{if
            (eq this.filter this.baseKey.id)
            (concat "No " (pluralize options.item) ' under "' this.filter '".')
            (concat 'No folders matching "' this.filter '".')
          }}
        >
          <LinkTo @route="vault.cluster.secrets.backend.list-root" @model={{this.backend}} data-test-list-root-link>
            Back to root
          </LinkTo>
        </EmptyState>
      {{else}}
        <EmptyState @title={{(concat "No " (pluralize options.item) ' matching "' this.filter '".')}}>
          <LinkTo @route="vault.cluster.secrets.backend.list-root" @model={{this.backend}} data-test-list-root-link>
            Back to root
          </LinkTo>
        </EmptyState>
      {{/if}}
    {{/if}}
  {{/if}}
{{/let}}