{% macro ico(name) %}
    {% spaceless %}
        <i class="fa fa-{{ name }}"></i>
    {% endspaceless %}
{% endmacro %}

{# Locale buttons #}
{% macro add(options = {}) %}
    {% set options = {color: '', shape: '', class:'mainBtn', ico: null, ico_position: null, size: '', type: 'link', attr: {'data-btn-type': 'add'}}|merge(options) %}
    {% set text = options.text|default('designer_common.action.add.default') %}
    {{ _self.custom(text, options) }}
{% endmacro %}

{% macro save(options = {}) %}
    {% set options = {color: '', shape: '', class:'mainBtn', ico: 'check', ico_position:"left", size: '', type: 'submit', attr: {'data-btn-type': 'save'}}|merge(options) %}
    {% set text = options.text|default('designer_common.action.save.default') %}
    {{ _self.custom(text, options) }}
{% endmacro %}

{% macro back(options = {}) %}
    {% set options = {color: '', shape: '', class:'defaultBtn', ico: 'arrow-circle-o-left', ico_position:"left", size: '', type: 'link', attr: {class:'button-previous', 'data-btn-type': 'back'} }|merge(options) %}
    {% set text = options.text|default('designer_common.action.back') %}
    {{ _self.custom(text, options) }}
{% endmacro %}

{% macro continue(options = {}) %}
    {% set options = {color: '', shape: '', class:'defaultBtn', ico: '', ico_position:"", size: '', type: 'link', attr: {class:'button-next', 'data-btn-type': 'continue'} }|merge(options) %}
    {% set text = options.text|default('designer_common.action.continue') %}
    {{ _self.custom(text, options) }}
{% endmacro %}

{%- macro cancel(options = {}) %}
    {% set options = { color: '', shape: '', class:'defaultBtn', ico: 'lg', ico_position:"left", size: '', type: 'link', attr: {'data-btn-type': 'cancel'}}|merge(options) %}
    {% set text = options.text|default('designer_common.action.cancel.default') %}
    {{ _self.custom(text, options) }}
{%- endmacro %}

{%- macro delete(options = {}) %}
    {% set options = {color: '', shape: '', class:'deleteBtn', ico: 'lg', ico_position:"left", size: '', type: 'link', attr: {'data-btn-type': 'delete'}}|merge(options) %}
    {% set text = options.text|default('designer_common.action.delete.default') %}
    {{ _self.custom(text, options) }}
{%- endmacro %}

{# Style buttons #}

{% macro check(options = {}) %}
    {% set options = {color: '', class:'mainBtn', ico: 'check'}|merge(options) %}
    {{ _self.custom(text, options) }}
{% endmacro %}

{%- macro default(text = '', options = {}) %}
    {% set options = {color: '', class:'defaultBtn'}|merge(options) %}
    {{ _self.custom(text, options) }}
{%- endmacro %}

{% macro custom( text = '', options = {}) -%}
    {% spaceless %}
        {% set options = {
            color: 'default',
            class: 'defaultBtn',
            shape: '',
            size: 'lg',
            ico: '',
            type: 'button',
            attr: options.attr|default({}),
            ico_position: 'left',
            translation_attributes: {},
            translation_domain: 'messages'
            }|merge(options) %}
            {% set tag = ('link' == options.type) ? 'a' : 'button' %}
            {% set attr = options.attr|merge({
                type: ('link' == options.type) ? '': options.type,
                class: "btn " ~ (options.class is defined ? ' ' ~ options.class : '')
            }) %}
        <{{tag}} {{ block('button_attributes') }}>
            {{ text|trans(options.translation_attributes, options.translation_domain) }}
            {% if options.ico is not empty and options.ico_position == "right" %}
                {{ _self.ico(options.ico) }}
            {% endif %}
        </{{ tag }}>
    {% endspaceless %}
{%- endmacro %}

{% block button_attributes -%}
    {%- for attrname, attrvalue in attr -%}
        {{- " " -}}
        {%- if attrname in ['placeholder', 'title'] -%}
            {{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
        {%- elseif attrvalue is same as(true) -%}
            {{- attrname }}="{{ attrname }}"
        {%- elseif attrvalue is not same as(false) -%}
            {{- attrname }}="{{ attrvalue }}"
        {%- endif -%}
    {%- endfor -%}
{%- endblock button_attributes %}
