{#
    Copyright (C) 2020-2024 CERN.
    Copyright (C) 2020 Northwestern University.
    Copyright (C) 2021 Graz University of Technology.
    Copyright (C) 2021-2022 New York University.
    Copyright (C) 2025 KTH Royal Institute of Technology.

    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) %}

  {% if config.APP_RDM_IDENTIFIER_SCHEMES_UI %}
    {% for scheme, details in config.APP_RDM_IDENTIFIER_SCHEMES_UI.items() %}
      {% for identifier in creatibutor.person_or_org.identifiers|selectattr("scheme", "equalto", scheme) %}
        {% set identifier_found.value = True %}
        <a href="{{ identifier.identifier|pid_url(scheme) }}"
           aria-label="{{ creatibutor.person_or_org.name }}'s {{ details.label }} {{ _('profile') }}"
           title="{{ creatibutor.person_or_org.name }}'s {{ details.label }} {{ _('profile') }}">
          <img class="ml-5 inline-id-icon"
               src="{{ url_for('static', filename=details.icon) }}"
               alt="{{ details.label }} icon"/>
        </a>
      {% endfor %}
    {% endfor %}
  {% endif %}

  {# Fallback for missing identifiers #}
  {% if not identifier_found.value %}
    {% if creatibutor.person_or_org.type == 'organizational' %}
      <i class="group icon"></i>
    {% else %}
      <i class="user icon"></i>
    {% endif %}
  {% endif %}
{% endmacro %}



{% macro show_creatibutors(creatibutors, show_affiliations=False, type="creators", show_role=False) %}
  {% for creatibutor in creatibutors if creatibutor.person_or_org and creatibutor.person_or_org.name %}
  <li class="creatibutor-wrap separated">
    <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.' + type + '.person_or_org.name:"' + creatibutor.person_or_org.name + '"')}}"
    >

      <span class="creatibutor-name">
        {{- creatibutor.person_or_org.name -}}
        {% if show_role and creatibutor.role %}
          ({{- creatibutor.role.title -}})
        {%- endif -%}
      </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) -}}
  </li>
  {% endfor %}
{%- endmacro %}


{% macro affiliations_accordion(group, affiliations) %}
<div class="ui sixteen wide tablet three wide computer column title right aligned bottom aligned">
  <button class="ui affiliations-button trigger button mini mr-0"
          aria-controls="{{ group }}-affiliations"
          data-open-text="{{_('Show affiliations')}}"
          data-close-text="{{ _('Hide affiliations') }}"
          aria-expanded="false"
  >
    {{ _("Show affiliations") }}
  </button>
</div>

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

      {% if affiliation[2] %}
        {% set scheme, identifier = (affiliation[2].split(':', 1) if ':' in affiliation[2] else ('ror', affiliation[2])) %}
        {% set scheme_config = config.APP_RDM_IDENTIFIER_SCHEMES_UI.get(scheme) %}

        {% if scheme_config %}
          <a class="no-text-decoration"
             href="{{ scheme_config.url_prefix + identifier }}"
             aria-label="{{ affiliation[1] }}'s {{ scheme_config.label }} {{ _('profile') }}"
             title="{{ affiliation[1] }}'s {{ scheme_config.label }} {{ _('profile') }}"
          >
            <img class="ml-5 inline-id-icon"
                 src="{{ url_for('static', filename=scheme_config.icon) }}"
                 alt="{{ scheme_config.label }} icon"
            />
          </a>
        {% endif %}
      {% endif %}

      {{ affiliation[1] }}
    </li>
  {% endfor %}

    </ul>
</section>
{% endmacro %}
