{% macro action_cardtoolbutton(icon, attrs = {}) -%}
    {% import '@Tabler/components/button.html.twig' as macro %}
    {{ macro.button(icon, attrs) }}
{%- endmacro %}

{% macro action_collapsebutton(label, target) -%}
    {{ _self.action_cardtoolbutton('collapse', {collapse: target, title: label|default('')}) }}
{%- endmacro %}

{% macro link_button(label, href = '#', icon = false, type = 'primary', size = null) -%}
    {% import '@Tabler/components/button.html.twig' as macro %}

    {% set _href = href ?? '#' %}
    {% set _icon = icon ?? false %}
    {% set _type = type ?? 'primary' %}
    {% set _size = size ?? null %}

    {{ macro.button(_icon, {
        title: label,
        combined: true,
        href: _href,
        class: _size ? 'btn-' ~ _size : ''
    }, _type) }}
{%- endmacro %}

{% macro action_button(label, action, icon = false, type = 'primary', size = null) -%}
    {% import '@Tabler/components/button.html.twig' as macro %}

    {% set _icon = icon ?? false %}
    {% set _type = type ?? 'primary' %}
    {% set _size = size ?? null %}

    {{ macro.button(_icon, {
        title: label,
        class: _size ? 'btn-' ~ _size : '',
        buttonType: 'button',
        attr: {'data-action': action}
    }, _type) }}
{%- endmacro %}

{% macro submit_button(icon, user_values = {}, type = null) -%}
    {% import '@Tabler/components/button.html.twig' as macro %}

    {% set default_values = {
        combined : true,
        attr : {type: 'submit'},
        disabled : false,
        buttonType : 'submit',
    } %}

    {% set values_attr = default_values.attr | merge(user_values.attr|default({})) %}
    {% set values = default_values | merge(user_values|default({})) %}
    {% set values = values | merge({'attr' : values_attr}) %}

    {{ macro.button(icon, values, type) }}
{%- endmacro %}

{% macro dropdown_button(button, actions = [], options = {}) -%}
    {% import '@Tabler/components/button.html.twig' as macro %}
    {% import '@Tabler/includes/utils.html.twig' as utils %}

    {% set button_default = {
        buttonType : 'button',
        class: '',
        combined : true,
        attr : {
            'data-bs-toggle' : 'dropdown'
        },
    } %}

    {% set options = (options|default({})) | merge({icon_class: 'me-2 text-center'}) %}
    {% set values_attr = button_default.attr | merge(button.attr|default({})) %}
    {% set values = button_default | merge(button|default({})) %}
    {% set values = values | merge({'attr' : values_attr}) %}

    <div class="dropdown">
        {{ macro.button(
            button.icon ?? false,
            (values|merge({class: values.class ~ ' dropdown-toggle'})),
            button.type ?? null
        ) }}

        {% if actions is iterable and actions|length > 0 %}
            <div class="dropdown-menu">
                {% for action in actions %}
                    {% set _title   = action.title ?? null %}
                    {% if action.url is defined %}
                        {% set _href = action.url %}
                    {% else %}
                        {% set _href = action.href ?? '#' %}
                    {% endif %}
                    {% set _icon    = action.icon ?? false %}
                    {% set _attr    = action.attr ?? {} %}
                    {% set _class   = 'dropdown-item ' ~ (action.class ?? '') %}

                    <a class="{{ _class }}" href="{{ _href }}" {{ utils.attr_to_html(_attr) }}>
                        {%- if _icon is not same as (false) -%}
                            <span class="{{ options.icon_class }}">{{ tabler_icon(_icon, false) }}</span>
                        {% endif %}
                        {%- if _title is not null -%}
                            {{ _title|raw }}
                        {% endif %}
                    </a>
                {% endfor %}
            </div>
        {% endif %}
    </div>
{%- endmacro %}
