{{ $contextType := printf "%T" . }}
{{ $isShortcode := (eq $contextType "*hugolib.ShortcodeWithPage") }}

{{/* ---------------------------------------------------------------------------
     NEWSLETTER FORM CONFIGURATION
     --------------------------------------------------------------------------- */}}
{{- /* Form action and method */}}
{{- $formAction := "" -}}
{{- $formMethod := "" -}}
{{- if $isShortcode }}
    {{- $formAction = .Get "form_action" | default .Site.Data.homepage.newsletter.form.action -}}
    {{- $formMethod = .Get "form_method" | default .Site.Data.homepage.newsletter.form.method -}}
{{ else }}
    {{- $formAction = .Site.Data.homepage.newsletter.form.action -}}
    {{- $formMethod = .Site.Data.homepage.newsletter.form.method -}}
{{ end }}

{{/*
  sectionId: Optional argument to override the default HTML id for this section. If not provided, the default id is used. */}}
{{ $sectionId := "newsletter" }}
{{ with .Get "sectionId" }}
  {{ $sectionId = . }}
{{ end }}

<section {{if $sectionId}} id="{{ $sectionId }}"{{end}} class="section section--cta">
  <div class="container d-flex justify-content-center align-items-center">
    <div class="row">
      <div class="col-12 text-center">
        <h2>
          {{ if $isShortcode }} 
            {{ .Get "newsletter_title" }} 
          {{ else }} 
            {{ i18n "newsletter_title" }}
          {{ end }}
        </h2>
        <div class="rad-subscription-group">
          <form
            id="rad-subscription"
            action="{{ $formAction }}"
            method="{{ $formMethod }}"
          >
            <input
              id="rad-subscription-email"
              type="email"
              class="form-control"
              aria-describedby="emailHelp"
              placeholder='{{ if $isShortcode }} {{ .Get "newsletter_placeholder" }} {{ else }} {{ i18n "newsletter_placeholder" }} {{ end }}'
              aria-label='{{ if $isShortcode }} {{ .Get "newsletter_placeholder" }} {{ else }} {{ i18n "newsletter_placeholder" }} {{ end }}'
            >
            <button type="submit" id="rad-subscription-submit" class="btn btn-light btn-rounded">
              {{ if $isShortcode }} 
                {{ .Get "newsletter_button" }} 
              {{ else }} 
                {{ i18n "newsletter_button" }} 
              {{ end }}
            </button>
          </form>
          <div id="rad-subscription-success" class="d-none">
            <p class="lead">
              {{ if $isShortcode }} 
                {{ .Get "newsletter_success_message" }} 
              {{ else }} 
                {{ i18n "newsletter_success_message" }} 
              {{ end }}
            </p>
          </div>
          <div id="rad-subscription-fail" class="d-none">
            <p>
              {{ if $isShortcode }} 
                {{ .Get "newsletter_error_message" }} 
              {{ else }} 
                {{ i18n "newsletter_error_message" }} 
              {{ end }}
            </p>
          </div>
          <script src='{{ "js/subscription.js" | absURL }}'></script>
        </div>
        <small id="emailHelp" class="form-text text-muted">
          {{ if $isShortcode }} 
            {{ .Get "newsletter_note" }} 
          {{ else }} 
            {{ i18n "newsletter_note" }}
          {{ end }}
        </small>
      </div>
    </div>
  </div>
</section>
