{#
    Copyright (C) 2020 CERN.
    Copyright (C) 2020 Northwestern University.
    Copyright (C) 2021 Graz University of Technology.
    Copyright (C) 2021 New York University.

    Invenio RDM Records is free software; you can redistribute it and/or modify
    it under the terms of the MIT License; see LICENSE file for more details.
#}

{% macro creatibutor_icon(creatibutor) %}
{% set identifier_found = namespace(value=False) %}
{% for scheme, identifier in creatibutor.person_or_org.identifiers|groupby("scheme") %}
{%- if scheme == "orcid" %}
{% set identifier_found.value = True %}
<a href="{{ identifier[0]['identifier']|pid_url('orcid') }}" aria-label="{{ creatibutor.person_or_org.name }}'s ORCID Profile" title="{{ creatibutor.person_or_org.name }}'s ORCID Profile">
  <img alt="" class="inline-id-icon" src="{{ url_for('static', filename='images/orcid.svg') }}" />
</a>
{%- elif scheme == "ror" %}
{% set identifier_found.value = True %}
<a href="{{ identifier[0]['identifier']|pid_url('ror') }}" aria-label="{{ creatibutor.person_or_org.name }}'s ROR Profile" title="{{ creatibutor.person_or_org.name }}'s ROR Profile">
  <img alt=""  class="inline-id-icon" src="{{ url_for('static', filename='images/ror-icon.svg') }}" />
</a>
{%- endif %}
{% endfor %}
{# if no identifiers: distinguish btw people and organizations #}
{%- if not identifier_found.value and creatibutor.person_or_org.type == 'organizational'%}
<i class="group icon"></i>
{%- endif %}
{% endmacro %}

{% macro show_creatibutors(creatibutors, show_affiliations=False) %}
  {% for creatibutor in creatibutors %}
    {{ creatibutor_icon(creatibutor) }}
    <a
      class="ui tooltip-popup text-muted"
      {% if show_affiliations and creatibutor.affiliations %}
        data-content="{{ creatibutor.affiliations|join('; ', attribute='1') }}"
      {% endif %}
      href="{{url_for('invenio_search_ui.search', q='metadata.creators.person_or_org.name:"' + creatibutor.person_or_org.name + '"')}}">
      {{ creatibutor.person_or_org.name }}
      </a>
    <sup class="font-tiny">
      {% for affiliation in creatibutor.affiliations %}
        {{ affiliation[0] }}{{ ", " if not loop.last }}
      {% endfor %}
    </sup>
    {{ "; " if not loop.last }}
    {% endfor %}

{%- endmacro %}

{% macro show_affiliations(affiliations) %}
  {% for affiliation in affiliations %}
      {{ affiliation[0] }}.
      {%- if affiliation[2] %}
        <a href="https://ror.org/{{affiliation[2]}}"><img class="inline-id-icon" src="{{ url_for('static', filename='images/ror-icon.svg') }}"/></a>
      {%- endif %}
      {{affiliation[1]}}
    <br />
  {% endfor %}
{%- endmacro %}
