{% import 'macros/form.html' as form %}

{%- set options=[] -%}
{%- set form_restrict_choices_to=field.get('form_restrict_choices_to') -%}
{%- if not h.scheming_field_required(field) or
    field.get('form_include_blank_choice', false) -%}
    {%- do options.append({'value': '', 'text': ''}) -%}
{%- endif -%}
{%- for c in h.scheming_field_choices(field) -%}
    {%- if not form_restrict_choices_to or c.value in form_restrict_choices_to -%}
        {%- do options.append({
      'value': c.value|string,
      'text': h.scheming_language_text(c.label) }) -%}
    {%- endif -%}
{%- endfor -%}
{%- if field.get('sorted_choices') -%}
    {%- set options = options|sort(case_sensitive=false, attribute='text') -%}
{%- endif -%}
{%- if data[field.field_name] -%}
    {%- set option_selected = data[field.field_name]|string -%}
{%- else -%}
    {%- set option_selected = None -%}
{%- endif -%}

{% call form.select(
    field.field_name,
    id='field-' + field.field_name,
    label=h.scheming_language_text(field.label),
    options=options,
    selected=option_selected,
    error=errors[field.field_name],
    classes=['control-medium'],
    attrs=field.form_attrs if 'form_attrs' in field else {
      "data-module": "autocomplete"
    },
    is_required=h.scheming_field_required(field)
    )
        %}
    {%- snippet 'scheming/form_snippets/help_text.html', field=field -%}
{% endcall %}