{% set element = element ?? null %}

{# Keep track of the outer repeater field #}
{% set repeaterField = field %}

{% set html %}
    <fieldset class="fui-fieldset">
        {% set index = index ?? '__ROW__' %}

        {# Namespace tag can't handle `0`, which is what our indexes start at, being arrays #}
        {% set namespace = index != '__ROW__' ? 'row' ~ (index + 1) : index %}
        
        {% namespace namespace %}
            <div class="fui-field-rows">
                {% for row in field.getRows() %}
                    <div class="fui-row fui-page-row">
                        {% for field in row.getFields() %}
                            {# Update the namespace for the field, to include the specific block index #}
                            {% do field.setParentField(repeaterField, index) %}

                            {% include 'formie/_formfields/repeater/field' with {
                                field: field,
                                element: element,
                                required: field.required,
                            } only %}
                        {% endfor %}
                    </div>
                {% endfor %}
            </div>
        {% endnamespace %}

        {% set buttonAttributes = {
            class: 'btn fui-repeater-remove-btn',
            type: 'button',
            'data-remove-repeater-row': field.handle,
        } %}

        <button {{ attr(buttonAttributes) }}>
            {{ 'Remove' | t('formie') }}
        </button>
    </fieldset>
{% endset %}

{{ tag('div', {
    class: 'fui-repeater-row',
    'data-repeater-row': true,
    html: html,
}) }}