{# radio item
 @param {object} params The parent parameters 
 @param {boolean} isInline Is the radio inline type? Optional, default is false. Can be true,false 
 @param {int} index The index number in the items array
 @param {string} lang The language used. Can be 'en','el'. 
 @param {object} item The item object 
 i.e. 
 item : {
        type: "or",
        altOrText: {en:"If not",el:"Αν όχι"},
        value: "maybe",
        hint: {en:"We want you to be absolutely sure",el:"Θέλουμε να είστε απολύτως σίγουροι"},
        text: {en:"Maybe",el:"Ίσως"}
    }
    @param {string} item.type The item type. Can be empty,'or','and'. Optional, default is empty  
    @param {string} item.value The value of the radio.  
    @param {object} item.text The label of the radio. Example `{en:"Content",el:"Περιεχομένο"}`  
    @param {object} item.hint The hint of the radio. Example `{en:"Content",el:"Περιεχομένο"}`  
    @param {object} item.altAndText The and text. Optional, default is `{en:"And",el:"Και"}
    @param {object} item.altOrText The or text. Optional, default is `{en:"Or",el:"Ή"}
    @param {array} item.conditionalElements The conditional elements for this radio item. Optional. This will be an array of elements.
    @param {boolean} item.conditionalHasErrors The conditional elements for this radio item has error. Optional.
    i.e. `[
            {element:"button",params:{text:{en:"Button 1",el:"Κουμπί 1"},lang:"en",id:"govcy-test-23b"} },
            {element:"button",params:{text:{en:"Button 2",el:"Κουμπί 2"},variant:'secondary',lang:"en",id:"govcy-test-23c"} }
        ]
 @returns govcy radio item html 
#}
{%- macro _radioItem(params, item, isInline, index, lang) %}
{#- Import localizer from utilities -#}
{%- from "../utilities/govcyUtilities.njk" import govcyLocalizeContent, govcyLangAttribute, govcyElementsFromArray, govcyGetContent -%}
{%- from "./hint.njk" import hint -%}
{%- from "./label.njk" import label -%}
{#- set default values -#}
{%- set isInline = isInline | default(false) -%}
{#- set language labels -#}
{#- set 'or' labels -#}
{%- if item.altOrText -%}
    {%- set orText -%} {{- govcyLocalizeContent(item.altOrText, lang) -}}{%- endset -%}
{%- else -%}
    {%- set orText -%} {{- govcyGetContent('common_or', lang) -}}{%- endset -%}
{%- endif -%}
{#- set 'and' labels -#}
{%- if item.altAndText -%}
    {%- set andText -%} {{- govcyLocalizeContent(item.altAndText, lang) -}}{%- endset -%}
{%- else -%}
    {%- set andText -%} {{- govcyGetContent('common_and', lang) -}}{%- endset -%}
{%- endif -%}
{#- set option id -#}
{% set optionId = params.id ~ '-option-' ~ index %}
{#- set hint id -#}
{%- if item.hint -%}
    {%- set hintId = [optionId, "-hint"] | join -%}
{%- else -%}
    {%- set hintId = '' -%}
{%- endif -%}
{#- set conditional id -#}
{%- if item.conditionalElements -%}
    {%- set conditionalElementsId = [optionId, "-conditional"] | join -%}
    {%- set conditionalLabel = govcyGetContent('radios_conditionalLabel', lang) -%}
{%- else -%}
    {%- set conditionalElementsId = '' -%}
    {%- set conditionalLabel = '' -%}
{%- endif -%}
{# depending on the type, render html and set label#}
{%- if item.type == 'or' -%}
    <p class="govcy-ml-3 govcy-mb-3{% if isInline %} govcy-d-sm-inline-block govcy-mr-3{% endif %}">{{ orText }}</p>
    {%- set label -%}
        <span class="govcy-visually-hidden-error">{{ orText }}, </span> {{- govcyLocalizeContent(item.text, lang) -}}
    {%- endset -%}
{%- elif item.type == 'and' -%}
    <p class="govcy-ml-3 govcy-mb-3{% if isInline %} govcy-d-sm-inline-block govcy-mr-3{% endif %}">{{ andText }}</p>
    {%- set label -%}
        <span class="govcy-visually-hidden-error">{{ andText }}, </span> {{- govcyLocalizeContent(item.text, lang) -}}
    {%- endset -%}
{% else %}
    {%- set label -%}{{- govcyLocalizeContent(item.text, lang) -}}{%- endset -%}
{%- endif -%}
{#- render radio -#}
<div class="govcy-radio{% if isInline %} govcy-d-sm-inline-block{% endif %}">
    <input class="govcy-radio-input" name="{{ params.name }}"{% if params.value == item.value %} checked{% endif %} value="{{item.value}}" type="radio" id="{{ optionId }}" {% if item.hint%} aria-describedby="{{hintId}}"{% endif %}{% if item.conditionalElements %} data-aria-controls="{{conditionalElementsId}}"{% endif %}>
    <label class="govcy-label" for="{{ optionId }}">{% if item.conditionalElements %} <span class="govcy-visually-hidden">{{conditionalLabel}}</span>{% endif %}{{ label | safe }}</label>
    {#- {% call govcyLabel({label:label, for:optionId, isPrimary:false, isHTML:true, lang:lang}) %}{% endcall %} -#}
    {% call hint({hint:item.hint, id:hintId, lang:lang}) %}{% endcall %}
</div>
{#- conditional elements -#}
{%- if item.conditionalElements %}
    {#- Transform conditional elements, add hideFormControlError -#}
    {%- set updatedConditionalElements = [] -%}
    {%- for element in item.conditionalElements -%}
        {%- set updatedElement = element | merge({
            params: element.params | merge({"hideFormControlError": true})
        }) -%}
        {%- set updatedConditionalElements = updatedConditionalElements.concat([updatedElement]) -%}
    {%- endfor -%}
        <div class="govcy-form-control{% if item.conditionalHasErrors %} govcy-form-control-error{% else %} govcy-form-control-hint{% endif %} govcy-pl-4 govcy-ml-5 govcy-radio__conditional govcy-radio__conditional--hidden" id="{{conditionalElementsId}}">
            {{ govcyElementsFromArray(updatedConditionalElements, params.lang) }}
        </div>
{%- endif -%}
{%- endmacro %}
{# radios
 @param {object} legend The legend text. Will escape text. Example `{en:"Content",el:"Περιεχομένο"}`
 @param {string} id The id prefix for the options. Will escape text. 
 @param {string} name The name used in radio. Will escape text. 
 @param {string} value The selected radio option. Optional.
 @param {string} hint The hint text. Optional. Will escape text 
 @param {boolean} isPageHeading Is the label also the page heading? Optional, default is false. Can be true,false 
 @param {boolean} isInline Are the radios inline type? Optional, default is false. Can be true,false 
 @param {string} classes Additional classes to add to the outer fieldset. Optional  
 @param {object} error If not empty there is an error message and displays the error variant. Optional, default is ''. Will escape text. Example `{en:"Content",el:"Περιεχομένο"}`
 @param {boolean} hideFormControlError If true, hides the form control error (red line on the left). Mostly used in conditional radio elements. Optional    
 @param {string} lang The language used. Can be 'en','el'. Optional.
 @param {array} elements if defined, govcy-elements to be rendered after the legend. 
    i.e. `[
            {element:"button",params:{text:{en:"Button 1",el:"Κουμπί 1"},lang:"en",id:"govcy-test-23b"} },
            {element:"button",params:{text:{en:"Button 2",el:"Κουμπί 2"},variant:'secondary',lang:"en",id:"govcy-test-23c"} },
        ]`
 @param {array} items The array of items to turn onto radio 
    i.e. `[
            {
                        value: "yes",
                        text: {en:"Yes",el:"Ναι"}
            },
            {
                value: "no",
                text: {en:"No",el:"Όχι"}
            },
            {
                type: "or",
                altOrText: {en:"If not",el:"Αν όχι"},
                value: "maybe",
                hint: {en:"We want you to be absolutely sure",el:"Θέλουμε να είστε απολύτως σίγουροι"},
                text: {en:"Maybe",el:"Ίσως"}
            }
        ]`
 @returns govcy radios html 
#}
{% macro radios(params) -%}
{#- set default values -#}
{%- set isPageHeading = params.isPageHeading | default(false) -%}
{%- set isInline = params.isInline | default(false) -%}
{#- legend, id and name are mandatory -#}
{%- if params.legend and params.id and params.name -%}
    {%- from "./fieldset.njk" import fieldset -%}
    {%- from "./hint.njk" import hint -%}
    {%- from "./legend.njk" import legend -%}
    {%- from "./errorMessage.njk" import errorMessage -%}
    {%- from "./formControl.njk" import formControl -%}
    {#- set hint id -#}
    {%- if params.hint -%}
        {%- set hintId = [params.id, "-hint"] | join -%}
    {%- else -%}
        {%- set hintId = '' -%}
    {%- endif -%}
    {#- set error id -#}
    {%- if params.error -%}
        {%- set errorId = [params.id, "-error"] | join -%}
    {%- else -%}
        {%- set errorId = '' -%}
    {%- endif -%}
    {# set aria described by #}
    {%- if params.error or params.hint -%}
        {% set ariaDescribedBy = hintId ~ ' ' ~ errorId %}
    {%- else -%}
        {% set ariaDescribedBy = '' %}
    {%- endif -%}
    {% call fieldset({ariaDescribedby:ariaDescribedBy,classes:params.classes, lang:params.lang}) %}
        {% call legend({legend:params.legend, isPageHeading:isPageHeading,lang:params.lang}) %}{% endcall %}
        {#- Import localizer from utilities -#}
        {%- from "../utilities/govcyUtilities.njk" import govcyElementsFromArray -%}
        {#- render elements -#}
        {{ govcyElementsFromArray(params.elements, params.lang) }}
        {% call formControl({isError: false if params.hideFormControlError else params.error}) %}
            {#- render hint -#}
            {% call hint({hint:params.hint, id:hintId,lang:params.lang}) %}{% endcall %}
            {#- render error message -#}
            {% call errorMessage({message:params.error,id:errorId,lang:params.lang}) %}{% endcall %}
            {# for each item render checkbox item #}
            {%- for item in params.items -%}
                {%- if item -%}
                    {{- _radioItem(params, item, isInline, loop.index, params.lang) -}}
                {%- endif -%}
            {%- endfor -%}
        {% endcall %}
    {% endcall %}
{%- endif -%}
{%- endmacro %}