{% macro hmpoErrorGroup(ctx, hmpoParams, base) %}
    {%- set params = hmpoGetParams(ctx, hmpoParams, base) %}

    {%- set pageHeading = {
        legend: {
            isPageHeading: true,
            classes: "govuk-fieldset__legend--l"
        }
    } if params.isPageHeading %}

    {%- set params = {
        id: params.id,
        fieldset: false if params.fieldset == false else merge(pageHeading, {
            legend: hmpoGetOptions(ctx, params, "legend", true) or hmpoGetOptions(ctx, params, "label"),
            attributes: {
                id: params.id + "-fieldset"
            }
        }, params.fieldset),
        hint: hmpoGetOptions(ctx, params, "hint", true),
        errorMessage: hmpoGetError(ctx, params),
        classes: params.classes,
        attributes: params.attributes
    } %}

    {% set innerHtml %}

        {% if params.hint %}
            {% set hintId = params.id + '-hint' %}
            {% set describedBy = hintId %}
            {% from "govuk/components/hint/macro.njk" import govukHint %}
            {{ govukHint({
                id: hintId,
                classes: params.hint.classes,
                attributes: params.hint.attributes,
                html: params.hint.html,
                text: params.hint.text
            }) | indent(2) | trim }}
        {% endif %}

        {% if params.errorMessage %}
            {% set errorId = params.id + '-error' %}
            {% set describedBy = describedBy + ' ' + errorId if describedBy else errorId %}
            {% from "govuk/components/error-message/macro.njk" import govukErrorMessage %}
            {{ govukErrorMessage({
                id: errorId,
                classes: params.errorMessage.classes,
                attributes: params.errorMessage.attributes,
                html: params.errorMessage.html,
                text: params.errorMessage.text,
                visuallyHiddenText: params.errorMessage.visuallyHiddenText
            }) | indent(2) | trim }}
        {% endif %}

        {{ hmpoParams.html | safe if hmpoParams.html }}{{ caller() if caller }}
    {% endset %}

    <div id="{{ params.id }}"
            class="govuk-form-group hmpo-error-group{%- if params.classes %} {{ params.classes }}{% endif %}{% if params.errorMessage %} govuk-form-group--error{% endif %}"
            {%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor -%}>
        {% if params.fieldset %}

            {%- if params.fieldset.legend.attributes %}
                {% set legendHtml %}
                    <span {%- for attribute, value in params.fieldset.legend.attributes %} {{ attribute }}="{{ value }}"{% endfor %}>
                    {{ params.fieldset.legend.html | safe if params.fieldset.legend.html else params.fieldset.legend.text }}
                    </span>
                {% endset %}
                {%- set params = merge(params, { fieldset: { legend: { html: legendHtml } } }) %}
            {%- endif %}

            {% from "govuk/components/fieldset/macro.njk" import govukFieldset %}
            {% call govukFieldset({
                describedBy: describedBy,
                classes: params.fieldset.classes,
                attributes: params.fieldset.attributes,
                legend: params.fieldset.legend
            }) %}
                {{ innerHtml | safe }}
            {% endcall %}
        {% else %}
            {{ innerHtml | safe }}
        {% endif %}
    </div>
{% endmacro %}
