{% from 'theme/comps.icons.html.j2' import la %}


{% macro _build_btn(design='default',
size=None,
text=None,
url=None,
icon=None,
tag=None,
target=None,
extra_classes=[]) %}

{% set colors = {'default': 'light',
'primary': 'primary',
'secondary': 'secondary',
'success': 'success',
'danger': 'danger',
'warning': 'warning',
'info': 'info',
'light': 'light',
'dark': 'dark'} %}

{% if tag == None %}
{% set tag = 'a' %}
{% endif %}

{% if target != None %}
{# We don't want a href when opening a modal #}
{% set url = '#' %}

{# Add hashtag to target if user forgot to pass it through #}
{% if not target.startswith('#') %}
{% set target = '#' ~ target %}
{% endif %}
{% endif %}

{% if not extra_classes %}
{% set extra_classes = [] %}
{% endif %}

{% if not url %}
{% do extra_classes.append('disabled') %}
{% endif %}

{% if design != '' %}
{% set btn_class = ['btn btn-'] %}
{% do btn_class.append('outline-') if config['CASUAL_UI_OUTLINE'] %}
{% do btn_class.append(colors.get(design.lower(), '')) %}
{% do extra_classes.append(''.join(btn_class)) %}
{% endif %}

{% if size %}
{% do extra_classes.append('btn-' ~ size.lower() ) %}
{% endif %}


<{{ tag }} {{ ({'href': url}) | xmlattr if url else 'disabled' }} class="{{ extra_classes | join(' ') }}" {% if tag=='a'
    %} role="button" {% endif %} {% if target !=None %} data-toggle="modal" data-target="{{ target }}" {% endif %}
    rel="tooltip" {{ kwargs | xmlattr }}>

    {{ la(icon) if icon }}
    {{- text or '' -}}
</{{ tag }}>
{% endmacro %}

{% macro link(text, url, icon, extra_classes) %}
{{ _build_btn(text=text, url=url, icon=icon, tag='a', design='', extra_classes=extra_classes, **kwargs) }}
{% endmacro %}

{% macro link_m(text, icon, target, extra_classes) %}
{{ _build_btn(text=text, icon=icon, target=target, tag='a', extra_classes=extra_classes, **kwargs) }}
{% endmacro %}

{% macro btn(text, url, icon, size, design, tag, extra_classes) %}
{{ _build_btn(text=text, url=url, icon=icon, design=design, size=size,
tag=tag, extra_classes=extra_classes, **kwargs) }}
{% endmacro %}

{% macro btn_a(text, url, icon, size, design, extra_classes, type='default') %}
{% set design = design or type %}

{{ _build_btn(text=text, url=url, icon=icon, design=design, size=size,
tag='a', extra_classes=extra_classes, **kwargs) }}
{% endmacro %}

{% macro btn_b(text, url, icon, size, design, extra_classes) %}
{{ _build_btn(text=text, url=url, icon=icon, design=design, size=size,
tag='button', extra_classes=extra_classes, **kwargs) }}
{% endmacro %}

{% macro btn_m(text, target, icon, size, design, extra_classes, type='default') %}
{% set design = design or type %}

{{ _build_btn(text=text, url=url, icon=icon, design=design, size=size,
tag='button', target=target, extra_classes=extra_classes, **kwargs) }}
{% endmacro %}