{% assign cohort = include.cohort | strip %}
{% assign people = site.data.people %}
{% assign cohort_data = site.data.openseeds[cohort] %}
{% assign projects = cohort_data.projects %}
{% assign experts = cohort_data.metadata.experts %}

{% assign cohorts = site.data.openseeds | sort %}
{% for c in cohorts %}
    {% if c[0] == cohort %} 
        {% assign current_cohort_index = forloop.index0 %}
    {% endif %}
{% endfor %}
{% assign prev_cohort_index = current_cohort_index | minus: 1 %}
{% for c in cohorts %}
    {% if forloop.index0 == prev_cohort_index %} 
        {% assign prev_cohort = c %}
    {% endif %}
{% endfor %}

<!-- extract previous participants and mentors and count them later among mentors -->
{% assign prev_projects = prev_cohort[1].projects %}
{% assign prev_participants = '' %}
{% assign prev_mentors = '' %}
{% for project in prev_projects %}
    {% for p in project.participants %}
        {% capture prev_participants %}{{ prev_participants }}, {{ p }}{% endcapture %}
    {% endfor %}
    {% for m in project.mentors %}
        {% capture prev_mentors %}{{ prev_mentors }}, {{ m }}{% endcapture %}
    {% endfor %}
{% endfor %}

{% assign all-participants = '' %}
{% assign all-p-countries = '' %}
{% assign all-mentors = '' %}
{% assign all-m-countries = '' %}
{% assign all-keywords = '' %}
{% assign prev-part-count = 0 %}
{% assign prev-mentor-count = 0 %}

<!-- parse project participants, mentors and their countries -->
{% for project in projects %}
    {% for p in project.participants %}
        {% capture all-participants %}{{ all-participants }}, [{{ people[p].first-name }} {{ people[p].last-name }}]({% link openseeds/{{ cohort }}/projects-participants.md %}#{{ p }}){% endcapture %}
        {% assign p_country = people[p].country | strip %}
        {% if p_country != '' %}
        {% capture all-p-countries %}{{ all-p-countries }}, {{ p_country }}{% endcapture %}
        {% endif %}
    {% endfor %}

    {% for m in project.mentors %}
        {% capture all-mentors %}{{ all-mentors }}, [{{ people[m].first-name }} {{ people[m].last-name }}]({% link openseeds/{{ cohort }}/index.md %}#{{ m }}){% endcapture %}
        {% assign m_country = people[m].country | strip %}
        {% if m_country != '' %}
        {% capture all-m-countries %}{{ all-m-countries }}, {{ m_country }}{% endcapture %}
        {% endif %}
<!-- add +1 if participant in previous cohort -->
        {% if prev_participants contains m %}
            {% assign prev-part-count = prev-part-count | plus: 1 %}
        {% endif %}
<!-- add +1 if mentor in previous cohort -->
        {% if prev_mentors contains m %}
            {% assign prev-mentor-count = prev-mentor-count | plus: 1 %}
        {% endif %}
    {% endfor %}

<!-- parse keywords -->
    {% for k in project.keywords %}
        {% capture all-keywords %}{{ all-keywords }}, {{ k }}{% endcapture %}
    {% endfor %}
{% endfor %}

<!-- transform into lists -->
{% assign p-participants = all-participants | remove_first: ', ' | split: ", " | uniq | sort %}
{% assign p-countries = all-p-countries | remove_first: ', ' | split: ", " | uniq | sort %}
{% assign p-mentors = all-mentors | remove_first: ', ' | split: ", " | uniq | sort %}
{% assign m-countries = all-m-countries | remove_first: ', ' | split: ", " | uniq | sort %}
{% assign keywords = all-keywords | remove_first: ', ' | split: ", " | uniq | sort %}