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

<SecretListHeader @model={{this.backendModel}} />
{{#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>

      {{! Old Engines will keep the existing toolbar actions }}
      {{#if this.isOldEngine}}
        <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-create-secret-link
          >
            {{options.create}}
          </ToolbarSecretLink>
        </ToolbarActions>
      {{/if}}
    </Toolbar>
  {{/if}}

  {{! Display CTA buttons if isOldEngine flag is not set }}
  {{#unless this.isOldEngine}}
    <div class="flex column-gap-16 top-right-absolute has-top-margin-s">
      {{! TODO: Hook dropdown actions to the appropriate routes & actions }}
      <Hds::Dropdown as |D|>
        <D.ToggleButton @text="Manage" @color="secondary" data-test-manage-dropdown />
        <D.Interactive
          @icon="settings"
          @route="vault.cluster.secrets.backend.configuration.index"
          data-test-manage-dropdown-item="Configure"
        >Configure</D.Interactive>
        <D.Interactive
          {{on "click" (fn (mut this.engineToDisable) this.backendModel)}}
          @color="critical"
          @icon="trash"
          data-test-manage-dropdown-item="Delete"
        >Delete</D.Interactive>
      </Hds::Dropdown>

      <Hds::Button
        @text={{options.create}}
        @icon="plus"
        {{! This route would be set in secret-link.js but we're using an HDS button instead of the ToolbarSecretLink wrapper so we're explicitly setting it here  }}
        @route="vault.cluster.secrets.backend.create-root"
        @query={{hash initialKey=(or this.filter this.baseKey.id) itemType=this.tab}}
        {{! TODO: rename this test identifier when updating tests }}
        data-test-create-secret-link
      />
    </div>
  {{/unless}}

  {{#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")}}
        @models={{if this.baseKey.id (array this.backendModel.id this.baseKey.id) (array 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}}

{{#if this.engineToDisable}}
  <ConfirmModal
    @color="critical"
    @confirmMessage="Any data in this engine will be permanently deleted."
    @confirmTitle="Disable engine?"
    @onClose={{fn (mut this.engineToDisable) null}}
    @onConfirm={{perform this.disableEngine this.engineToDisable}}
  />
{{/if}}