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

<Identity::EntityNav @identityType={{this.identityType}} @model={{this.model}} />
{{#if this.model.meta.total}}
  {{#each this.model as |item|}}
    <LinkedBlock
      @params={{array "vault.cluster.access.identity.show" item.id "details"}}
      class="list-item-row"
      data-test-identity-row={{item.name}}
    >
      <div class="columns is-mobile">
        <div class="column is-7-tablet is-10-mobile">
          <LinkTo
            @route="vault.cluster.access.identity.show"
            @models={{array item.id "details"}}
            class="is-block has-text-black has-text-weight-semibold"
            data-test-identity-link={{true}}
          >
            <Icon @name="user" class="has-text-grey-light" />
            <span class="has-text-weight-semibold">{{item.name}}</span>
          </LinkTo>
          <div class="has-text-grey is-size-8">
            {{item.id}}
          </div>
        </div>
        <div class="column is-3 is-hidden-mobile">
          {{#if item.aliases.length}}
            {{pluralize item.aliases.length "alias"}}
          {{/if}}
        </div>
        <div class="column has-text-right">
          <Hds::Dropdown @isInline={{true}} @listPosition="bottom-right" as |dd|>
            <dd.ToggleIcon
              @icon="more-horizontal"
              @text="Identity management options"
              @hasChevron={{false}}
              {{on "click" (action "reloadRecord" item)}}
              data-test-popup-menu-trigger
            />
            <dd.Interactive
              @route="vault.cluster.access.identity.show"
              @models={{array item.id "details"}}
            >Details</dd.Interactive>
            {{#if (or item.isReloading item.updatePath.isPending item.aliasPath.isPending)}}
              <dd.Generic class="has-text-center">
                <LoadingDropdownOption />
              </dd.Generic>
            {{else}}
              {{#if item.canAddAlias}}
                {{! entities can always add aliases, internal groups cannot have any and external groups can only have one }}
                {{#if (or (eq this.identityType "entity") (and (eq item.type "external") (not item.alias)))}}
                  <dd.Interactive
                    data-test-popup-menu="create alias"
                    @route="vault.cluster.access.identity.aliases.add"
                    @models={{array (pluralize this.identityType) item.id}}
                  >Create alias</dd.Interactive>
                {{/if}}
              {{/if}}
              {{#if item.canEdit}}
                <dd.Interactive @route="vault.cluster.access.identity.edit" @model={{item.id}}>Edit</dd.Interactive>
                {{#if item.disabled}}
                  <dd.Interactive {{on "click" (action "toggleDisabled" item)}}>Enable</dd.Interactive>
                {{else if (eq this.identityType "entity")}}
                  <dd.Interactive
                    @color="critical"
                    {{on "click" (fn (mut this.entityToDisable) item)}}
                  >Disable</dd.Interactive>
                {{/if}}
              {{/if}}
              {{#if item.canDelete}}
                <dd.Interactive
                  @color="critical"
                  {{on "click" (fn (mut this.itemToDelete) item)}}
                  data-test-popup-menu="delete"
                >Delete</dd.Interactive>
              {{/if}}
            {{/if}}
          </Hds::Dropdown>
        </div>
      </div>
    </LinkedBlock>
  {{/each}}

  <Hds::Pagination::Numbered
    @currentPage={{this.model.meta.currentPage}}
    @currentPageSize={{this.model.meta.pageSize}}
    @route="vault.cluster.access.identity.index"
    {{! The identity routes expect the dynamic URL segment for identity/:item_type to be plural }}
    @model={{pluralize this.identityType}}
    @showSizeSelector={{false}}
    @totalItems={{this.model.meta.total}}
    @queryFunction={{this.paginationQueryParams}}
  />

{{else}}
  <EmptyState
    @title="No {{pluralize this.identityType}} yet"
    @message="A list of {{pluralize
      this.identityType
    }} in this namespace will be listed here. Create your first {{this.identityType}} to get started."
  >
    <Hds::Link::Standalone
      @text="Identity: entities and groups tutorial"
      @href={{doc-link "/vault/tutorials/auth-methods/identity"}}
      @icon="learn-link"
      @iconPosition="trailing"
    />
  </EmptyState>
{{/if}}

{{#if this.entityToDisable}}
  <ConfirmModal
    @confirmMessage="Associated tokens will not be revoked, but cannot be used."
    @confirmTitle="Disable this entity?"
    @onConfirm={{action "toggleDisabled" this.entityToDisable}}
    @onClose={{fn (mut this.entityToDisable) null}}
  />
{{/if}}

{{#if this.itemToDelete}}
  <ConfirmModal
    @color="critical"
    @confirmTitle="Delete this {{this.identityType}}?"
    @onConfirm={{action "delete" this.itemToDelete}}
    @onClose={{fn (mut this.itemToDelete) null}}
  />
{{/if}}