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

<form
  {{on
    "submit"
    (fn
      @doSubmit
      (hash
        ciphertext=@ciphertext
        decrypt_padding_scheme=@decrypt_padding_scheme
        encrypt_padding_scheme=@encrypt_padding_scheme
        context=@context
        nonce=@nonce
        key_version=@key_version
      )
    )
  }}
  ...attributes
>
  <div class="box is-sideless is-fullwidth is-marginless">
    <NamespaceReminder @mode="perform" @noun="rewrap" />
    <div class="content has-bottom-margin-l">
      <p>
        You can rewrap the provided ciphertext using the latest version of
        <code>{{@key.name}}</code>
        as the cryptographic key.
      </p>
    </div>
    <KeyVersionSelect @key={{@key}} @onVersionChange={{fn (mut @key_version)}} @key_version={{@key_version}} />
    <div class="field">
      <div class="control is-expanded">
        <JsonEditor @title="Ciphertext" @valueUpdated={{fn (mut @ciphertext)}} @mode="ruby" />
      </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}} @onUpdate={{@updateContext}} data-test-transit-b64-toggle="context" />
          </div>
        </div>
      </div>
    {{/if}}
    {{#if (includes @key.type (array "rsa-2048" "rsa-3072" "rsa-4096"))}}
      <div class="field">
        <label for="decrypt_padding_scheme" class="is-label">Decrypt padding scheme</label>
        <div class="control is-expanded">
          <div class="select is-fullwidth">
            <select
              name="decrypt_padding_scheme"
              id="decrypt_padding_scheme"
              data-test-padding-scheme="decrypt"
              onchange={{action (mut @decrypt_padding_scheme) value="target.value"}}
            >
              {{#each (array "oaep" "pkcs1v15") as |scheme|}}
                <option selected={{eq @decrypt_padding_scheme scheme}} value={{scheme}}>{{scheme}}</option>
              {{/each}}
            </select>
          </div>
        </div>
      </div>
      <div class="field">
        <label for="encrypt_padding_scheme" class="is-label">Encrypt padding scheme</label>
        <div class="control is-expanded">
          <div class="select is-fullwidth">
            <select
              name="encrypt_padding_scheme"
              id="encrypt_padding_scheme"
              data-test-padding-scheme="encrypt"
              onchange={{action (mut @encrypt_padding_scheme) value="target.value"}}
            >
              {{#each (array "oaep" "pkcs1v15") as |scheme|}}
                <option selected={{eq @encrypt_padding_scheme scheme}} value={{scheme}}>{{scheme}}</option>
              {{/each}}
            </select>
          </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}} @onUpdate={{@updateNonce}} data-test-transit-b64-toggle="nonce" />
          </div>
        </div>
      </div>
    {{/if}}
  </div>
  <div class="field box is-fullwidth is-bottomless">
    <div class="content has-bottom-margin-l">
      <p class="help">
        Submitting this form will update the
        <code>ciphertext</code>
        in-place.
      </p>
    </div>
    <div class="control">
      <Hds::Button @text="Rewrap" type="submit" />
    </div>
  </div>
</form>
{{#if @isModalActive}}
  <Hds::Modal id="transit-rewrap-modal" @onClose={{fn (mut @isModalActive) false}} as |M|>
    <M.Header>
      Copy your token
    </M.Header>
    <M.Body>
      <h2 class="title is-6">Ciphertext</h2>
      <Hds::Copy::Snippet
        @textToCopy={{@ciphertext}}
        @color="secondary"
        @container="#transit-rewrap-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}}