{% macro onsSummary(params) %}
    {% set className = "ons-summary" %}
    {% set headingLevel = "2" %}

    {% if params.variant == "hub" %}
        {% set variantHub = true %}
        {% set className = className + " ons-summary--hub" %}
    {% elif params.variant %}
        {% set variantClasses = " ons-summary__group--" + params.variant %}
    {% endif %}

    {% if params.classes %}
        {% set className = className + " " + params.classes %}
    {% endif %}

    <div class="{{ className }}">
        {% for summary in params.summaries %}
            {% if summary.title %}
                <h2 class="ons-summary__title ons-u-mb-l{{ ' ons-u-mt-l' if loop.index > 1 else "" }}">{{ summary.title }}</h2>
                {% set headingLevel = "3" %}
            {% endif %}
            {% for group in summary.groups %}
                <div {% if group.id %}id="{{ group.id }}"{% endif %} class="ons-summary__group{{ variantClasses }}">
                    {% if group.title %}
                        {% set openingHeadingTag = "<h" ~ headingLevel %}
                        {% set closingHeadingTag = "</h" ~ headingLevel ~ ">" %}
                        {{ openingHeadingTag | safe }}
                        class="ons-summary__group-title">{{ group.title }}{{ closingHeadingTag | safe }}
                    {% endif %}
                    {% if group.rows %}
                        <dl class="ons-summary__items">
                            {% for row in group.rows %}
                                {% set itemClass = "" %}
                                {% if row.error %}{% set itemClass = " ons-summary__item--error" %}{% endif %}
                                {% if row.total %}{% set itemClass = itemClass + " ons-summary__item--total" %}{% endif %}
                                <div {% if row.id %}id="{{ row.id }}"{% endif %}class="ons-summary__item{{ itemClass }}">
                                    {% if row.errorMessage %}
                                        <dt class="ons-summary__row-title--error ons-u-fs-r">{{ row.errorMessage }}</dt>
                                    {% endif %}
                                    {% if row.itemsList | length > 1 and row.title %}
                                        <dt class="ons-summary__row-title ons-summary__row-title--no-group-title ons-u-fs-r">
                                            {{- row.title -}}
                                        </dt>
                                    {% endif %}

                                    {% for item in row.itemsList %}
                                        <dt
                                            class="ons-summary__item-title"
                                            {% if item.titleAttributes %}{% for attribute, value in (item.titleAttributes.items() if item.titleAttributes is mapping and item.titleAttributes.items else item.titleAttributes) %}{{ attribute }}="{{ value }}"{% endfor %}{% endif %}
                                        >
                                            {% if item.iconType %}
                                                {% from "components/icon/_macro.njk" import onsIcon %}
                                                <span
                                                    class="ons-summary__item-title-icon{{ ' ons-summary__item-title-icon--check' if item.iconType == 'check' else "" }}"
                                                >
                                                    {{-
                                                        onsIcon({
                                                            "iconType": item.iconType
                                                        })
                                                    -}}
                                                    {% if item.iconVisuallyHiddenText %}
                                                        <span class="ons-u-vh">{{ item.iconVisuallyHiddenText }}</span>
                                                    {% endif %}
                                                </span>
                                            {% endif %}

                                            <div
                                                class="ons-summary__item--text{{ ' ons-summary__item-title--text' if item.iconType else "" }}"
                                            >
                                                {{- item.title | default(row.title) | safe -}}
                                            </div>

                                            {# Render section status for mobile if is hub #}
                                            {% if variantHub and item.valueList %}
                                                <span class="ons-u-d-no@m ons-u-fs-r"> — {{ item.valueList[0].text | safe }}</span>
                                            {% endif %}
                                        </dt>
                                        {% if item.valueList %}
                                            <dd
                                                class="ons-summary__values{{ ' ons-summary__column-size--2' if not item.actions }}"
                                                {% if item.attributes %}{% for attribute, value in (item.attributes.items() if item.attributes is mapping and item.attributes.items else item.attributes) %}{{ attribute }}="{{ value }}"{% endfor %}{% endif %}
                                            >
                                                {% if item.valueList | length == 1 %}
                                                    <span class="ons-summary__text">{{ item.valueList[0].text | safe }}</span>
                                                    {% if item.valueList[0].other or item.valueList[0].other == 0 %}
                                                        <ul class="ons-u-mb-no">
                                                            <li>{{ item.valueList[0].other | safe }}</li>
                                                        </ul>
                                                    {% endif %}
                                                {% else %}
                                                    <ul class="ons-u-mb-no">
                                                        {% for value in item.valueList %}
                                                            <li>
                                                                <span class="ons-summary__text">{{ value.text | safe }}</span>
                                                                {% if value.other or value.other == 0 %}
                                                                    <ul class="ons-u-mb-no">
                                                                        <li>{{ value.other | safe }}</li>
                                                                    </ul>
                                                                {% endif %}
                                                            </li>
                                                        {% endfor %}
                                                    </ul>
                                                {% endif %}
                                            </dd>
                                        {% endif %}
                                        {% if item.actions %}
                                            <dd
                                                class="ons-summary__actions{{ ' ons-summary__column-size--2' if not item.valueList }}"
                                                {% if item.id %}id="{{ item.id }}"{% endif %}
                                            >
                                                {% for action in item.actions %}
                                                    {% if loop.index > 1 %}<span class="ons-summary__spacer"></span>{% endif %}
                                                    <a
                                                        href="{{ action.url }}"
                                                        class="ons-summary__button"
                                                        {% if action.attributes %}{% for attribute, value in (action.attributes.items() if action.attributes is mapping and action.attributes.items else action.attributes) %}{{ ' ' }}{{ attribute }}="{{ value }}"{% endfor %}{% endif %}
                                                    >
                                                        <span class="ons-summary__button-text" aria-hidden="true">{{- action.text -}}</span>
                                                        <span class="ons-u-vh"
                                                            >{{ action.visuallyHiddenText | default (action.text) }}</span
                                                        >
                                                    </a>
                                                {% endfor %}
                                            </dd>
                                        {% endif %}
                                    {% endfor %}
                                </div>
                            {% endfor %}
                        </dl>
                    {% elif group.placeholderText %}
                        <span class="ons-summary__placeholder">{{ group.placeholderText }}</span>
                    {% endif %}

                    {% if group.summaryLink %}
                        <div
                            class="ons-summary__link{{ ' ons-u-pt-s' if params.variant != 'card' and (group.placeholderText or group.rows) }}{{ ' ons-u-bt' if group.placeholderText is not defined and group.rows | length > 1 and params.variant != 'card' }}{{ ' ons-u-mb-3xl' if not group.last }}"
                        >
                            {% set link %}
                                <a
                                    {% if group.summaryLink.attributes %}{% for attribute, value in (group.summaryLink.attributes.items() if group.summaryLink.attributes is mapping and group.summaryLink.attributes.items else group.summaryLink.attributes) %}{{ ' ' }}{{ attribute }}="{{ value }}"{% endfor %}{% endif %}
                                    href="{{ group.summaryLink.url }}"
                                >
                                    {{ group.summaryLink.text }}
                                </a>
                            {% endset %}
                            {% if params.variant == 'card' %}
                                <div class="ons-summary__link-container">{{ link | safe }}</div>
                            {% else %}
                                {{ link | safe }}
                            {% endif %}
                        </div>
                    {% endif %}
                </div>
            {% endfor %}
        {% endfor %}
    </div>
{% endmacro %}
