{% macro onsRadios(params) %}
    {% from "components/fieldset/_macro.njk" import onsFieldset %}
    {% from "components/button/_macro.njk" import onsButton %}
    {% from "components/label/_macro.njk" import onsLabel %}

    {%
        call onsFieldset({
            "id": params.id,
            "classes": params.classes,
            "legend": params.legend,
            "legendClasses": params.legendClasses,
            "description": params.description,
            "dontWrap": params.dontWrap,
            "legendIsQuestionTitle": params.legendIsQuestionTitle,
            "error": params.error
        })
    %}
        <div class="ons-radios__items">
            {% for radio in params.radios %}
                {% if params.or and loop.revindex0 == 0 %}
                    <span class="ons-radios__label ons-u-mt-2xs ons-u-fs-r--b" aria-hidden="true">{{ params.or }}</span>
                {% endif %}
                <span class="ons-radios__item{{ " ons-radios__item--no-border" if params.borderless }}">
                    <span class="ons-radio{{ " ons-radio--no-border" if params.borderless }}">
                        {% if radio.other and radio.other.selectAllChildren == true %}
                            {% set selectAllClass = ' ons-js-select-all-children' %}
                        {% else %}
                            {% set selectAllClass = '' %}
                        {% endif %}
                        <input
                            type="radio"
                            id="{{ radio.id }}"
                            class="ons-radio__input ons-js-radio{{ ' ' + params.inputClasses if params.inputClasses else '' }}{{ ' ' + radio.classes if radio.classes else '' }}{{ ' ons-js-other' if radio.other }}{{ selectAllClass }}"
                            value="{{ radio.value }}"
                            name="{{ params.name }}"
                            {% if radio.checked or (params.value is defined and params.value == radio.value) %}{{ ' ' }}checked{% endif %}
                            {% if radio.other and not radio.other.open %}
                                {{ ' ' }}aria-controls="{{ radio.id }}-other-wrap"
                                aria-haspopup="true"
                            {% endif %}
                            {% if radio.attributes %}{% for attribute, value in (radio.attributes.items() if radio.attributes is mapping and radio.attributes.items else radio.attributes) %}{{ ' ' }}{{ attribute }}{% if value %}="{{ value }}"{% endif %}{% endfor %}{% endif %}
                        />
                        {{
                            onsLabel({
                                "id": radio.id + "-label",
                                "for": radio.id,
                                "inputType": "radio",
                                "text": radio.label.text,
                                "classes": "ons-radio__label" + (" " + radio.label.classes if radio.label.classes else ""),
                                "description": radio.label.description
                            })
                        }}
                        {% if radio.other %}
                            {% set otherType = radio.other.otherType | default('input') %}
                            <span
                                class="ons-radio__other{{ ' ' + 'ons-radio__other--open' if radio.other.open else '' }}"
                                id="{{ radio.id }}-other-wrap"
                            >
                                {% if otherType == "input" %}
                                    {% from "components/input/_macro.njk" import onsInput %}
                                    {{
                                        onsInput({
                                            "id": radio.other.id,
                                            "name": radio.other.name,
                                            "type": radio.other.type,
                                            "classes": ("ons-input--error " if params.error and radio.checked else "") + radio.other.classes | default(''),
                                            "width": radio.other.width | default('auto'),
                                            "attributes": radio.other.attributes,
                                            "label": {
                                                "id": radio.other.id + "-label",
                                                "text": radio.other.label.text,
                                                "classes": 'ons-u-fw-n'
                                            },
                                            "dontWrap": true,
                                            "value": radio.other.value
                                        })
                                    }}
                                {% elif otherType == "select" %}
                                    {% from "components/select/_macro.njk" import onsSelect %}
                                    {{
                                        onsSelect({
                                            "id": radio.other.id,
                                            "name": radio.other.name,
                                            "options": radio.other.options,
                                            "classes": ("ons-input--error " if params.error and radio.checked else "") + radio.other.classes | default(''),
                                            "label": {
                                                "id": radio.other.id + "-label",
                                                "text": radio.other.label.text,
                                                "classes": 'ons-u-fw-n'
                                            },
                                            "dontWrap": true,
                                            "value": radio.other.value
                                        })
                                    }}
                                {% elif otherType == "checkboxes" %}
                                    {% from "components/checkboxes/_macro.njk" import onsCheckboxes %}
                                    {{
                                        onsCheckboxes({
                                            "id": radio.other.id,
                                            "name": radio.other.name,
                                            "checked": radio.other.checked,
                                            "borderless": true,
                                            "legend": radio.other.legend,
                                            "legendClasses": radio.other.legendClasses,
                                            "attributes": radio.other.attributes,
                                            "classes": "ons-js-other-fieldset-radio",
                                            "checkboxes": radio.other.checkboxes,
                                            "autoSelect": radio.other.autoSelect,
                                            "selectAllChildren": radio.other.selectAllChildren
                                        })
                                    }}
                                {% elif otherType == "radios" %}
                                    {% from "components/radios/_macro.njk" import onsRadios %}
                                    {{
                                        onsRadios({
                                            "id": radio.other.id,
                                            "name": radio.other.name,
                                            "borderless": true,
                                            "legend": radio.other.legend,
                                            "legendClasses": radio.other.legendClasses | default('') + ' ons-u-mb-2xs',
                                            "attributes": radio.other.attributes,
                                            "classes": "ons-js-other-fieldset-radio",
                                            "radios": radio.other.radios
                                        })
                                    }}
                                {% elif otherType == "textarea" %}
                                    {% from "components/textarea/_macro.njk" import onsTextarea %}
                                    {{
                                        onsTextarea({
                                            "id": radio.other.id,
                                            "name": radio.other.name,
                                            "value": radio.other.value,
                                            "label": {
                                                "id": radio.other.id + "-label",
                                                "text": radio.other.label.text,
                                                "classes": 'ons-u-fw-n',
                                                "description": radio.other.label.description
                                            },
                                            "charCheckLimit": {
                                                "limit": radio.other.charCheckLimit.limit,
                                                "charCountSingular": radio.other.charCheckLimit.charCountSingular,
                                                "charCountPlural": radio.other.charCheckLimit.charCountPlural
                                            },
                                            "attributes": radio.other.attributes
                                        })
                                    }}
                                {% endif %}
                            </span>
                        {% endif %}
                    </span>
                </span>
                {% if not loop.last %}
                    <br />
                {% endif %}
            {% endfor %}
        </div>
        {% if params.clearRadios %}
            {{
                onsButton({
                    "text": params.clearRadios.text,
                    "name": params.clearRadios.name,
                    "type": "submit",
                    "classes": "ons-js-clear-btn ons-u-mt-s ons-u-db-no-js_enabled",
                    "variants": ["secondary", "small"]
                })
            }}
            <span
                class="ons-js-clear-radio-alert ons-u-vh"
                role="alert"
                aria-live="polite"
                data-clear="{{ params.clearRadios.ariaClearText }}"
                data-cleared="{{ params.clearRadios.ariaClearedText }}"
            ></span>
        {% endif %}
    {% endcall %}
{% endmacro %}
