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

<SecretListHeader @model={{this.backendModel}}>
  <:actions>
    {{#let
      (options-for-backend this.backendType this.tab) (engines-display-data this.backendType)
      as |options engineDisplayData|
    }}
      <Hds::Dropdown as |D|>
        <D.ToggleButton @text="Manage" @color="secondary" data-test-dropdown="Manage" />
        <D.Interactive
          @icon="settings"
          @route={{if
            engineDisplayData.isConfigurable
            "vault.cluster.secrets.backend.configuration.plugin-settings"
            "vault.cluster.secrets.backend.configuration.general-settings"
          }}
          data-test-popup-menu="Configure"
        >Configure</D.Interactive>
        <D.Interactive
          {{on "click" (fn (mut this.engineToDisable) this.backendModel)}}
          @color="critical"
          @icon="trash"
          data-test-popup-menu="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"
        @model={{this.backendModel}}
        @query={{hash initialKey=(or this.filter this.baseKey.id) itemType=this.tab}}
        data-test-create-secret-link
      />
    {{/let}}
  </:actions>
</SecretListHeader>

{{#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>
    </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 }}
      <Hds::ApplicationState @align="center" class="top-padding-32" as |A|>
        <A.Header
          data-test-empty-state-title
          @title='There are no {{pluralize options.item}} matching "{{this.filter}}". {{if
            this.filterFocused
            'Press ENTER to add one.'
          }}'
          @titleTag="h2"
        />
      </Hds::ApplicationState>

    {{/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))}}
        <Hds::ApplicationState @align="center" class="top-padding-32" as |A|>
          <A.Header data-test-empty-state-title @title="Get started with {{capitalize this.backendType}}" @titleTag="h2" />
          <A.Body data-test-empty-state-message @text={{options.firstStep}} />
        </Hds::ApplicationState>
      {{else}}
        <Hds::ApplicationState @align="center" class="top-padding-32" as |A|>
          <A.Header data-test-empty-state-title @title="No {{pluralize options.item}} in this backend" @titleTag="h2" />
          <A.Body
            data-test-empty-state-message
            @text="{{concat
              (pluralize (capitalize options.item))
              ' in this backend will be listed here. '
              (or options.message (concat 'Add a ' options.item ' to get started.'))
            }}"
          />
        </Hds::ApplicationState>
      {{/if}}
    {{else}}
      {{#if this.filterIsFolder}}
        <Hds::ApplicationState @align="center" class="top-padding-32" as |A|>
          <A.Header
            data-test-empty-state-title
            @title={{if
              (eq this.filter this.baseKey.id)
              (concat "No " (pluralize options.item) ' under "' this.filter '".')
              (concat 'No folders matching "' this.filter '".')
            }}
            @titleTag="h2"
          />
          <A.Body>
            <LinkTo @route="vault.cluster.secrets.backend.list-root" @model={{this.backend}} data-test-list-root-link>
              Back to root
            </LinkTo>
          </A.Body>
        </Hds::ApplicationState>
      {{else}}
        <Hds::ApplicationState @align="center" class="top-padding-32" as |A|>
          <A.Header
            data-test-empty-state-title
            @title={{concat "No " (pluralize options.item) ' matching "' this.filter '".'}}
            @titleTag="h2"
          />
          <A.Body>
            <LinkTo @route="vault.cluster.secrets.backend.list-root" @model={{this.backend}} data-test-list-root-link>
              Back to root
            </LinkTo>
          </A.Body>
        </Hds::ApplicationState>
      {{/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}}