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

<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 (and (not @create) @item @item.ID)}}
    <DataSource
      @src={{uri '/${partition}/${nspace}/${dc}/tokens/for-policy/${id}'
        (hash
          partition=@partition
          nspace=@nspace
          dc=@dc
          id=(or @id '')
        )
      }}
      @onchange={{@onItemsChange}}
    />
    {{#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' @onCreate}}
            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' @onUpdate}}
              disabled={{if @item.isInvalid 'disabled'}}
            />
          {{/if}}
        {{/if}}
        <Hds::Button
          @text='Cancel'
          @color='secondary'
          type='reset'
          {{ on 'click' @onCancel}}
        />
        {{# 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'
                {{on 'click' confirm}}
                data-test-delete
              />
            </BlockSlot>
            <BlockSlot @name="dialog" as |execute cancel message|>
              {{#if (gt @items.length 0)}}
                <ModalDialog
                  data-test-delete-modal
                  @onclose={{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'
                        {{on 'click' (queue execute (fn @onDelete @item))}}
                      />
                      <Hds::Button
                        @text='Cancel'
                        @color='secondary'
                        {{on 'click' close}}
                      />
                    </Hds::ButtonSet>
                  </BlockSlot>
                </ModalDialog>
              {{else}}
                <DeleteConfirmation @message={{message}} @execute={{queue execute (fn @onDelete @item)}} @cancel={{cancel}} />
              {{/if}}
            </BlockSlot>
          </ConfirmationDialog>
        {{/if}}
      </Hds::ButtonSet>
    </div>
</form>