{% set classes = [
  'js-form-item',
  'form-item',
  'js-form-wrapper',
  'form-wrapper',
  'c-fieldset',
  required or is_required ? 'is-required' : '',
  is_disabled ? 'is-disabled' : '',
  modifier_classes ? modifier_classes : '',
]|join(' ')|trim %}

{% set attributes_to_add = {
  class: classes,
} %}

{% if description.content %}
  {% set attributes_to_add = attributes_to_add|merge({
    'aria-describedby': id,
  }) %}
{% endif %}

{% if disabled or is_disabled %}
  {% set attributes_to_add = attributes_to_add|merge({
    'disabled': disabled,
  }) %}
{% endif %}

{% if required or is_required %}
  {% set attributes_to_add = attributes_to_add|merge({
    'required': 'required',
    'aria-required': 'true',
  }) %}
{% endif %}

{% set legend_span_classes = [
  'c-fieldset__legend-text',
  required or is_required ? 'is-required' : '',
  required or is_required ? 'js-form-required' : '',
]|join(' ')|trim %}

<fieldset {{ add_attributes(attributes_to_add) }}>
  {#  Always wrap fieldset legends in a <span> for CSS positioning. #}
  <legend {{ add_attributes({class: 'c-fieldset__legend'}, 'legend_attributes') }}>
    <span {{ add_attributes({class: legend_span_classes}, 'legend_span_attributes') }}>{{ legend.title }}</span>
  </legend>
  <div class="c-fieldset__content">
    {% if errors %}
      <div class="c-fieldset__errors">{{ errors }}</div>
    {% endif %}

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

    {{ children }}

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

    {% if description.content %}
      <div {{ add_attributes({class: 'c-fieldset__description', id: id}, 'description_attributes') }}>{{ description.content }}</div>
    {% endif %}
  </div>
</fieldset>
