{% from "nhsuk/components/error-message/macro.njk" import errorMessage as nhsukErrorMessage %}
{% from "nhsuk/components/hint/macro.njk" import hint as nhsukHint %}
{% from "nhsuk/components/label/macro.njk" import label as nhsukLabel %}
{% from "nhsuk/macros/attributes.njk" import nhsukAttributes %}
{% from "nhsuk/macros/content.njk" import nhsukContent %}
{% from "nhsuk/macros/i18n.njk" import nhsukI18nAttributes %}

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

{#- Support label as string #}
{%- set label = params.label if params.label is mapping and params.label is not escaped else {
  text: params.label if params.label is string else undefined,
  html: params.label if params.label is escaped else undefined
} %}

{#- Support hint as string #}
{%- set hint = params.hint if params.hint is mapping and params.hint is not escaped else {
  text: params.hint if params.hint is string else undefined,
  html: params.hint if params.hint is escaped else undefined
} %}

{#- Support error message as string #}
{%- set errorMessage = params.errorMessage if params.errorMessage is mapping and params.errorMessage is not escaped else {
  text: params.errorMessage if params.errorMessage is string else undefined,
  html: params.errorMessage if params.errorMessage is escaped else undefined
} %}

{%- if errorMessage.text or errorMessage.html or params.errorMessage is sameas(true) %}
  {%- 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 -%}

{%- macro _inputElement(params, parent) -%}
<input class="{{ parent.classNames }}" id="{{ parent.id }}" name="{{ params.name }}" type="file"
  {%- if params.disabled %} disabled{% endif %}
  {%- if params.multiple %} multiple{% endif %}
  {%- if parent.describedBy %} aria-describedby="{{ parent.describedBy }}"{% endif %}
  {{- nhsukAttributes(params.attributes) }}>
{%- endmacro -%}

<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) }}>
  {{ nhsukLabel({
    html: label.html,
    text: label.text,
    visuallyHiddenText: label.visuallyHiddenText,
    caption: label.caption,
    id: label.id,
    for: label.for | default(id),
    size: label.size,
    heading: label.heading,
    isPageHeading: label.isPageHeading,
    classes: label.classes,
    attributes: label.attributes
  }) | trim | indent(2) }}
{% if hint.text or hint.html %}
  {%- set hintId = hint.id | default(id ~ "-hint") %}
  {%- set describedBy = (describedBy ~ " " ~ hintId) | trim %}
  {{ nhsukHint({
    html: hint.html,
    text: hint.text,
    id: hint.id | default(hintId),
    classes: hint.classes,
    attributes: hint.attributes
  }) | trim | indent(2) }}
{% endif %}
{% if errorMessage.text or errorMessage.html %}
  {%- set errorId = errorMessage.id | default(id ~ "-error") %}
  {%- set describedBy = (describedBy ~ " " ~ errorId) | trim %}
  {{ nhsukErrorMessage({
    html: errorMessage.html,
    text: errorMessage.text,
    visuallyHiddenText: errorMessage.visuallyHiddenText,
    id: errorMessage.id | default(errorId),
    classes: errorMessage.classes,
    attributes: errorMessage.attributes
  }) | trim | indent(2) }}
{% endif %}
  {{ nhsukContent({
    html: _inputElement(params, {
      id: id,
      describedBy: describedBy,
      classNames: classNames
    }) | trim,
    slots: {
      before: params.formGroup.beforeInput,
      after: params.formGroup.afterInput
    }
  }) | trim | indent(2) }}
</div>
