{% from 'govuk/components/character-count/macro.njk' import govukCharacterCount %}

{% set fieldErrors = params.casaErrors[params.name] %}

{# Generate validation data- attributes #}
{% set mergedAttributes = params.attributes or {} %}
{% if fieldErrors %}
  {% set mergedAttributes = mergeObjects(mergedAttributes, {
    'data-validation': {fn: params.name, va: fieldErrors[0].validator} | dump
  }) %}
{% endif %}

{# Merge parameters #}
{% set mergedParams = mergeObjects(params, {
  id: params.id if params.id else 'f-' + params.name,
  attributes: mergedAttributes,
  textareaDescriptionText: params.textareaDescriptionText or (
    t("common:form.characterCount.words.description") if params.maxwords
    else t("common:form.characterCount.characters.description")
  ),
  charactersUnderLimitText: params.charactersUnderLimitText or {
    one: t("common:form.characterCount.characters.underLimitOne"),
    two: t("common:form.characterCount.characters.underLimitTwo"),
    few: t("common:form.characterCount.characters.underLimitFew"),
    many: t("common:form.characterCount.characters.underLimitMany"),
    other: t("common:form.characterCount.characters.underLimitOther")
  } if not params.maxwords,
  charactersAtLimitText: params.charactersAtLimitText or t("common:form.characterCount.characters.atLimit") if not params.maxwords,
  charactersOverLimitText: params.charactersOverLimitText or {
    one: t("common:form.characterCount.characters.overLimitOne"),
    two: t("common:form.characterCount.characters.overLimitTwo"),
    few: t("common:form.characterCount.characters.overLimitFew"),
    many: t("common:form.characterCount.characters.overLimitMany"),
    other: t("common:form.characterCount.characters.overLimitOther")
  } if not params.maxwords,
  wordsUnderLimitText: params.wordsUnderLimitText or {
    one: t("common:form.characterCount.words.underLimitOne"),
    two: t("common:form.characterCount.words.underLimitTwo"),
    few: t("common:form.characterCount.words.underLimitFew"),
    many: t("common:form.characterCount.words.underLimitMany"),
    other: t("common:form.characterCount.words.underLimitOther")
  } if params.maxwords,
  wordsAtLimitText: params.wordsAtLimitText or t("common:form.characterCount.words.atLimit") if params.maxwords,
  wordsOverLimitText: params.wordsOverLimitText or {
    one: t("common:form.characterCount.words.overLimitOne"),
    two: t("common:form.characterCount.words.overLimitTwo"),
    few: t("common:form.characterCount.words.overLimitFew"),
    many: t("common:form.characterCount.words.overLimitMany"),
    other: t("common:form.characterCount.words.overLimitOther")
  } if params.maxwords,
  errorMessage: {
    text: t(params.casaErrors[params.name][0].inline, params.casaErrors[params.name][0].variables),
    attributes: {
      'data-ga-question': params.label.text or params.label.html | striptags
    } if params.casaWithAnalytics else {}
  } if params.casaErrors[params.name].length
}) %}

{{ govukCharacterCount(mergedParams) }}
