{# To trigger whether the TOC and its button show up #}
{% set page_toc = generate_toc_html() %}

{# Start copy from buttons.html -#}
{# Utility macros we'll re-use below -#}
{% macro render_tooltip_metadata(tooltip, tooltip_placement) -%}
data-toggle="tooltip"
data-placement="{{ tooltip_placement }}"
title="{{ translate(tooltip) }}"
{%- endmacro %}


{% macro render_inner_html(icon, text) %}
{# used across multiple button types #}
{% if icon -%}
<span class="headerbtn__icon-container">
  {% if icon.startswith("fa") -%}
    <i class="{{ icon }}"></i>
  {% else %}
    <img src="{{ pathto(icon, 1) }}">
  {% endif -%}
</span>
{% endif %}
{%- if text %}<span class="headerbtn__text-container">{{ translate(text) }}</span>{% endif -%}
{% endmacro %}


{% macro render_link_button(url, tooltip=None, icon=None, text=None, label=None, tooltip_placement="bottom") -%}
<a href="{{ url }}"
   class="headerbtn{% if label %} headerbtn-{{ label }}{% endif %}"
   {% if tooltip %}{{ render_tooltip_metadata(tooltip, tooltip_placement) }}{% endif %}
>
  {{ render_inner_html(icon, text) }}
</a>
{% endmacro %}


{% macro render_js_button(javascript, tooltip=None, icon=None, text=None, label=None, tooltip_placement="bottom") %}
<button onclick="{{ javascript }}"
  class="headerbtn{% if label %} headerbtn-{{ label }}{% endif %}"
  {% if tooltip %}{{ render_tooltip_metadata(tooltip, tooltip_placement) }}{% endif %}
>
  {{ render_inner_html(icon, text) }}
</button>
{% endmacro %}


{% macro render_label_input_button(for_input, tooltip=None, icon=None, text=None, label=None, tooltip_placement="bottom") -%}
<label for="{{ for_input }}"
  class="headerbtn{% if label %} headerbtn-{{ label }}{% endif %}"
  {% if tooltip %}{{ render_tooltip_metadata(tooltip, tooltip_placement) }}{% endif %}
>
  {{ render_inner_html(icon, text) }}
</label>
{% endmacro %}


{# Modified from original buttons.html to add a text option -#}
{% macro render_button_group(buttons, icon=None, text=None, tooltip=None, label=None) %}
<div class="menu-dropdown{% if label %} menu-dropdown-{{ label }}{% endif %}">
  <button class="headerbtn menu-dropdown__trigger"
      aria-label="{{ tooltip }}">
{% if text %}<span class="headerbtn__text-container">{{ translate(text) }}</span>{% endif %}
{% if icon %}<i class="{{ icon }}"></i>{% endif %}
  </button>
{# Modified from original buttons.html to avoid -75% translation, which makes sense only for icon-only buttons -#}
  <div class="menu-dropdown__content" {% if text %}style="transform:translateX(0%)"{% endif %}>
    <ul>
      {%- for button in buttons %}
      <li>
        {{ render_funcs[button.pop("type")](**button) }}
      </li>
      {% endfor %}
    </ul>
  </div>
</div>
{% endmacro %}

{%- set render_funcs = {
  "group" : render_button_group,
  "javascript" : render_js_button,
  "link": render_link_button,
  "input_label": render_label_input_button,
}
-%}
{# End copy from buttons.html -#}

<div class="col py-1 d-flex header-article-main">
    <div class="header-article__left">
        {% if theme_single_page != True %}
        {{ render_label_input_button(for_input="__navigation", tooltip="Toggle navigation", icon="fas fa-bars", tooltip_placement="right") }}
        {% endif %}
    </div>
    <div class="header-article__right">
        {%- for button in header_buttons -%}
        {{ render_funcs[button.pop("type")](**button) }}
        {%- endfor -%}

        {% if page_toc -%}
        {{ render_label_input_button("__page-toc", icon="fas fa-list", label="page-toc") }}
        {%- endif %}
    </div>
</div>

<!-- Table of contents -->
<div class="col-md-3 bd-toc show noprint">
    {%- if page_toc | length >= 1 %}
    <div class="tocsection onthispage pt-5 pb-3">
        <i class="fas fa-list"></i> {{ translate(theme_toc_title) }}
    </div>
    <nav id="bd-toc-nav" aria-label="Page">
        {{ page_toc }}
    </nav>
    {%- endif %}
</div>
