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

<form>
  <RoleForm
    @form={{@form}}
    @item={{@item}}
    @dc={{@dc}}
    @nspace={{@nspace}}
    @partition={{@partition}}
  />
  {{#if (not @create)}}
    <DataSource
      @src={{uri '/${partition}/${nspace}/${dc}/tokens/for-role/${id}'
                 (hash
                   partition=@partition
                   nspace=@nspace
                   dc=@dc
                   id=(if @item @item.ID '')
                 )
      }}
      as |loader|>
      {{#if (gt loader.data.length 0)}}
        <h2>Where is this role used?</h2>
        <p>
          We're only able to show information for the primary datacenter and the current datacenter. This list may not
          show every case where this role is applied.
        </p>
        <TokenList @caption="Tokens" @items={{loader.data}} />
      {{/if}}
    </DataSource>
  {{/if}}
  <div>
    <Hds::ButtonSet>
      {{#if (and @create (can "create roles")) }}
        <Hds::Button
          @text='Save'
          type='submit'
          {{on 'click' @onCreate}}
          disabled={{or @item.isPristine @item.isInvalid (eq @item.Name '')}}
        />
      {{ else }}
        {{#if (can "write role" item=@item)}}
          <Hds::Button
            @text='Save'
            type='submit'
            {{on 'click' @onUpdate}}
            disabled={{@item.isInvalid}}
          />
        {{/if}}
      {{/if}}
      <Hds::Button
        @text='Cancel'
        @color='secondary'
        type='reset'
        {{on 'click' @onCancel}}
      />

      {{# if (and (not @create) (can "delete role" item=@item) ) }}
        <ConfirmationDialog @message="Are you sure you want to delete this Role?">
          <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
                @onclose={{cancel}}
                @aria={{hash
                label="Role in Use"
              }}
              >
                <BlockSlot @name="header">
                  <h2>Role in Use</h2>
                </BlockSlot>
                <BlockSlot @name="body">
                  <p>
                    This Role is currently in use. If you choose to delete this Role, 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'
                      @color='critical'
                      data-test-delete
                      {{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>