{% from "nhsuk/macros/attributes.njk" import nhsukAttributes %}
{% from "nhsuk/macros/i18n.njk" import nhsukI18nAttributes %}
{% 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-file-upload__input" -%}
{%- set classNamesFormGroup = "nhsuk-form-group" -%}

{%- if params.errorMessage %}
  {% 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 -%}

<div
  {{- nhsukAttributes({
    class: classNamesFormGroup ~ " nhsuk-file-upload",
    "data-module": "nhsuk-file-upload",
    "data-choose-files-button-class-list": {
      value: params.chooseFilesButtonClassList,
      type: "array",
      optional: true
    }
  }) -}}

  {{- nhsukI18nAttributes({
    key: "choose-files-button",
    message: params.chooseFilesButtonText
  }) -}}

  {{- nhsukI18nAttributes({
    key: "no-file-chosen",
    message: params.noFileChosenText
  }) -}}

  {{- nhsukI18nAttributes({
    key: "multiple-files-chosen",
    messages: params.multipleFilesChosenText
  }) -}}

  {{- nhsukI18nAttributes({
    key: "drop-instruction",
    message: params.dropInstructionText
  }) -}}

  {{- nhsukI18nAttributes({
    key: "entered-drop-zone",
    message: params.enteredDropZoneText
  }) -}}

  {{- nhsukI18nAttributes({
    key: "left-drop-zone",
    message: params.leftDropZoneText
  }) -}}

  {{- 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 params.formGroup.beforeInput %}
  {{ params.formGroup.beforeInput.html | safe | trim | indent(2) if params.formGroup.beforeInput.html else params.formGroup.beforeInput.text }}
{% endif %}
  <input class="{{ classNames }}" id="{{ id }}" name="{{ params.name }}" type="file"
    {%- if params.disabled %} disabled{% endif %}
    {%- if params.multiple %} multiple{% endif %}
    {%- if describedBy %} aria-describedby="{{ describedBy }}"{% endif %}
    {{- nhsukAttributes(params.attributes) }}>
{% if params.formGroup.afterInput %}
  {{ params.formGroup.afterInput.html | safe | trim | indent(2) if params.formGroup.afterInput.html else params.formGroup.afterInput.text }}
{% endif %}
</div>
