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

{{! private_key is only available after initial save }}
{{#if this.generatedKey.privateKey}}
  <Page::PkiKeyDetails
    @key={{this.generatedKey}}
    @canDelete={{this.generatedKey.canDelete}}
    @canEdit={{this.generatedKey.canEdit}}
  />
{{else}}
  <form {{on "submit" (perform this.save)}}>
    <div class="box is-sideless is-fullwidth is-marginless">
      <MessageError @errorMessage={{this.errorBanner}} class="has-top-margin-s" />
      <NamespaceReminder @mode={{if @model.isNew "generate" "update"}} @noun="PKI key" />
      {{#if @model.isNew}}
        {{#each @model.formFieldGroups as |fieldGroup|}}
          {{#each-in fieldGroup as |group fields|}}
            {{#if (eq group "Key parameters")}}
              <PkiKeyParameters @model={{@model}} @fields={{fields}} @modelValidations={{this.modelValidations}} />
            {{else}}
              {{#each fields as |attr|}}
                <FormField
                  data-test-field={{attr}}
                  @attr={{attr}}
                  @model={{@model}}
                  @modelValidations={{this.modelValidations}}
                  @showHelpText={{false}}
                />
              {{/each}}
            {{/if}}
          {{/each-in}}
        {{/each}}
      {{else}}
        {{! only key name is edit-able }}
        {{#let (find-by "name" "keyName" @model.formFields) as |keyName|}}
          <FormField data-test-field={{keyName}} @attr={{keyName}} @model={{@model}} @showHelpText={{false}} />
        {{/let}}
        {{#let (find-by "name" "keyType" @model.formFields) as |keyType|}}
          <ReadonlyFormField @attr={{keyType}} @value={{@model.keyType}} />
        {{/let}}
      {{/if}}
    </div>
    <Hds::ButtonSet class="has-top-padding-s">
      <Hds::Button
        @text={{if @model.isNew "Generate key" "Edit key"}}
        @icon={{if this.save.isRunning "loading"}}
        type="submit"
        disabled={{this.save.isRunning}}
        data-test-submit
      />
      <Hds::Button
        @text="Cancel"
        @color="secondary"
        disabled={{this.save.isRunning}}
        {{on "click" @onCancel}}
        data-test-cancel
      />
    </Hds::ButtonSet>
    {{#if this.invalidFormAlert}}
      <div class="control">
        <AlertInline
          @type="danger"
          class="has-top-padding-s"
          @message={{this.invalidFormAlert}}
          data-test-pki-key-validation-error
        />
      </div>
    {{/if}}
  </form>
{{/if}}