{# LIBRARIES #}
{{ attach_library('ui_patterns/libraries.bs_forms') }}
{{ attach_library('ui_patterns/form-form.form_lib') }}
{{ attach_library('ui_patterns/libraries.bs_card') }}

{# VARIABLES #}
{% set attributes = attributes|default(create_attribute()) %}
{% set title_attributes = title_attributes|default(create_attribute()) %}
{% set legend_attributes = legend_attributes|default(create_attribute()) %}
{% set content_attributes = content_attributes|default(create_attribute()) %}
{% set description_display = description_display|default('before') %}
{% set no_card = no_card|default(false) %}
{% set no_legend_wrapper = no_legend_wrapper|default(false) %}

{% set content_classes = content_classes|default('') %}
{% set title_classes = title_classes|default('') %}
{% set legend_classes = legend_classes|default('') %}
{% set classes = classes|default('') %}

{% if title_display == 'inline' %}
  {% set type = 'input' %}
{% endif %}

{% set classes = [
  classes ? classes : '',
  type != 'input' and no_card == false ? 'card',
  title_display == 'inline' ? 'form-row row',
  'js-form-wrapper',
] %}

{% set title_classes = [
  title_classes ? title_classes : '',
  'fieldset-legend',
  'legend-title',
  required ? 'js-form-required',
  required ? 'form-required',
] %}

{% set legend_classes = [
  legend_classes ? legend_classes : '',
  type != 'input' and no_card == false ? 'card-header',
  title_display == 'inline' ? 'col-md-3',
] %}

{% set content_classes = [
  content_classes ? content_classes : '',
  'fieldset-wrapper',
  type != 'input' and no_card == false ? 'card-body',
  title_display == 'inline' ? 'col-md-9',
] %}

{% set wrapper_elem = 'fieldset' %}
{% set label_elem = 'span' %}
{% if title_display == 'inline' %}
  {% set wrapper_elem = 'div' %}
  {% set label_elem = 'h2' %}
  {% set classes = classes|merge(['fieldset']) %}
{% endif %}

<{{ wrapper_elem }} {{ attributes.addClass(classes) }}>
  <legend {{ legend_attributes.addClass(legend_classes) }}>
    {% if not no_legend_wrapper %}
    <{{ label_elem}} {{ title_attributes.addClass(title_classes) }}>
    {% endif %}
      {% if title_prefix %}
        {{ title_prefix|raw }}
      {% endif %}
      {{ legend_title|raw }}
      {%- if required -%}
        {# Deprecating in favor of CSS for JS.
        <span class="reqd">*</span>
        <span class="sr-only">Required form item</span>
        #}
      {%- endif -%}
      {% if title_suffix %}
        {{ title_suffix|raw }}
      {% endif %}
    {% if not no_legend_wrapper %}
    </{{ label_elem }}>
    {% endif %}
  </legend>

  <div {{ content_attributes.addClass(content_classes) }}>

    {% if description_display == 'before' and description_content %}
      <small{{ description_attributes }} class="help-block fshb">
        {{ description_content }}
      </small>
    {% endif %}

    {% if prefix %}
      <span class="field-prefix">{{ prefix }}</span>
    {% endif %}

    {{ children }}

    {% if suffix %}
      <span class="field-suffix">{{ suffix }}</span>
    {% endif %}

    {% if errors %}
      <div class="alert alert-danger form-item--error-message">
        <strong>{{ errors }}</strong>
      </div>
    {% endif %}

    {% if description_display in ['after', 'invisible'] and description_content %}
      <small{{ description_attributes }} class="help-block fshb">
        {{ description_content }}
      </small>
    {% endif %}

  </div>
</{{ wrapper_elem }}>
