{% set hub = get_origin("hub.sr.ht", default=None, external=True) %}
{% set git = get_origin("git.sr.ht", default=None, external=True) %}
{% set hg = get_origin("hg.sr.ht", default=None, external=True) %}
{% set lists = get_origin("lists.sr.ht", default=None, external=True) %}
{% set todo = get_origin("todo.sr.ht", default=None, external=True) %}

{% macro project_nav(res, projects, fluid) %}
{% if fluid %}
{% set cls = "container-fluid" %}
{% else %}
{% set cls = "container" %}
{% endif %}

{% if len(projects) == 1 %}
{% set project = projects[0] %}

<div class="header-tabbed project-nav">
  <div class="{{cls}}">
    <h2>
      <a
        href="{{hub}}/{{project.owner.canonical_name}}"
      >{{project.owner.canonical_name}}</a>/<wbr>{{project.name}}
    </h2>
    <ul class="nav nav-tabs">
      <li class="nav-item">
        <a
          class="nav-link nav-project-back"
          href="{{hub}}/{{project.owner.canonical_name}}/{{project.name}}"
        >{{icon('caret-left')}} project</a>
      </li>

      {% if project.sources.results %}
      {{ source_tab(project) }}
      {% endif %}

      {% if project.mailing_lists.results %}
      {{ mailing_list_tab(project) }}
      {% endif %}

      {% if project.trackers.results %}
      {{ tracker_tab(project) }}
      {% endif %}
    </ul>
  </div>
</div>
{% elif len(projects) > 1 %}
<div class="header-tabbed project-nav">
  <div class="{{cls}}">
    <h2>
      <a
        href="{{hub}}/{{res.owner.canonical_name}}"
      >{{res.owner.canonical_name}}</a>/<wbr>{{res.name}}
    </h2>
    <ul class="nav nav-tabs">
      <li class="nav-item">
        <a
          class="nav-link nav-project-back"
          href="{{hub}}/resource/{{to_rid(res.rid)}}"
        >{{icon('caret-left')}} projects ({{len(projects)}})</a>
      </li>
    </ul>
  </div>
</div>
{% else %}
<div class="header-tabbed project-nav">
  <div class="{{cls}}">
    <h2>
      <a
        href="{{hub}}/{{res.owner.canonical_name}}"
      >{{res.owner.canonical_name}}</a>/<wbr>{{res.name}}
    </h2>
  </div>
</div>
{% endif %}
{% endmacro %}

{% macro source_tab(project) %}
{% set many = len(project.sources.results) > 1 %}
{% if not many %}
{% set source = project.sources.results[0] %}
{% endif %}

{% if many %}
{% set url = "{hub}/{owner}/{name}/sources".format(
  hub=hub, owner=project.owner.canonical_name, name=project.name) %}

{% elif source.repo_type.value == 'GIT' %}
{% set url = "{git}/{owner}/{name}".format(
  git=git, owner=project.owner.canonical_name, name=source.name) %}

{% elif source.repo_type.value == 'HG' %}
{% set url = "{hg}/{owner}/{name}".format(
  hg=hg, owner=project.owner.canonical_name, name=source.name) %}
{% endif %}

<li class="nav-item">
  <a
    class="nav-link {% if site in ['git.sr.ht', 'hg.sr.ht'] %}active{% endif %}"
    href="{{url}}"
  >source{% if many %}s{% endif %}</a>
</li>
{% endmacro %}

{% macro mailing_list_tab(project) %}
{% set many = len(project.mailing_lists.results) > 1 %}
{% if not many %}
{% set ml = project.mailing_lists.results[0] %}
{% endif %}

{% if many %}
{% set url = "{hub}/{owner}/{name}/lists".format(
  hub=hub, owner=project.owner.canonical_name, name=project.name) %}
{% else %}
{% set url = "{lists}/{owner}/{name}".format(
  lists=lists, owner=project.owner.canonical_name, name=ml.name) %}
{% endif %}

<li class="nav-item">
  <a
    class="nav-link {% if site == 'lists.sr.ht' %}active{% endif %}"
    href="{{url}}"
  >mailing list{% if many %}s{% endif %}</a>
</li>
{% endmacro %}

{% macro tracker_tab(project) %}
{% set many = len(project.trackers.results) > 1 %}
{% if not many %}
{% set tracker = project.trackers.results[0] %}
{% endif %}

{% if many %}
{% set url = "{hub}/{owner}/{name}/trackers".format(
  hub=hub, owner=project.owner.canonical_name, name=project.name) %}
{% else %}
{% set url = "{todo}/{owner}/{name}".format(
  todo=todo, owner=project.owner.canonical_name, name=tracker.name) %}
{% endif %}

<li class="nav-item">
  <a
    class="nav-link {% if site == 'todo.sr.ht' %}active{% endif %}"
    href="{{url}}"
  >tickets</a>
</li>
{% endmacro %}
