{% macro render_icon(item_or_texture, name="") -%}
  {% if item_or_texture.texture is defined %}
    {{- render_item(item_or_texture) -}}
  {% else %}
    {{- render_texture(name, item_or_texture) -}}
  {% endif %}
{%- endmacro %}

{% macro render_texture(name, texture, class_names=[], lazy=true, title=true) -%}
  {% if texture.meta is defined -%}
    <div
      role="img"
      {{ ('title="'~name~'"')|safe if title }}
      aria-label="{{ name }} (animated)"
      class="{{
        (class_names + [
          'texture',
          'animated-sync',
          texture.css_class,
          'pixelated' if texture.pixelated,
        ])|join(' ')
      }}"
    ></div>
  {%- else -%}
    <img
      {{ ('title="'~name~'"')|safe if title }}
      alt="{{ name }}"
      src="{{ texture.url }}"
      {{ 'loading="lazy"'|safe if lazy }}
      class="{{
        (class_names + [
          'texture',
          'pixelated' if texture.pixelated,
        ])|join(' ')
      }}"
    >
  {%- endif %}
{%- endmacro %}

{# display a single item, with a badge if the count is greater than 1 #}
{% macro render_item(item, is_first=none, count=1) -%}
  {% if item.image_textures is sequence -%}
    <div
      class="texture item-texture multi-textures {{
        'gaslight-textures' if item.gaslighting
      }} {{
        'multi-texture-active' if is_first is true
      }}"
    >
      {% for texture in item.image_textures %}
        {{
          render_texture(
            name=item.name,
            texture=texture,
            class_names=[
              "item-texture",
              "multi-texture-active" if loop.first,
            ],
          )
        }}
      {% endfor %}
    </div>
  {%- else -%}
    {{ render_texture(
      name=item.name,
      texture=item.image_texture,
      class_names=[
        "item-texture",
        "multi-texture-active" if is_first is true,
      ],
    ) }}
  {%- endif -%}
  {% if count > 1 -%}
    <div class="badge item-texture-count">{{ count }}</div>
  {%- endif %}
{%- endmacro %}

{% macro render_recipe_gui(recipe, class_names=[]) -%}
  {{ render_texture(
    name=recipe.gui_name,
    texture=recipe.gui_texture,
    class_names=class_names,
    lazy=false,
  ) }}
{%- endmacro %}

{% macro icon_url(item_or_texture) -%}
  {%- if item_or_texture.texture is defined -%}
    {{ item_url(item_or_texture) }}
  {%- else -%}
    {{ texture_url(item_or_texture) }}
  {%- endif -%}
{%- endmacro %}

{% macro item_url(item) -%}
  {%- if item.image_textures is sequence -%}
    {{ texture_url(item.image_textures|first) }}
  {%- else -%}
    {{ texture_url(item.image_texture) }}
  {%- endif -%}
{%- endmacro %}

{% macro texture_url(texture) -%}
  {#- TODO: replace when we implement gif rendering -#}
  {% if texture.meta is not defined -%}
    {{ texture.url }}
  {%- endif %}
{%- endmacro %}
