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

{# VARIABLES #}
{% set description_content = description_content|default(false) %}
{% set errors = errors|default(false) %}
{% set prefix = prefix|default(false) %}
{% set suffix = suffix|default(false) %}
{% set attributes = attributes|default(create_attribute()) %}
{% set size = size|default(false) %}

{% set classes = [
  classes|default(""),
  title_display not in ['after', 'before'] ? 'form-no-label',
  disabled == 'disabled' ? 'form-disabled',
  label_display == 'inline' ? 'label-inline form-row row',
  label_display == 'invisible' ? 'label-invisible',
  errors ? 'form-item--error',
  'form-group',
  'js-form-item',
  'js-form-type-' ~ type|clean_class,
  'js-form-item-' ~ name|clean_class,
] %}
{% set description_classes = [
    'description',
    'form-text',
    'text-muted',
    description_display == 'invisible' ? 'visually-hidden',
] %}

{% set presufix_classes = "" %}
{% if type not in ['checkbox', 'checkboxes', 'radio', 'radios']  %}
  {% set presufix_classes = 'input-group-text' %}
{% endif %}

<div {{ attributes.addClass(classes) }}>

  {% if label_display == 'inline' %}
  <div class="col-md-3">
    {{ label }}
  </div>
  <div class="col-md-9">
  {% endif %}

  {% if label_display in ['before', 'invisible'] %}
    {{ label }}
  {% endif %}

  {% if description_display == 'before' and description_content %}
    <small {{ description_attributes.addClass(description_classes) }} class="help-block">
      {{ description_content }}
    </small>
  {% endif %}

  {% if prefix is not empty or suffix is not empty %}
    <div class="input-group {{ (size in ['sm', 'lg']) ? 'input-group-' ~ size }}">
  {% endif %}

    {% if prefix is not empty %}
      <div class="prefix {{ presufix_classes }}">
        {{ prefix|raw }}
      </div>
    {% endif %}

    {{ children }}

    {% if suffix is not empty %}
      <div class="suffix {{ presufix_classes }}">
        {{ suffix|raw }}
      </div>
    {% endif %}

  {% if prefix is not empty or suffix is not empty %}</div>{% endif %}

  {% if label_display == 'after' %}
    {{ label }}
  {% 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.addClass(description_classes) }} class="help-block">
      {{ description_content }}
    </small>
  {% endif %}

  {% if label_display == 'inline' %}
  </div>
  {% endif %}

</div>
