{#
    Copyright (C) 2020 CERN.
    Copyright (C) 2020 Northwestern University.
    Copyright (C) 2021 Graz University of Technology.
    Copyright (C) 2021-2022 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 class="no-text-decoration" 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 class="ml-5 inline-id-icon" src="{{ url_for('static', filename='images/orcid.svg') }}" alt="ORCID icon"/>
      </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 class="ml-5 inline-id-icon" src="{{ url_for('static', filename='images/ror-icon.svg') }}" alt="ROR icon"/>
      </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 %}
  <dd class="creatibutor-wrap">
    <a class="ui creatibutor-link"
      {% if show_affiliations and creatibutor.affiliations %}
        data-tooltip="{{ 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 + '"')}}"
    >

      <span class="creatibutor-name">
        {{- creatibutor.person_or_org.name -}}
      </span>
      {%- if creatibutor.affiliations -%}
      <sup class="font-tiny">
        {%- for affiliation in creatibutor.affiliations -%}
          {{ affiliation[0] }}{{ ", " if not loop.last }}
        {%- endfor -%}
      </sup>
      {%- endif -%}
    </a>
    {{- creatibutor_icon(creatibutor) -}}
    {%- if not loop.last -%}
       <span class="separator">;</span>
    {%- endif -%}
  </dd>
  {% endfor %}
{%- endmacro %}


{% macro affiliations_accordion(group, affiliations) %}
<div class="ui sixteen wide tablet three wide computer column title right aligned">
    <button class="ui affiliations-button button mini up mr-0" aria-controls="{{ group }}-affiliations">{{ _("Show affiliations") }}</button>
    <button class="ui affiliations-button button mini down mr-0" aria-controls="{{ group }}-affiliations">{{ _("Hide affiliations") }}</button>
</div>

<section class="ui sixteen wide column content text-muted" id="{{ group }}-affiliations" aria-label="{{ _('Affiliations for') }} {{ group }}">
  <dl>
  {% for affiliation in affiliations %}
      <dd>
          {{ affiliation[0] }}.

          {% if affiliation[2] %}
          <a class="no-text-decoration"
             href="https://ror.org/{{affiliation[2]}}"
             aria-label="{{ affiliation[1] }}'s ROR {{ _('profile') }}"
             title="{{ affiliation[1] }}'s ROR {{ _('profile') }}"
          >
              <img class="ml-5 inline-id-icon" src="{{ url_for('static', filename='images/ror-icon.svg') }}" alt="ROR icon"/>
          </a>
          {%- endif %}
          {{affiliation[1]}}
      </dd>
  {% endfor %}
  </dl>
</section>
{% endmacro %}
