{# Utility macros we'll reuse below -#}
{% macro render_inner_html(icon, text) %}
{# used across multiple button types #}
{% if icon -%}
<span class="btn__icon-container">
  {% if icon.startswith("fa") -%}
    <i class="{{ icon }}"></i>
  {% else %}
    <img alt="{{ translate(text) }} {{ translate("logo") }}" src="{{ pathto(icon, 1) }}">
  {% endif -%}
</span>
{% endif %}
{%- if text %}<span class="btn__text-container">{{ translate(text) }}</span>{% endif -%}
{% endmacro %}


{% macro render_link_button(url, tooltip=None, tooltip_placement="bottom", icon=None, text=None, label=None, classes="") -%}
<a href="{{ url }}" target="_blank"
   class="btn btn-sm{% if label %} btn-{{ label }}{% endif %}{% if classes %} {{ classes }}{% endif %}"
   {% if tooltip %}title="{{ translate(tooltip) }}"{% endif %}
   data-bs-placement="{{ tooltip_placement }}" data-bs-toggle="tooltip"
>
  {{ render_inner_html(icon, text) }}
</a>
{% endmacro %}


{% macro render_js_button(javascript, tooltip=None, tooltip_placement="bottom", icon=None, text=None, label=None, classes="") %}
<button onclick="{{ javascript }}"
  class="btn btn-sm{% if label %} btn-{{ label }}{% endif %}{% if classes %} {{ classes }}{% endif %}"
  {% if tooltip %}title="{{ translate(tooltip) }}"{% endif %}
  data-bs-placement="{{ tooltip_placement }}" data-bs-toggle="tooltip"
>
  {{ render_inner_html(icon, text) }}
</button>
{% endmacro %}


{% macro render_button_group(buttons, icon, tooltip=None, label=None, classes="") %}
{# A bootstrap dropdown #}
{# Bootstrap dropdown ref: https://getbootstrap.com/docs/5.2/components/dropdowns/ #}
<div class="dropdown{% if label %} dropdown-{{ label }}{% endif %}{% if classes %} {{ classes }}{% endif %}">
  <button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-label="{{tooltip}}">
    <i class="{{ icon }}"></i>
  </button>
  <ul class="dropdown-menu">
      {%- for button in buttons %}
      {% set btype = button.get("type") %}
      {% set bopts = button.copy() %}
      {% set _ = bopts.pop("type") %}
      <li>{{ render_funcs[btype](classes="dropdown-item", tooltip_placement="left", **bopts) }}</li>
      {% endfor %}
  </ul>
</div>
{% endmacro %}

{%- set render_funcs = {
  "group" : render_button_group,
  "javascript" : render_js_button,
  "link": render_link_button,
}
-%}
