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

{{#if (has-feature "Namespaces")}}
  <PageHeader as |p|>
    <p.levelLeft>
      <h1 class="title is-3">
        Namespaces
      </h1>
    </p.levelLeft>
  </PageHeader>

  <Toolbar>
    <ToolbarActions>
      <ToolbarLink @route="vault.cluster.access.namespaces.create" @type="add">
        Create namespace
      </ToolbarLink>
    </ToolbarActions>
  </Toolbar>

  <ListView @items={{this.model}} @itemNoun="namespace" @paginationRouteName="vault.cluster.access.namespaces" as |list|>
    {{#if list.empty}}
      <list.empty>
        <Hds::Link::Standalone
          @icon="learn-link"
          @text="Secure multi-tenancy with namespaces tutorial"
          @href={{doc-link "/vault/tutorials/enterprise/namespaces"}}
        />
      </list.empty>
    {{else}}
      <ListItem as |Item|>
        <Item.content>
          {{list.item.id}}
        </Item.content>
        <Item.menu>
          <Hds::Dropdown @isInline={{true}} @listPosition="bottom-right" as |dd|>
            <dd.ToggleIcon @icon="more-horizontal" @text="More options" @hasChevron={{false}} data-test-popup-menu-trigger />
            {{#let (concat this.currentNamespace (if this.currentNamespace "/") list.item.id) as |targetNamespace|}}
              {{#if (includes targetNamespace this.accessibleNamespaces)}}
                <dd.Generic>
                  <NamespaceLink @targetNamespace={{targetNamespace}} @unparsed={{true}} @class="ns-dropdown-item">
                    Switch to Namespace
                  </NamespaceLink>
                </dd.Generic>
              {{/if}}
            {{/let}}
            <dd.Interactive @text="Delete" @color="critical" {{on "click" (fn (mut this.nsToDelete) list.item)}} />
          </Hds::Dropdown>
          {{#if (eq this.nsToDelete list.item)}}
            <ConfirmModal
              @color="critical"
              @onClose={{fn (mut this.nsToDelete) null}}
              @onConfirm={{action
                (perform
                  Item.callMethod
                  "destroyRecord"
                  this.nsToDelete
                  (concat "Successfully deleted namespace: " this.nsToDelete.id)
                  "There was an error deleting this namespace: "
                  (action "refreshNamespaceList")
                )
              }}
              @confirmTitle="Delete this namespace?"
              @confirmMessage="Any engines or mounts in this namespace will also be removed."
            />
          {{/if}}
        </Item.menu>
      </ListItem>
    {{/if}}
  </ListView>
{{else}}
  <UpgradePage @title="Namespaces" @minimumEdition="Vault Enterprise Pro" />
{{/if}}