{% from "nhsuk/macros/attributes.njk" import nhsukAttributes %}
{% from "nhsuk/components/error-message/macro.njk" import errorMessage -%}
{% from "nhsuk/components/hint/macro.njk" import hint %}
{% from "nhsuk/components/label/macro.njk" import label %}

{#- Set classes for this component #}
{%- set classNames = "nhsuk-select" -%}
{%- set classNamesFormGroup = "nhsuk-form-group" -%}

{%- if params.errorMessage %}
  {% set classNames = classNames ~ " nhsuk-select--error" %}
  {% set classNamesFormGroup = classNamesFormGroup ~ " nhsuk-form-group--error" %}
{% endif %}

{%- if params.classes %}
  {% set classNames = classNames ~ " " ~ params.classes %}
{% endif %}

{%- if params.formGroup.classes %}
  {% set classNamesFormGroup = classNamesFormGroup ~ " " ~ params.formGroup.classes %}
{% endif %}

{#- a record of other elements that we need to associate with the input using
  aria-describedby – for example hints or error messages -#}
{% set describedBy = params.describedBy if params.describedBy else "" %}
{%- set id = params.id if params.id else params.name -%}

{%- set hasBeforeInput = true if params.formGroup.beforeInput and (params.formGroup.beforeInput.text or params.formGroup.beforeInput.html) else false %}
{%- set hasAfterInput = true if params.formGroup.afterInput and (params.formGroup.afterInput.text or params.formGroup.afterInput.html) else false %}

{%- macro _selectElement(params) -%}
  <select class="{{ classNames }}" id="{{ id }}" name="{{ params.name }}"
    {%- if params.disabled %} disabled{% endif %}
    {%- if describedBy %} aria-describedby="{{ describedBy }}"{% endif %}
    {{- nhsukAttributes(params.attributes) }}>
  {% for item in params.items %}
  {%- if item.divider %}
    <hr>
  {% elif item %}
    {#- Allow selecting by text content (the value for an option when no value attribute is specified) #}
    {%- set effectiveValue = item.value | default(item.text) %}
    <option {%- if item.value is not undefined %} value="{{ item.value }}"{% endif %}
      {{- " selected" if item.selected | default((effectiveValue == params.value and item.selected != false) if params.value else false, true) }}
      {{- " disabled" if item.disabled }}
      {{- nhsukAttributes(item.attributes) }}>
      {{- item.text -}}
    </option>
    {% endif %}
  {% endfor %}
  </select>
{%- endmacro -%}

<div class="{{ classNamesFormGroup }}" {{- nhsukAttributes(params.formGroup.attributes) }}>
  {{ label({
    html: params.label.html,
    text: params.label.text,
    id: params.label.id,
    for: params.label.for | default(id),
    size: params.label.size,
    isPageHeading: params.label.isPageHeading,
    classes: params.label.classes,
    attributes: params.label.attributes
  }) | trim | indent(2) }}
{% if params.hint %}
  {% set hintId = params.hint.id | default(id ~ "-hint") %}
  {% set describedBy = (describedBy ~ " " ~ hintId) | trim %}
  {{ hint({
    html: params.hint.html,
    text: params.hint.text,
    id: params.hint.id | default(hintId),
    classes: params.hint.classes,
    attributes: params.hint.attributes
  }) | trim | indent(2) }}
{% endif %}
{% if params.errorMessage.text or params.errorMessage.html %}
  {% set errorId = params.errorMessage.id | default(id ~ "-error") %}
  {% set describedBy = (describedBy ~ " " ~ errorId) | trim %}
  {{ errorMessage({
    html: params.errorMessage.html,
    text: params.errorMessage.text,
    visuallyHiddenText: params.errorMessage.visuallyHiddenText,
    id: params.errorMessage.id | default(errorId),
    classes: params.errorMessage.classes,
    attributes: params.errorMessage.attributes
  }) | trim | indent(2) }}
{% endif %}

{%- if hasBeforeInput or hasAfterInput or params.inputWrapper.classes or params.inputWrapper.attributes %}
  <div class="nhsuk-input-wrapper {%- if params.inputWrapper.classes %} {{ params.inputWrapper.classes }}{% endif %}"
    {{- nhsukAttributes(params.inputWrapper.attributes) }}>
    {% if hasBeforeInput %}
      {{- params.formGroup.beforeInput.html | safe | trim | indent(4, true) if params.formGroup.beforeInput.html else params.formGroup.beforeInput.text }}
    {% endif %}
    {{ _selectElement(params) }}
    {% if hasAfterInput %}
      {{- params.formGroup.afterInput.html | safe | trim | indent(4, true) if params.formGroup.afterInput.html else params.formGroup.afterInput.text }}
    {% endif %}
  </div>
{% else %}
  {{ _selectElement(params) }}
{% endif %}
</div>
