{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: MPL-2.0
}}

<ListCollection
  class="consul-token-list"
  @items={{@items}}
as |item|>
    <BlockSlot @name="header">
{{#if (eq item.AccessorID @token.AccessorID)}}
        <dl rel="me">
          <dd>
            <Tooltip @position="top-start">
              Your token
            </Tooltip>
          </dd>
        </dl>
{{/if}}
        <a data-test-token={{item.AccessorID}} href={{href-to 'dc.acls.tokens.edit' item.AccessorID}}>{{substr item.AccessorID -8}}</a>
    </BlockSlot>
    <BlockSlot @name="details">
        <dl>
          <dt>Scope</dt>
          <dd>
              {{if item.Local 'local' 'global' }}
          </dd>
        </dl>
        <Consul::Token::Ruleset::List @item={{item}} />
        <dl>
          <dt>Description</dt>
          <dd data-test-description>
              {{or item.Description item.Name}}
          </dd>
        </dl>
    </BlockSlot>
    <BlockSlot @name="actions" as |Actions|>
    {{#if item.hasSecretID}}
      <CopyButton
        @value={{item.SecretID}}
        @name={{t "components.consul.token.secretID"}}
      >
        {{t "components.consul.token.secretID"}}
      </CopyButton>
    {{/if}}
      <Actions as |Action|>
        <Action data-test-edit-action @href={{href-to 'dc.acls.tokens.edit' item.AccessorID}}>
          <BlockSlot @name="label">
{{#if (can "write token" item=item)}}
            Edit
{{else}}
            View
{{/if}}
          </BlockSlot>
        </Action>
{{#if (can "duplicate token" item=item)}}
        <Action data-test-clone-action @onclick={{action @onclone item}}>
          <BlockSlot @name="label">
            Duplicate
          </BlockSlot>
        </Action>
{{/if}}

  {{#if (eq item.AccessorID token.AccessorID)}}
        <Action data-test-logout-action class="dangerous" @onclick={{action @onlogout item}}>
          <BlockSlot @name="label">
            Logout
          </BlockSlot>
          <BlockSlot @name="confirmation" as |Confirmation|>
            <Confirmation class="warning">
              <BlockSlot @name="header">
                Confirm logout
              </BlockSlot>
              <BlockSlot @name="body">
                <p>
                  Are you sure you want to stop using this ACL token? This will log you out.
                </p>
              </BlockSlot>
              <BlockSlot @name="confirm" as |Confirm|>
                <Confirm>Logout</Confirm>
              </BlockSlot>
            </Confirmation>
          </BlockSlot>
        </Action>
  {{else}}
        <Action data-test-use-action @onclick={{action @onuse item}}>
          <BlockSlot @name="label">
            Use
          </BlockSlot>
          <BlockSlot @name="confirmation" as |Confirmation|>
            <Confirmation class="warning">
              <BlockSlot @name="header">
                Confirm use
              </BlockSlot>
              <BlockSlot @name="body">
                <p>
                  Are you sure you want to use this ACL token?
                </p>
              </BlockSlot>
              <BlockSlot @name="confirm" as |Confirm|>
                <Confirm>Use</Confirm>
              </BlockSlot>
            </Confirmation>
          </BlockSlot>
        </Action>
  {{/if}}


{{#if (can "delete token" item=item token=@token)}}
        <Action data-test-delete-action @onclick={{action @ondelete item}} class="dangerous">
          <BlockSlot @name="label">
            Delete
          </BlockSlot>
          <BlockSlot @name="confirmation" as |Confirmation|>
            <Confirmation class="warning">
              <BlockSlot @name="header">
                Confirm delete
              </BlockSlot>
              <BlockSlot @name="body">
                <p>
                  Are you sure you want to delete this token?
                </p>
              </BlockSlot>
              <BlockSlot @name="confirm" as |Confirm|>
                <Confirm>Delete</Confirm>
              </BlockSlot>
            </Confirmation>
          </BlockSlot>
        </Action>
{{/if}}

      </Actions>
    </BlockSlot>
</ListCollection>