{#
/**
 * Available variables:
 * - link_content - the content of the link (typically text)
 * - link_url - the url this link should point to
 * - link_attributes - array of attribute,value pairs
 * - link_base_class - the base class name
 * - link_modifiers - array of modifiers to add to the base classname
 * - link_icon - an icon to suppliment the link
 * NB: link should support icon with a11y built in, therefore icon doesn't replace content but supplements it for visual rendering.
 *
 * Available blocks:
 * - link_content - used to replace the content of the link
 *     Example: to insert the image component
 */
#}
{% set link_base_class = link_base_class|default('mds-link') %}

{# Set local_link_attributes to link_attributes or an empty object so we can use/modify it locally more easily. #}
{% set local_link_attributes = link_attributes is defined ? link_attributes : {} %}

<a class="{{ link_base_class }} {{ link_modifiers|join(' ') }} {{ link_icon ? 'mds-link__icon' }} "

  {# Set a fallback for the aria-label attribute for the link if rendering a link that *only* has an icon so at least there's a label for screen-readers. If there is an aria-label already set in local_link_attributes, do nothing since it will be set by attribute rendering loop below. #}
  {% if 'aria-label' not in local_link_attributes|keys and link_content is not defined and link_icon is defined %}

    {# Set aria-label value to use the link_icon since the icon names are usually at least somewhat semantic and merge with existing local_link_attributes object. #}
    {% set local_link_attributes = local_link_attributes| merge ({'aria-label': link_icon})%}

  {% endif %}
  
  {# Loop through and set available attributes. #}
  {% for attribute, value in local_link_attributes %}
    {{ attribute }}="{{ value }}"
  {% endfor %}
href="{{ link_url }}"
>

  {# Include the link_icon if there is one.  #}
  {% if link_icon %}
    {% include "@mds/icon/icon.twig" with {icon_alt: link_icon_alt, icon_name: link_icon, icon_size: link_icon_size, icon_fallback: link_icon ~ " icon" } %}
  {% endif %}

  {% block link_content %}
    {% if link_icon_label %}
        <span class="mds-link__icon-label">{{ link_content }}</span>
     {% else %}
       {{ link_content }}
    {% endif %}
  {% endblock %}
</a>
