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

{{#if (or (eq this.policyType "acl") (has-feature "Sentinel"))}}
  <PageHeader as |p|>
    <p.levelLeft>
      <h1 class="title is-3">
        {{uppercase this.policyType}}
        Policies
        {{#if (not-eq this.policyType "acl")}}
          <Hds::Badge @text="Sentinel" />
        {{/if}}
      </h1>
    </p.levelLeft>
  </PageHeader>
  <Toolbar>
    {{#if this.model.meta.total}}
      <ToolbarFilters>
        <NavigateInput
          @filterFocusDidChange={{action "setFilterFocus"}}
          @filterDidChange={{action "setFilter"}}
          @filter={{this.filter}}
          @filterMatchesKey={{this.filterMatchesKey}}
          @firstPartialMatch={{this.firstPartialMatch}}
          @extraNavParams={{this.policyType}}
          @placeholder="Filter policies"
          @mode="policy"
        />
        {{#if this.filterFocused}}
          {{#if this.filterMatchesKey}}
            <p class="input-hint">
              <kbd>ENTER</kbd>
              to go to
              <code>{{or this.pageFilter this.filter}}</code>
            </p>
          {{/if}}
          {{#if this.firstPartialMatch}}
            <p class="input-hint">
              <kbd>TAB</kbd>
              to complete
              <code>{{this.firstPartialMatch.id}}</code>
            </p>
          {{/if}}
        {{/if}}
      </ToolbarFilters>
    {{/if}}
    <ToolbarActions>
      <ToolbarLink @route="vault.cluster.policies.create" @type="add" data-test-policy-create-link>
        Create
        {{uppercase this.policyType}}
        policy
      </ToolbarLink>
    </ToolbarActions>
  </Toolbar>
  {{#if this.model.meta.total}}
    {{#each this.model as |item|}}
      {{#if (eq item.id "root")}}
        <div class="list-item-row is-flex" data-test-policy-item>
          <Icon @name="file" class="has-text-grey-light" />
          <div>
            <span class="has-text-weight-semibold has-text-grey" data-test-policy-name>
              {{item.id}}
            </span>
            <p class="help has-text-grey">
              The
              <code>root</code>
              policy does not contain any rules but can do anything within Vault. It should be used with extreme care.
            </p>
          </div>
        </div>
      {{else}}
        <LinkedBlock
          @params={{array "vault.cluster.policy.show" this.policyType item.id}}
          class="list-item-row"
          data-test-policy-link={{item.id}}
        >
          <div class="columns is-mobile">
            <div class="column is-10">
              <LinkTo
                @route="vault.cluster.policy.show"
                @models={{array this.policyType item.id}}
                class="has-text-black has-text-weight-semibold"
                data-test-policy-item={{true}}
              >
                <Icon @name="file" class="has-text-grey-light" />
                <span class="is-underline" data-test-policy-name>{{item.id}}</span>
              </LinkTo>
            </div>
            <div class="column has-text-right">
              <Hds::Dropdown @isInline={{true}} @listPosition="bottom-right" as |dd|>
                <dd.ToggleIcon
                  @icon="more-horizontal"
                  @text="Policy nav menu"
                  @hasChevron={{false}}
                  data-test-popup-menu-trigger
                />
                {{#if item.updatePath.isPending}}
                  <dd.Generic class="has-text-center">
                    <LoadingDropdownOption />
                  </dd.Generic>
                {{else}}
                  {{#if item.canRead}}
                    <dd.Interactive
                      @route="vault.cluster.policy.show"
                      @models={{array this.policyType item.id}}
                      data-test-policy-link="show"
                    >Details</dd.Interactive>
                  {{/if}}
                  {{#if item.canEdit}}
                    <dd.Interactive
                      @route="vault.cluster.policy.edit"
                      @models={{array this.policyType item.id}}
                      data-test-policy-link="edit"
                    >Edit</dd.Interactive>
                  {{/if}}
                  {{#if (and item.canDelete (not-eq item.name "default"))}}
                    <dd.Interactive
                      @color="critical"
                      data-test-confirm-action-trigger
                      {{on "click" (fn (mut this.policyToDelete) item)}}
                    >Delete</dd.Interactive>
                  {{/if}}
                {{/if}}
              </Hds::Dropdown>
            </div>
          </div>
        </LinkedBlock>
      {{/if}}
    {{else}}
      <EmptyState @title="No policies matching &quot;{{this.pageFilter}}&quot;" />
    {{/each}}
    {{#unless this.pageFilter}}
      <Hds::Pagination::Numbered
        @currentPage={{this.model.meta.currentPage}}
        @currentPageSize={{this.model.meta.pageSize}}
        @route="vault.cluster.policies.index"
        @showSizeSelector={{false}}
        @totalItems={{this.model.meta.total}}
        @queryFunction={{this.paginationQueryParams}}
      />
    {{/unless}}
  {{else}}
    <EmptyState
      @title="No {{uppercase this.policyType}} policies yet"
      @message="A list of policies will be listed here. Create your first {{uppercase
        this.policyType
      }} policy to get started."
    >
      {{#if (eq this.policyType "acl")}}
        <Hds::Link::Standalone
          @iconPosition="trailing"
          @icon="learn-link"
          @text="Getting started with policies"
          @href={{doc-link "/vault/tutorials/get-started/introduction-policies"}}
        />
      {{else}}
        <Hds::Link::Standalone
          @iconPosition="trailing"
          @icon="learn-link"
          @text="Sentinel policies tutorial"
          @href={{doc-link "/vault/tutorials/policies/sentinel"}}
        />
      {{/if}}
    </EmptyState>
  {{/if}}
{{else}}
  <UpgradePage @title="Sentinel" @minimumEdition="Vault Enterprise Premium" />
{{/if}}

{{#if this.policyToDelete}}
  <ConfirmModal
    @color="critical"
    @confirmTitle="Delete this policy?"
    @confirmMessage="This will permanently delete this policy and may affect access to some data."
    @onClose={{fn (mut this.policyToDelete) null}}
    @onConfirm={{action "deletePolicy" this.policyToDelete}}
  />
{{/if}}