{% comment %}
Copyright (C) 2017-2025  The Software Heritage developers
See the AUTHORS file at the top-level directory of this distribution
License: GNU Affero General Public License version 3, or any later version
See top-level LICENSE file for more information
{% endcomment %}

{% load swh_templatetags %}

<div class="swh-browse-top-navigation d-flex align-items-start justify-content-between flex-wrap mt-1">
  {% if snapshot_context %}
    {% if snapshot_context.branch or snapshot_context.release or snapshot_context.revision_id %}
      <div class="dropdown" id="swh-branches-releases-dd">
        <button class="btn btn-block btn-secondary btn-sm dropdown-toggle"
                type="button"
                data-bs-toggle="dropdown">
          {% if snapshot_context.branch %}
            {% if snapshot_context.branch_alias %}
              <i class="{{ swh_object_icons.alias }} mdi-fw" aria-hidden="true"></i>
            {% else %}
              <i class="{{ swh_object_icons.branch }} mdi-fw" aria-hidden="true"></i>
            {% endif %}
            Branch: <strong>{{ snapshot_context.branch }}</strong>
          {% elif snapshot_context.release %}
            {% if snapshot_context.release_alias %}
              <i class="{{ swh_object_icons.alias }} mdi-fw" aria-hidden="true"></i>
            {% else %}
              <i class="{{ swh_object_icons.release }} mdi-fw" aria-hidden="true"></i>
            {% endif %}
            Release: <strong>{{ snapshot_context.release }}</strong>
          {% elif snapshot_context.revision_id %}
            Revision: <strong>{{ snapshot_context.revision_id }}</strong>
          {% endif %}
          <span class="caret"></span>
        </button>
        <ul class="scrollable-menu dropdown-menu swh-branches-releases">
          <ul class="nav nav-tabs">
            <li class="nav-item">
              <a class="nav-link active swh-branches-switch" data-bs-toggle="tab">Branches</a>
            </li>
            <li class="nav-item">
              <a class="nav-link swh-releases-switch" data-bs-toggle="tab">Releases</a>
            </li>
          </ul>
          <div class="tab-content">
            <div class="tab-pane active" id="swh-tab-branches">
              <ul>
                {% for b in snapshot_context.branches %}
                  <li class="swh-branch">
                    <a href="{{ b.url | safe }}">
                      {% if b.alias %}
                        <i class="{{ swh_object_icons.alias }} mdi-fw" aria-hidden="true"></i>
                      {% else %}
                        <i class="{{ swh_object_icons.branch }} mdi-fw" aria-hidden="true"></i>
                      {% endif %}
                      {% if b.name == snapshot_context.branch %}
                        <i class="mdi mdi-check-bold mdi-fw" aria-hidden="true"></i>
                      {% else %}
                        <i class="mdi mdi-fw" aria-hidden="true"></i>
                      {% endif %}
                      {{ b.name }}
                    </a>
                  </li>
                {% endfor %}
                {% if snapshot_context.branches|length < snapshot_context.snapshot_sizes.branch %}
                  <li>
                    <i class="mdi mdi-alert mdi-fw" aria-hidden="true"></i>
                    Branches list truncated to {{ snapshot_context.branches|length }} entries,
                    {{ snapshot_context.branches|length|mul:-1|add:snapshot_context.snapshot_sizes.branch }}
                    were omitted.
                  </li>
                {% endif %}
              </ul>
            </div>
            <div class="tab-pane" id="swh-tab-releases">
              {% if snapshot_context.releases %}
                <ul>
                  {% for r in snapshot_context.releases %}
                    {% ifchanged r.name r.alias %}
                      {% if r.target_type == 'revision' or r.target_type == 'directory' %}
                        <li class="swh-release"
                            {% if r.alias %}id="swh-release-{{ r.name }}-alias"{% else %}id="swh-release-{{ r.name }}"{% endif %}>
                          <a href="{{ r.url | safe }}">
                            {% if r.alias %}
                              <i class="{{ swh_object_icons.alias }} mdi-fw" aria-hidden="true"></i>
                            {% else %}
                              <i class="{{ swh_object_icons.release }} mdi-fw" aria-hidden="true"></i>
                            {% endif %}
                            {% if r.name == snapshot_context.release %}
                              <i class="mdi mdi-check-bold mdi-fw" aria-hidden="true"></i>
                            {% else %}
                              <i class="mdi mdi-fw" aria-hidden="true"></i>
                            {% endif %}
                            {{ r.name }}
                          </a>
                        </li>
                      {% endif %}
                    {% endifchanged %}
                  {% endfor %}
                  {% if snapshot_context.releases|length < snapshot_context.snapshot_sizes.release %}
                    <li>
                      <i class="mdi mdi-alert mdi-fw" aria-hidden="true"></i>
                      Releases list truncated to {{ snapshot_context.releases|length }} entries,
                      {{ snapshot_context.releases|length|mul:-1|add:snapshot_context.snapshot_sizes.release }}
                      were omitted.
                    </li>
                  {% endif %}
                </ul>
              {% else %}
                <span>No releases to show</span>
              {% endif %}
            </div>
          </div>
        </ul>
      </div>
    {% endif %}
  {% endif %}
  <div id="swh-breadcrumbs-container" class="flex-grow-1">{% include "./breadcrumbs.html" %}</div>
  <div class="btn-group swh-actions-dropdown ms-auto">
    {% if top_right_link %}
      <a href="{{ top_right_link.url | safe }}"
         class="btn btn-secondary btn-sm swh-tr-link"
         role="button">
        {% if top_right_link.icon %}
          <i class="{{ top_right_link.icon }} mdi-fw" aria-hidden="true"></i>
        {% endif %}
        {{ top_right_link.text }}
      </a>
    {% endif %}
    {% if show_actions %}
      {% if "swh.web.vault" in SWH_DJANGO_APPS and vault_cooking or vault_cooking and vault_cooking.content_context %}
        {% if not snapshot_context or not snapshot_context.is_empty %}
          {% include "includes/vault-create-tasks.html" %}
        {% endif %}
      {% endif %}
      {% if available_languages %}
        <select data-placeholder="Select Language"
                class="language-select chosen-select">
          <option value=""></option>
          {% for lang in available_languages %}<option value="{{ lang }}">{{ lang }}</option>{% endfor %}
        </select>
      {% endif %}
      {% if "swh.web.save_code_now" in SWH_DJANGO_APPS or SWH_MIRROR_CONFIG %}
        {% include "includes/take-new-snapshot.html" %}
      {% endif %}
      {% include "./show-metadata.html" %}
    {% endif %}
  </div>
</div>
{% include "./sidetabs.html" %}
<script>
  var snapshotContext = false;
  var branch = false;
  {% if snapshot_context %}
    snapshotContext = true;
    branch = "{{ snapshot_context.branch|escape }}";
  {% endif %}
  {% if available_languages %}
    $(".chosen-select").val("{{ language }}");
    setTimeout(function() {
      $(".chosen-search-input").attr("aria-label", "Search language to highlight");
    });
    $(".chosen-select").chosen().change(function(event, params) {
      updateLanguage(params.selected);
    });
  {% endif %}
  swh.browse.initSnapshotNavigation(snapshotContext, branch !== "None");
</script>
