{% import _self as link_macro %}
{% apply spaceless %}

{#
  Parameters:
   - "link" (associative array) (default: predefined structure): format:
      {
        type: '' (can be '', 'standalone', 'primary', 'primary-highlight', 'primary-neutral', 'secondary', 'secondary-neutral', 'secondary-inverted')
        inverted: false (boolean): Is the link inverted (displayed on dark background)?
        branded: false (boolean): Is the link using brand color (usually dark)?
        label: '' (string),
        path: '', (string) (Link url)
        aria_label: '' (string) Aria label attribute value
        icon_position: 'after' (string) (Can be 'before' or 'after'. Is required only if Icon is specified)
        external: (boolean) (default: false)
        sr_external: (string) (default: '') Additional label for external icon
        hide_label: (boolean) (default: false) hide link label, for screen reader only. Note: requires to have an icon defined
        indicator: (object) (default: {}) object of type Indicator. To be displayed it should be defined and not empty (should contain at least an empty value), and only if there is an icon and no label
      }
    - "icon" (associative array) OR (array) of associative arrays : format
      {
        name: (string) (default: ''),
        extra_classes: (string) (default: 'ecl-link__icon'),
        ...
      }
      OR
      [
        {
          name: (string) (default: ''),
          extra_classes: (string) (default: 'ecl-link__icon'),
          ....
        },
        {
          name: (string) (default: ''),
          extra_classes: (string) (default: 'ecl-link__icon'),
          ...
        }
        ...
      ]
    - "extra_classes" (string) (default: '') Extra classes (space separated)
    - "extra_attributes" (array) (default: []): format: [
        {
          "name" (string) Attribute name, eg. 'data-test'
          "value" (string) Attribute value, eg: 'data-test-1'
        }, ...
      ]
#}

{# Internal properties #}

{% set _link = {
  type: '',
  label: '',
  path: '',
  icon_position: 'after',
  external: false,
  sr_external: '',
  hide_label: false,
  indicator: {}
} %}
{% set _extra_accessibility = extra_accessibility|default({}) %}
{% set _as_image = false %}
{% set _label_id = '' %}
{% set _icon = {
  name: '',
  size: '',
  title: ''
} %}

{% set _css_class = 'ecl-link' %}
{% set _extra_attributes = '' %}

{% if link is defined %}
  {% set _link = _link|merge(link) %}
{% endif %}

{% if _link.external %}
  {% set _icons = [_icon|merge({
    name: 'external',
    size: '2xs',
    title: _extra_accessibility.title is empty and _icon.title is empty and _link.sr_external is not empty ? _link.sr_external : '',
  })] %}

  {% set _as_image = true %}

  {% set _link = _link|merge({ 
    icon_position: _link.icon_position|default('after'),
  }) %}
{% else %}
  {% if icon.name is defined and icon.name is not empty %}
    {% set _icons = [icon] %}
  {% elseif icon[1] is defined and icon[1] is not empty %}
    {% set _icons = icon %}
  {% endif %}
{% endif %}

{# Internal logic - Process properties #}

{% if _link.type is defined and _link.type is not empty %}
  {% set _css_class = _css_class ~ ' ' ~ _css_class ~ '--' ~ _link.type %}
{% endif %}

{% if _link.inverted is defined and _link.inverted %}
  {% set _css_class = _css_class ~ ' ecl-link--inverted' %}
{% endif %}

{% if _link.branded is defined and _link.branded %}
  {% set _css_class = _css_class ~ ' ecl-link--brand' %}
{% endif %}

{% if _icons is defined %}
  {% set _css_class = _css_class ~ ' ecl-link--icon' %}
{% endif %}

{% if extra_classes is defined and extra_classes is not empty %}
  {% set _css_class = _css_class ~ ' ' ~ extra_classes %}
{% endif %}

{% if _link.hide_label and _icon is not empty %}
  {% set _css_class = _css_class ~ ' ecl-link--icon-only' %}
{% endif %}

{% if _link.aria_label is defined and _link.aria_label is not empty %}
  {% set extra_attributes = extra_attributes|default([])|merge([{ name: 'aria-label', value: _link.aria_label }]) %}
{% endif %}

{% if extra_attributes is defined and extra_attributes is not empty and extra_attributes is iterable %}
  {% for attr in extra_attributes %}
    {% if attr.value is defined %}
      {% if attr.name == 'id' %}
        {% set _label_id = attr.value ~ '-label' %}
      {% endif %}
      {% set _extra_attributes = _extra_attributes ~ ' ' ~ attr.name|e('html_attr') ~ '="' ~ attr.value|e('html_attr') ~ '"' %}
    {% else %}
      {% set _extra_attributes = _extra_attributes ~ ' ' ~ attr.name|e('html_attr') %}
    {% endif %}
  {% endfor %}
{% endif %}

{# Define icon macro #}

{% macro icon_macro(_icons, _link, _as_image) %}{% apply spaceless %}
  {% import _self as link_macro %}

  {% for icon in _icons %}
    {%- if icon.name is not empty -%}
      {% set _icon_extra_classes = 'ecl-link__icon' %}
      {%- if icon.extra_classes is defined and icon.extra_classes is not empty -%}
        {% set _icon_extra_classes = _icon_extra_classes ~ ' ' ~ icon.extra_classes %}
      {%- endif -%}

      {% if _link.indicator is not empty %}
        <span class="ecl-link__icon-container">
          {%- include '@ecl/icon/icon.html.twig' with icon|merge({
            icon: icon|default({}),
            extra_classes: _icon_extra_classes,
            extra_accessibility: _extra_accessibility,
            as_image: _as_image,
          }) only -%}
          {% include '@ecl/indicator/indicator.html.twig' with _link.indicator only %}
        </span>
      {% else %}
        {%- include '@ecl/icon/icon.html.twig' with icon|merge({
          icon: icon|default({}),
          extra_classes: _icon_extra_classes,
          extra_accessibility: _extra_accessibility,
          as_image: _as_image,
        }) only -%}
      {% endif %}  
    {%- endif -%}
  {% endfor %}

{% endapply %}{% endmacro %}

{# Print the result #}

<a
  href="{{ _link.path }}"
  class="{{ _css_class }}"
  {{ _extra_attributes|raw }}
>
  {%- if _link.icon_position == 'before' and (_icons is defined or _link.external) -%}
    {{ link_macro.icon_macro(_icons, _link, _as_image) }}
  {%- endif -%}
  {%- if (_icons is defined and _icons is not empty) -%}
    <span 
      class="ecl-link__label"
    {%- if _label_id is not empty %}
      id="{{ _label_id }}"
    {%- endif -%}
    >
      {{- _link.label -}}
    </span>
  {%- endif -%}
  {%- if _link.icon_position == 'after' and (_icons is defined or _link.external) -%}
    {{ link_macro.icon_macro(_icons, _link, _as_image) }}
  {%- endif -%}
  {% if _icons is not defined %}
    {{- _link.label -}}
  {% endif %}
</a>

{% endapply %}
