{% comment %}
    Renders a form status message
    Accepts:
    - form: {Object} Form Liquid object (required)
    - form_id: {String} Form's id attribute for accessibility purpose (required)
    - success_message: {String} Success message locale key. Default to 'contact.form.post_success' (optional)

    Usage:
    {% include 'form-status', form: form, form_id: formId, success_message: post_message %}
{% endcomment %}
{%- if form.posted_successfully? -%}
  <p class="form-message form-message--success" tabindex="-1" data-form-status>
    {{ success_message | default: 'contact.form.post_success' | t }}
  </p>
{%- endif -%}

{% comment %} We need to add this so the errors are output in the right order {% endcomment %}
{% assign error_order = "author, email, body, password, form" | split: ", " %}

{%- if form.errors -%}

  {%- if form.errors.translated_fields.size == 1 and form.errors.first == 'form' -%}
    <p class="form-message" tabindex="-1" data-form-status>
      {{ form.errors.messages['form'] }}
    </p>

  {% else %}
    <div class="form-message form-message--error">
      <h2 class="h3 form-message__title" tabindex="-1" data-form-status>{{ 'contact.form.error_heading' | t }}</h2>

      <ul>
        {% for error in error_order %}
          {% for field in form.errors %}
            {% if error == field %}

              {% capture field_label %}
                {% case field %}
                  {% when 'author' %}
                    {{ 'contact.form.name' | t }}
                  {% when 'body' %}
                    {{ 'contact.form.message' | t }}
                  {% else %}
                    {{ form.errors.translated_fields[field] }}
                {% endcase %}
              {% endcapture %}
              <li>
                {%- if field == 'form' -%}
                  {{ form.errors.messages[field] }}
                {%- else -%}
                  {% comment %} the href should match the input's id {% endcomment %}
                  <a href="#{{ form_id }}-{{ field }}" class="form-message__link">{{ field_label | strip | capitalize }} {{ form.errors.messages[field] }}</a>
                {%- endif -%}
              </li>
            {% endif %}
          {% endfor %}
        {% endfor %}
      </ul>
    </div>
  {%- endif -%}
{%- endif -%}
