{% set countryId = countryId|default(form_value('country_id')) %}
{% set stateId = stateId|default(form_value('state_id')) %}

{# If only one country is available it can be preselected. #}
{% if (__SELF__.countries | length) == 1 %}
    {% set countryId = (__SELF__.countries | keys)[0] %}
{% endif %}

{% set id = type ~ 'country_id' %}
<div class="mall-form-control">
    <label for="{{ id }}">{{ 'offline.mall::frontend.form.country' | trans }}</label>
    {{ form_select_country(id, countryId, {
        id: id,
        'class': 'form-select',
        emptyOption: ('offline.mall::frontend.select' | trans ),
        'data-request': 'onInit',
        'data-request-update': {
            (__SELF__ ~ '::countrystate'): '#countryState'
        }
    }) }}
    <div data-validate-for="{{ type }}country_id"></div>
</div>

{% if __SELF__.useState %}
    {% set id = type ~ 'state_id' %}
    <div class="mall-form-control">
        <label for="{{ id }}">{{ 'offline.mall::frontend.form.state' | trans }}</label>
        {{ form_select_state(id, countryId, stateId, {
            emptyOption: ('offline.mall::frontend.select' | trans ),
            id: id,
            'class': 'form-select',
        }) }}
        <div data-validate-for="{{ id }}"></div>
    </div>
{% endif %}