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

<form {{on "submit" (fn @doSubmit (hash input=@trackedInput algorithm=@algorithm key_version=@key_version))}} ...attributes>
  <div class="box is-sideless is-fullwidth is-marginless">
    <NamespaceReminder @mode="perform" @noun="HMAC creation" />
    <div class="content has-bottom-margin-l">
      <p>
        Generate the digest of given data using the specified hash algorithm and
        <code>{{@key.name}}</code>
        as the named key.
      </p>
    </div>
    <KeyVersionSelect @key={{@key}} @onVersionChange={{fn (mut @key_version)}} @key_version={{@key_version}} />
    <div class="field">
      <div id="input-control" class="control is-relative">
        <JsonEditor @title="Input" @valueUpdated={{fn (mut @trackedInput)}} @mode="ruby" @data-test-transit-input="input" />
      </div>
    </div>
    <div class="field">
      <Input
        @type="checkbox"
        id="encodedBase64"
        @checked={{@encodedBase64}}
        {{on "change" @toggleEncodeBase64}}
        data-test-transit-input="encodedBase64"
      />
      <label for="encodedBase64">This data is already encoded in base64</label>
    </div>
    <div class="field">
      <label for="algorithm" class="is-label">Hash Algorithm</label>
      <div class="control is-expanded">
        <div class="select is-fullwidth">
          <select name="algorithm" id="algorithm" onchange={{action (mut @algorithm) value="target.value"}}>
            {{#each (sha2-digest-sizes) as |algo|}}
              <option selected={{if @algorithm (eq @algorithm algo)}} value={{algo}}>
                <code>{{algo}}</code>
              </option>
            {{/each}}
          </select>
        </div>
      </div>
    </div>
  </div>
  <div class="field is-grouped box is-fullwidth is-bottomless">
    <div class="control">
      <Hds::Button @text="HMAC" type="submit" data-test-submit />
    </div>
  </div>
</form>
{{#if @isModalActive}}
  <Hds::Modal id="transit-hmac-modal" @onClose={{fn (mut @isModalActive) false}} as |M|>
    <M.Header>
      Copy your unwrapped data
    </M.Header>
    <M.Body>
      <h2 class="title is-6">HMAC</h2>
      <Hds::Copy::Snippet
        @textToCopy={{@hmac}}
        @color="secondary"
        @container="#transit-hmac-modal"
        @isFullWidth={{true}}
        @isTruncated={{true}}
        @onError={{(fn (set-flash-message "Clipboard copy failed. The Clipboard API requires a secure context." "danger"))}}
        data-test-encrypted-value="hmac"
      />
    </M.Body>
    <M.Footer as |F|>
      <Hds::Button @text="Close" {{on "click" F.close}} />
    </M.Footer>
  </Hds::Modal>
{{/if}}