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

<form>
  <PolicyForm
    @form={{form}}
    @partition={{partition}}
    @nspace={{nspace}}
    @item={{item}}
  >
    {{!don't show template selection here, i.e. Service Identity}}
    <BlockSlot @name="template" />
  </PolicyForm>
{{#if (not create) }}
    <DataSource
      @src={{uri '/${partition}/${nspace}/${dc}/tokens/for-policy/${id}'
        (hash
          partition=partition
          nspace=nspace
          dc=dc
          id=(or id '')
        )
      }}
      @onchange={{action (mut items) value="data"}}
    />
    {{#if (gt items.length 0)}}
      <TokenList
        @caption="Applied to the following tokens:"
        @items={{items}}
      />
    {{/if}}
{{/if}}
    <div>
      <Hds::ButtonSet>
        {{#if (and create (can "create tokens")) }}
        {{! we only need to check for an empty name here as ember munges autofocus, once we have autofocus back revisit this}}
          <Hds::Button
            @text='Save'
            type='submit'
            {{ on 'click' (route-action 'create' item)}}
            disabled={{if (or item.isPristine item.isInvalid (eq item.Name '')) 'disabled'}}
          />
        {{ else }}
          {{#if (can "write policy" item=item)}}
            <Hds::Button
              @text='Save'
              type='submit'
              {{ on 'click' (route-action 'update' item)}}
              disabled={{if item.isInvalid 'disabled'}}
            />
          {{/if}}
        {{/if}}
        <Hds::Button
          @text='Cancel'
          @color='secondary'
          type='reset'
          {{ action "cancel" item}}
        />
        {{# if (and (not create) (can "delete policy" item=item) ) }}
          <ConfirmationDialog @message="Are you sure you want to delete this Policy?">
            <BlockSlot @name="action" as |confirm|>
              <Hds::Button
                @text='Delete'
                @color='critical'
                {{action confirm 'delete' item}}
                data-test-delete
              />
            </BlockSlot>
            <BlockSlot @name="dialog" as |execute cancel message|>
              {{#if (gt items.length 0)}}
                <ModalDialog
                  data-test-delete-modal
                  @onclose={{action cancel}}
                  @open={{true}}
                  @aria={{hash
                  label="Policy in Use"
                }}
                >
                  <BlockSlot @name="header">
                    <h2>Policy in Use</h2>
                  </BlockSlot>
                  <BlockSlot @name="body">
                    <p>
                      This Policy is currently in use. If you choose to delete this Policy, it will be removed from the
                      following <strong>{{items.length}} Tokens</strong>:
                    </p>
                    <TokenList @items={{items}} @target="_blank" />
                    <p>
                      This action cannot be undone. {{message}}
                    </p>
                  </BlockSlot>
                  <BlockSlot @name="actions" as |close|>
                    <Hds::ButtonSet>
                      <Hds::Button
                        @text='Yes, Delete'
                        data-test-delete
                        @color='critical'
                        {{action execute}}
                      />
                      <Hds::Button
                        @text='Cancel'
                        @color='secondary'
                        {{action close}}
                      />
                    </Hds::ButtonSet>
                  </BlockSlot>
                </ModalDialog>
              {{else}}
                <DeleteConfirmation @message={{message}} @execute={{execute}} @cancel={{cancel}} />
              {{/if}}
            </BlockSlot>
          </ConfirmationDialog>
        {{/if}}
      </Hds::ButtonSet>
    </div>
</form>
