{% macro box(type, title, slim=False, caller=None) %}
<aside class="box{% if slim %} slim{% endif %} {{ type }}"><header>{{ title }}</header> {{ caller() }}</aside>
{% endmacro %}

{% macro avatar(from_, hash, char=None, caller=None) -%}
{%- if hash -%}
<div class="avatar" style="background-image: url('{{ url_for_avatar(from_, hash) }}');"></div>
{%- else -%}
<div class="avatar shim" style="background-color: {{ text_to_css(from_) }}"><span data-avatar-char="{{ char or from_[0] }}"></span></div>
{%- endif -%}
{%- endmacro %}

{% macro showuri(uri, caller=None, id_=None) %}
{%- if uri is none -%}
<em>—</em>
{%- else -%}
<div><input type="text" {% if id_ %}id="{{ id_ }}" {% endif %}readonly="readonly" value="{{ uri }}"></div>
<div>{% call clipboard_button(uri, show_label=True) %}{% trans %}Copy link{% endtrans %}{% endcall %}</div>
{%- endif -%}
{% endmacro %}

{% macro icon(name, caller=None) -%}
{%- set alt = "" if caller is none else caller() -%}
{%- if alt %}<span class="a11y-only">{{ alt }}</span>{% endif %}<svg class="icon icon-{{ name }}" aria-hidden="true"><use xlink:href="{{ url_for('static', filename='img/icons.svg' )}}#icon-{{ name }}"></use></svg>
{%- endmacro %}

{% macro standard_button(icon_name, href, caller=None, class=None, onclick=None) -%}
{%- set label = caller() -%}
<a href="{{ href }}" class="button {% if class %}{{ class }}{% endif %}" {% if onclick %} onclick="{{ onclick }}"{% endif %}>{% call icon(icon_name) %}{% endcall %}<span>{{ label }}</span></a>
{%- endmacro %}

{% macro form_button(icon_name, button_obj, caller=None, class=None) -%}
<button {% if class %}class="{{ class }}"{% endif %} id="{{ button_obj.id }}" name="{{ button_obj.name }}" type="submit" value="{{ button_obj.data or 'true' }}">{% call icon(icon_name) %}{% endcall %}<span>{{ button_obj.label.text }}</span></button>
{%- endmacro %}

{% macro custom_form_button(icon_name, name, value, caller=None, slim=False, class=None) -%}
{%- set text = caller() -%}
<button
	{% if class %}class="{{ class }}"{% endif %}
	{% if name %}name="{{ name }}"{% endif %}
	type="submit"
	{% if value %} value="{{ value }}"{% endif %}
{% if slim %}
	aria-label="{{ text }}"
	title="{{ text }}"
{% endif %}
>
	{%- call icon(icon_name) %}{% endcall -%}
	{%- if not slim -%}
	<span>{{ text }}</span>
	{%- endif -%}
</button>
{%- endmacro %}

{% macro action_button(icon_name, href, caller=None, class=None, onclick=None) -%}
{%- set a11y = caller() -%}
<a href="{{ href }}" class="button {% if class %}{{ class }}{% endif %}" aria-label="{{ a11y }}" title="{{ a11y }}"{% if onclick %} onclick="{{ onclick }}"{% endif %}>{% call icon(icon_name) %}{% endcall %}</a>
{%- endmacro %}

{% macro clipboard_button(data, show_label=False, caller=None, class=None) -%}
{%- set label = caller() -%}
<a class="button{% if class %} {{ class }}{% endif %}"
	href="#"
{% if not show_label %}
	aria-label="{{ label }}"
	title="{{ label }}"
{% endif %}
	data-cliptext="{{ data }}"
	onclick="copy_to_clipboard_btn(this); return false;">
	{%- call icon("copy") %}{% endcall -%}
{%- if show_label %}
	<span>{{ label }}</span>
{% endif -%}
</a>
{%- endmacro %}

{% macro render_errors(field, caller=None) -%}
{%- set error_list = field.errors if field.errors is not mapping else (field.errors.values() | flatten | list) -%}
{%- if error_list -%}
<div class="box warning">{#- -#}
<header>{% trans %}Invalid input{% endtrans %}</header>
{%- if error_list | length == 1 -%}
<p>{{ error_list[0] }}</p>
{%- else -%}
<ul>
{%- for error in error_list -%}
	<li>{{ error }}</li>
{%- endfor -%}
</ul>
{%- endif -%}
</div>
{%- endif -%}
{%- endmacro %}

{% macro value_or_hint(v, caller=None) %}
{%- if v is not none -%}
{{- v -}}
{%- else -%}
—
{%- endif -%}
{% endmacro %}

{% macro extract_circle_name(circle_map, id, caller=None) %}
{%- set circle_info = circle_map[id] -%}
{%- if circle_info -%}
<a href="{{ url_for('.edit_circle', id_=id) }}">{{ circle_info | circle_name }}</a>
{%- else -%}
<em>{% trans %}deleted{% endtrans %}</em>
{%- endif -%}
{% endmacro %}

{%- macro invite_type_name(invite_info, caller=None) -%}
{%- if invite_info.reusable -%}
{% trans %}Group{% endtrans %}
{%- else -%}
{% trans %}Individual{% endtrans %}
{%- endif -%}
{%- endmacro -%}

{%- macro invite_type_description(invite_info, caller=None) -%}
{%- if invite_info.reusable -%}
{% trans %}Can be used multiple times to create accounts on this Snikket service.{% endtrans %}
{%- else -%}
{% trans %}Can be used once to create an account on this Snikket service.{% endtrans %}
{%- endif -%}
{%- endmacro -%}
