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

<form {{on "submit" (fn @doSubmit (hash param=@param context=@context nonce=@nonce bits=@bits))}} ...attributes>
  <div class="box is-sideless is-fullwidth is-marginless">
    <NamespaceReminder @mode="perform" @noun="datakey creation" />
    <div class="content has-bottom-margin-l">
      <p>Generate a new high-entropy key and value using <code>{{@key.name}}</code> as the cryptographic key.</p>
    </div>
    <div class="field">
      <label for="param" class="is-label">Output format</label>
      <div class="control is-expanded">
        <div class="select is-fullwidth">
          <select name="param" id="param" onchange={{action (mut @param) value="target.value"}}>
            {{#each (array "plaintext" "wrapped") as |paramOption|}}
              <option selected={{eq @param paramOption}} value={{paramOption}}>
                {{paramOption}}
              </option>
            {{/each}}
          </select>
        </div>
      </div>
    </div>
    {{#if @key.derived}}
      <div class="field">
        <label for="context" class="is-label">
          Context
        </label>
        <div class="field has-addons">
          <div class="control">
            <Input @type="text" id="context" @value={{@context}} class="input" data-test-transit-input="context" />
          </div>
          <div class="control">
            <B64Toggle @value={{@context}} @data-test-transit-b64-toggle="context" />
          </div>
        </div>
      </div>
    {{/if}}
    {{#if (eq @key.convergentEncryptionVersion 1)}}
      <div class="field">
        <label for="nonce" class="is-label">Nonce</label>
        <div class="field has-addons">
          <div class="control">
            <Input @type="text" id="nonce" @value={{@nonce}} class="input" data-test-transit-input="nonce" />
          </div>
          <div class="control">
            <B64Toggle @value={{@nonce}} @data-test-transit-b64-toggle="nonce" />
          </div>
        </div>
      </div>
    {{/if}}
    <div class="field">
      <label for="bits" class="is-label">Bits</label>
      <div class="control is-expanded">
        <div class="select is-fullwidth">
          <select name="bits" id="bits" onchange={{action (mut @bits) value="target.value"}}>
            {{#each (array 128 256 512) as |bitOption|}}
              <option selected={{eq @bits bitOption}} value={{bitOption}}>
                <code>{{bitOption}}</code>
              </option>
            {{/each}}
          </select>
        </div>
      </div>
    </div>
  </div>
  <div class="field is-grouped box is-fullwidth is-bottomless">
    <div class="control">
      <Hds::Button @text="Create datakey" type="submit" />
    </div>
  </div>
</form>
{{#if @isModalActive}}
  <Hds::Modal id="transit-datakey-modal" @onClose={{fn (mut @isModalActive) false}} as |M|>
    <M.Header>
      Copy your generated key
    </M.Header>
    <M.Body>
      {{#if @plaintext}}
        <h2 class="has-text-weight-semibold is-6">Plaintext</h2>
        <p class="sub-text">Plaintext is base64 encoded</p>
        <Hds::Copy::Snippet
          class="has-bottom-margin-m"
          @textToCopy={{@plaintext}}
          @color="secondary"
          @container="#transit-datakey-modal"
          @isFullWidth={{true}}
          @isTruncated={{true}}
          @onError={{(fn
            (set-flash-message "Clipboard copy failed. The Clipboard API requires a secure context." "danger")
          )}}
        />
      {{/if}}
      <h2 class="has-text-weight-semibold is-6">Ciphertext</h2>
      <Hds::Copy::Snippet
        @textToCopy={{@ciphertext}}
        @color="secondary"
        @container="#transit-datakey-modal"
        @isFullWidth={{true}}
        @isTruncated={{true}}
        @onError={{(fn (set-flash-message "Clipboard copy failed. The Clipboard API requires a secure context." "danger"))}}
      />
    </M.Body>
    <M.Footer as |F|>
      <Hds::Button @text="Close" {{on "click" F.close}} />
    </M.Footer>
  </Hds::Modal>
{{/if}}