{% set spinner = spinner ?? false -%}
{% set busyMessage = busyMessage ?? false %}
{% set failureMessage = failureMessage ?? false %}
{% set retryMessage = retryMessage ?? false %}
{% set successMessage = successMessage ?? false %}
{% set label = label ?? null %}
{% set labelHtml = labelHtml ?? null %}

{% set hasLabel = label or label is same as('0') %}
{% set hasLabelHtml = labelHtml or labelHtml is same as('0') %}

{% set attributes = {
    type: type ?? 'button',
    id: id ?? false,
    class: (class ?? [])|explodeClass|merge([
        'btn',
        not (hasLabel or hasLabelHtml) ? 'btn-empty' : null,
    ]|filter),
    data: {
        'busy-message': busyMessage,
        'failure-message': failureMessage,
        'retry-message': retryMessage,
        'success-message': successMessage,
    }
}|merge(attributes ?? {}) -%}

{% apply spaceless %}
    {% if spinner %}
        <div role="status" class="visually-hidden"></div>
    {% endif %}
    {% tag 'button' with attributes %}
        {{ (hasLabel or hasLabelHtml) ? tag('div', {
            class: ['label', 'inline-flex'],
            text: hasLabel ? label : null,
            html: hasLabelHtml ? labelHtml : null,
        }) }}
        {% if spinner %}
            <div class="spinner spinner-absolute">
                <span class="visually-hidden">{{ 'Loading'|t('app') }}</span>
            </div>
        {% endif %}
    {% endtag %}
{% endapply -%}
