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

<form
  {{on
    "submit"
    (fn
      @doSubmit
      (hash
        input=@trackedInput
        signature=@signature
        signature_algorithm=@signature_algorithm
        hmac=@hmac
        hash_algorithm=@hash_algorithm
        context=@context
        prehashed=@prehashed
        encodedBase64=@encodedBase64
      )
    )
  }}
  ...attributes
>
  <div class="box is-sideless is-fullwidth is-marginless">
    <div class="content has-bottom-margin-l">
      <p>Check whether the provided signature is valid for the given data.</p>
    </div>
    <div class="field">
      <div class="control is-relative">
        <JsonEditor
          @title="Input"
          @value={{@trackedInput}}
          @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>
    {{#if (and @key.supportsSigning @key.derived (not @hmac))}}
      <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 @key.supportsSigning}}
      <div class="columns is-5">
        <div class="column is-one-third">
          <div class="field">
            <label for="verification" class="is-label">Verification Type</label>
            <div class="control is-expanded">
              <div class="select is-fullwidth">
                <select
                  name="verification"
                  id="verification"
                  onchange={{queue
                    (action (mut @verification) value="target.value")
                    (fn @clearSpecificProps (array "hmac" "signature"))
                  }}
                >
                  {{#each (array "Signature" "HMAC") as |type|}}
                    <option
                      selected={{if @verification (eq @verification type) (if @hmac (eq type "HMAC") (eq type "Signature"))}}
                      value={{type}}
                    >
                      {{type}}
                    </option>
                  {{/each}}
                </select>
              </div>
            </div>
          </div>
          <div class="field">
            <div class="level is-mobile">
              <div class="level-left">
                <label for="hash_algorithm" class="is-label">Hash Algorithm</label>
              </div>
              <div class="level-right">
                {{#if (not-eq @verification "HMAC")}}
                  <div class="control is-flex">
                    <Input
                      id="prehashed"
                      @type="checkbox"
                      name="prehashed"
                      class="toggle is-success is-small"
                      @checked={{@prehashed}}
                    />
                    <label for="prehashed" class="has-text-weight-bold is-size-8">Prehashed</label>
                  </div>
                {{/if}}
              </div>
            </div>
            <div class="control is-expanded">
              <div class="select is-fullwidth">
                <select
                  name="hash_algorithm"
                  id="hash_algorithm"
                  onchange={{action (mut @hash_algorithm) value="target.value"}}
                >
                  {{#each (sha2-digest-sizes) as |algo|}}
                    <option selected={{if @hash_algorithm (eq @hash_algorithm algo) (eq algo "sha2-256")}} value={{algo}}>
                      <code>{{algo}}</code>
                    </option>
                  {{/each}}
                </select>
              </div>
            </div>
          </div>
          {{#if (and @keyIsRSA (or (and @verification (eq @verification "Signature")) @signature))}}
            <div class="field">
              <label for="signature_algorithm" class="is-label">Signature Algorithm</label>
              <div class="control is-expanded">
                <div class="select is-fullwidth">
                  <select
                    name="signature_algorithm"
                    id="signature_algorithm"
                    data-test-signature-algorithm="true"
                    onchange={{action (mut @signature_algorithm) value="target.value"}}
                  >
                    {{#each (array "pss" "pkcs1v15") as |sigAlgo|}}
                      <option
                        selected={{if @signature_algorithm (eq @signature_algorithm sigAlgo) (eq sigAlgo "pss")}}
                        value={{sigAlgo}}
                      >
                        {{sigAlgo}}
                      </option>
                    {{/each}}
                  </select>
                </div>
              </div>
            </div>
          {{/if}}

        </div>
        <div class="column is-two-thirds is-flex-column">
          {{#if (or (and @verification (eq @verification "HMAC")) @hmac)}}
            <div class="field is-flex-column is-flex-grow-1">
              <div class="control is-flex-column is-flex-grow-1">
                <JsonEditor @title="HMAC" @value={{@hmac}} @valueUpdated={{fn (mut @hmac)}} @mode="ruby" />
              </div>
            </div>
          {{else}}
            <div class="field is-flex-column is-flex-grow-1">
              <div class="control is-flex-column is-flex-grow-1">
                <JsonEditor @title="Signature" @value={{@signature}} @valueUpdated={{fn (mut @signature)}} @mode="ruby" />
              </div>
            </div>
          {{/if}}
        </div>
      </div>
    {{else}}
      <div class="field">
        <div class="control">
          <JsonEditor @title="HMAC" @value={{@hmac}} @valueUpdated={{fn (mut @hmac)}} @mode="ruby" />
        </div>
      </div>
      <div class="field">
        <label for="hash_algorithm" class="is-label">Algorithm</label>
        <div class="control is-expanded">
          <div class="select is-fullwidth">
            <select name="hash_algorithm" id="hash_algorithm" onchange={{action (mut @hash_algorithm) value="target.value"}}>
              {{#each (array "sha2-224" "sha2-256" "sha2-384" "sha2-512") as |algo|}}
                <option selected={{if @hash_algorithm (eq @hash_algorithm algo) (eq algo "sha2-256")}} value={{algo}}>
                  <code>{{algo}}</code>
                </option>
              {{/each}}
            </select>
          </div>
        </div>
      </div>
    {{/if}}
  </div>
  <div class="field is-grouped box is-fullwidth is-bottomless">
    <div class="control">
      <Hds::Button @text="Verify" @icon={{if @submitIsRunning "loading"}} type="submit" disabled={{@submitIsRunning}} />
    </div>
  </div>
</form>
{{#if @isModalActive}}
  <Hds::Modal id="transit-verify-modal" @size="small" @onClose={{fn (mut @isModalActive) false}} as |M|>
    <M.Header>
      Results
      <Hds::Badge
        @text={{if @valid "Valid" "Not Valid"}}
        @size="large"
        @color={{if @valid "success" "critical"}}
        @icon={{if @valid "check-circle" "x-circle"}}
      />
    </M.Header>
    <M.Body>
      The input is
      {{if @valid "valid" "not valid"}}
      for the given
      {{if @signature "signature." "HMAC."}}
    </M.Body>
    <M.Footer as |F|>
      <Hds::Button @text="Close" {{on "click" F.close}} />
    </M.Footer>
  </Hds::Modal>
{{/if}}