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

<TabularCollection class='consul-kv-list' ...attributes @items={{@items}} as |item index|>
  <BlockSlot @name='header'>
    <th>Name</th>
  </BlockSlot>
  <BlockSlot @name='row'>
    <td data-test-kv={{item.Key}} class={{if item.isFolder 'folder' 'file'}}>
      <a href={{href-to (if item.isFolder 'dc.kv.folder' 'dc.kv.edit') item.Key}}>{{right-trim
          (left-trim item.Key @parent.Key)
          '/'
        }}</a>
    </td>
  </BlockSlot>
  <BlockSlot @name='actions' as |index change checked|>
    <PopoverMenu
      @expanded={{if (eq checked index) true false}}
      @onchange={{action change index}}
      @keyboardAccess={{true}}
      as |components api|
    >
      <BlockSlot @name='trigger'>
        More
      </BlockSlot>
      <BlockSlot @name='menu' as |confirm keypressClick|>
        {{#if (can 'write kv' item=item)}}
          <components.MenuItem
            data-test-edit
            @href={{href-to (if item.isFolder 'dc.kv.folder' 'dc.kv.edit') item.Key}}
          >
            <BlockSlot @name='label'>
              {{if item.isFolder 'View' 'Edit'}}
            </BlockSlot>
          </components.MenuItem>
          <components.MenuItem data-test-delete class='dangerous' @onclick={{action @delete item}}>
            <BlockSlot @name='label'>
              Delete
            </BlockSlot>
            <BlockSlot @name='confirm-header'>
              Confirm Delete
            </BlockSlot>
            <BlockSlot @name='confirm-body'>
              <p>
                Are you sure you want to delete this KV entry?
              </p>
            </BlockSlot>
            <BlockSlot @name='confirm-footer' as |Params|>
              <Params.button @text='Delete' {{on 'click' Params.confirmAction}} />
            </BlockSlot>
          </components.MenuItem>
        {{else}}
          <components.MenuItem
            data-test-edit
            @href={{href-to (if item.isFolder 'dc.kv.folder' 'dc.kv.edit') item.Key}}
          >
            <BlockSlot @name='label'>
              View
            </BlockSlot>
          </components.MenuItem>
        {{/if}}
      </BlockSlot>
    </PopoverMenu>
  </BlockSlot>
</TabularCollection>