
{% macro hmpoDate(ctx, params, base) %}
    {%- set translate = ctx("translate") %}
    {%- set params = hmpoGetParams(ctx, params, base) %}

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

    {%- if not params.inexact %}
        {%- set day = hmpoGetParams(ctx, { id: params.id + "-day" }) %}
        {%- set dayError = hmpoGetError(ctx, day) %}
        {%- set dayArgs = {
            id: day.id,
            name: day.id,
            value: hmpoGetValue(ctx, day),
            label: translate(params.dayKey or 'fields.date-day.label'),
            attributes: hmpoGetAttributes(ctx, day, {
                maxlength: 2,
                autocomplete: day.autocomplete,
                "aria-required": hmpoGetValidatorAttribute(ctx, day, "required", null, false)
            } | filter(null)),
            classes: "govuk-input--width-2" + (" govuk-input--error" if dayError)
        } %}
    {% endif %}

    {%- set month = hmpoGetParams(ctx, { id: params.id + "-month" }) %}
    {%- set monthError = hmpoGetError(ctx, month) %}
    {%- set monthArgs = {
        id: month.id,
        name: month.id,
        value: hmpoGetValue(ctx, month),
        label: translate(params.monthKey or 'fields.date-month.label'),
        attributes: hmpoGetAttributes(ctx, month, {
            maxlength: 2,
            autocomplete: month.autocomplete,
            "aria-required": hmpoGetValidatorAttribute(ctx, month, "required", null, false)
        } | filter(null)),
        classes: "govuk-input--width-2" + (" govuk-input--error" if monthError)
    } %}

    {%- set year = hmpoGetParams(ctx, { id: params.id + "-year" }) %}
    {%- set yearError = hmpoGetError(ctx, year) %}
    {%- set yearArgs = {
        id: year.id,
        name: year.id,
        value: hmpoGetValue(ctx, year),
        label: translate(params.yearKey or 'fields.date-year.label'),
        attributes: hmpoGetAttributes(ctx, year, {
            maxlength: 4,
            autocomplete: year.autocomplete,
            "aria-required": hmpoGetValidatorAttribute(ctx, year, "required", null, false)
        } | filter(null)),
        classes: "govuk-input--width-4" + (" govuk-input--error" if yearError)
    } %}

    {%- set args = {
        id: params.id,
        fieldset: 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) or dayError or monthError or yearError,
        classes: params.classes,
        formGroup: params.formGroup,
        items: [ dayArgs, monthArgs, yearArgs ] if dayArgs else [ monthArgs, yearArgs ]
    } %}

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

    {% if args.errorMessage %}{{errorMessage.message}}{% endif %}
    {%- from "./govuk-date.njk" import govukDateInput %}
    {{- govukDateInput(args) }}
{% endmacro %}
