{% from '@bonniernews/dn-design-system-web/njk-helpers/attributes.njk' import getAttributes %}

{% macro ImageCaption(params) %}
  {% set componentClassName = "ds-image-caption" %}
  {% set attributes = getAttributes(params.attributes) %}

  {%- set classes = [
    componentClassName,
    "ds-force-px" if params.forcePx,
    params.classNames if params.classNames
  ] | join(" ") %}

  <figcaption class="{{ classes }}" {{- attributes | safe }} aria-hidden="true">
    {% if params.caption %}
      <span>{{ params.caption | safe }}</span>
    {% endif %}
    {% if params.author %}
      {% set authorText = ((params.imageType | capitalize) + ": " + params.author) if params.imageType else params.author %}

      {% set authorClasses = [ "ds-article-image__credits" ] %}
      {# Low estimate of how much will fit on tablet/desktop #}
      {% if (authorText | length) >= 27 and (authorText | length) < 60 %}
        {% set authorClasses = (authorClasses.push("ds-article-image__credits--nowrap-bigscreen"), authorClasses) %}
      {# Low estimate of how much will fit on 320px mobile #}
      {% elif (authorText | length) < 27 %}
        {% set authorClasses = (authorClasses.push("ds-article-image__credits--nowrap"), authorClasses) %}
      {% endif %}

      <span class="{{ authorClasses | join(" ") }}">
        {{- authorText -}}
      </span>
    {% endif %}
  </figcaption>
{% endmacro %}
