{% import 'utility.macros.njk' as utility %}


{% macro item_section(slug, title=none, supplement=none) %}
  <div data-item-section="{{ slug }}">
    {% if title or supplement %}
      <h3 class="item-subtitle">
        <span class="item-subtitle-main">{{ title }}</span>
        {% if supplement %}
          <span class="item-subtitle-supplement">{{ supplement|safe }}</span>
        {% endif %}
      </h3>
    {% endif %}

    {{ caller() }}
  </div>
{% endmacro %}


{% macro heading(item, type, id, display=none) %}
  {% set item_name = format_name(item, type) %}
  {% set display_type = format_type(type) %}

  {% set private = 'private' if (item.access == 'private') else none %}
  {% set deprecate = 'deprecated' if item.deprecated else none %}
  {% set note = private or deprecate or none %}
  {% set note = [private, deprecate]|join(' -- ') if (private and deprecate) else note %}

  <div data-item-section="header">
    <h2 class="item-title">
      {% if display_type %}
        <span class="item-type">{{ display_type }}</span>
      {% endif %}

      {% if item_name and id %}
        <a href="#{{ id }}" class="item-name">{{ item_name }}</a>
      {% endif %}

      {% if item.type %}
        <span class="value-type">({{ item.type }})</span>
      {% endif %}

      {% if note %}
        <span class="item-note">[{{ note }}]</span>
      {% endif %}
    </h2>

    {{ alias(
      is_alias=item.alias,
      has_alias=item.aliased,
      type=type,
      display=display
      ) }}

    {{ source(item, type) }}
    {{ summary(item) }}
  </div>
{% endmacro %}


{% macro prose(item) %}
  {% set content = item.description %}
  {% set origin = item.since %}
  {% set by = item.author %}
  {% set deprecation = item.deprecated %}

  {% if content or origin or by or deprecation %}
    <div data-item-section="prose" class="text-block">
      {{ content|safe }}
      {{ since(origin) }}
      {{ authors(by) }}
      {{ deprecated(deprecation) }}
    </div>
  {% endif %}
{% endmacro %}


{% macro alias(is_alias, has_alias, type, display=none) %}
  {% if is_alias or has_alias %}
    {% set alias_intro = 'alias for' if is_alias else 'aliased as' %}
    {% set items = is_alias or has_alias %}
    {% set string = items|string %}

    <p class="alias">
      {{ alias_intro }}

      {% if (items.length == string.length) %}
        {{ alias_link(
          item=items,
          type=type,
          link_alias=display.alias
          ) }}
      {% else %}
        {% for item in items -%}
          {{ alias_link(
            item=item,
            type=type,
            link_alias=display.alias
            ) }}{%- if not loop.last %},{% endif %}
        {% endfor %}
      {% endif %}
    </p>
  {% endif %}
{% endmacro %}


{% macro alias_link(item, type, link_alias) -%}
  {%- set alias_text = format_name(item, type) -%}
  {%- set alias_url = get_link(item, type) if link_alias else none -%}
  {%- set alias_url = alias_url if (alias_url != '') else none -%}
  {{ utility.link_if(
    content=alias_text,
    url=alias_url,
    attrs={'class': 'alias-title'}
    ) }}
{%- endmacro %}


{# This works great if we know the group,
   or the referenced item is on the same page #}
{% macro get_link(name, type, group=none) -%}
  {% set group = [group, 'html']|join('.') if group else none %}
  {{ group }}#{{ type|urlencode }}--{{ name|urlencode }}
{%- endmacro %}


{% macro format_name(item, type) -%}
  {%- set item_name = item.context.name if item.context else item -%}
  {%- set item_name = [item_name, '()']|join if (type == 'mixin' or type == 'function') else item_name -%}
  {%- set item_name = ['$', item_name]|join if (type == 'variable') else item_name -%}
  {{ item_name }}
{%- endmacro %}


{% macro format_type(type) -%}
  {%- set display_type = ['@', type]|join if ((type == 'mixin') or (type == 'function')) else type -%}
  {%- set display_type = none if ((type == 'variable') or (type == 'css')) else display_type -%}
  {{ display_type }}
{%- endmacro %}


{% macro source(item, type) %}
  {%- if not ((type == 'mixin') or (type == 'function')) -%}
    {% set code = scss_variable(item) if (type == 'variable') else none %}
    {% set code = scss_css(item) if (type == 'css') else code %}
    {{ utility.code_block(language='scss', content=code|escape|safe) }}
  {%- endif -%}
{% endmacro %}


{% macro summary(item) %}
  {% set content = item.description %}
  {% set origin = item.since %}
  {% set by = item.author %}
  {% set deprecation = item.deprecated %}

  {% if content or origin or by or deprecation %}
    <div class="text-block">
      {{ content|safe }}
      {{ since(origin) }}
      {{ authors(by) }}
      {{ deprecated(deprecation) }}
    </div>
  {% endif %}
{% endmacro %}


{% macro authors(data) %}
  {% if (data.length) %}
    {% set title = 'Authors' if (data.length > 1) else 'Author' %}
    <section class="summary-subsection">
      <h3 class="summary-subtitle">{{ title }}:</h3>
      <ul class="subsection-details">
        {% for author in data %}
          <li>{{ author|safe }}</li>
        {% endfor %}
      </ul>
    </section>
  {% endif %}
{% endmacro %}


{% macro deprecated(data) %}
  {% if data and (data != '') %}
    <section class="summary-subsection">
      <h3 class="summary-subtitle">DEPRECATED:</h3>
      <div class="subsection-details">{{ data|safe }}</div>
    </section>
  {% endif %}
{% endmacro %}


{% macro since(data) %}
  {% if (data.length) %}
    {% for item in data %}
      <section class="summary-subsection">
        <h3 class="summary-subtitle">Since <code>{{ item.version }}</code>:</h3>
        <div class="subsection-details">{{ item.description|safe }}</div>
      </section>
    {% endfor %}
  {% endif %}
{% endmacro %}


{% macro scss_variable(item) -%}
${{ item.context.origName or item.context.name }}: {{ item.context.value }}{% if item.context.scope != 'private' %} !{{ item.context.scope }}{% endif %};
{%- endmacro %}


{%- macro scss_css(item) -%}
{{ item.context.origName or item.context.name }} {
  {{ item.context.value }}
}
{%- endmacro -%}
