{% set hasAmount = amount is not null and amount != '' %}
{% set compact = compact|default(false) %}
{% if hasAmount %}
    {% set numericAmount = amount + 0 %}
    {% set absAmount = numericAmount < 0 ? 0 - numericAmount : numericAmount %}
    {% set displayCurrency = currency|default('') %}
    {% set amountPrefix = displayCurrency == 'USD' ? '$' : (displayCurrency ? displayCurrency ~ ' ' : '') %}
    {% if compact %}
        {% if absAmount >= 1000000 %}
            {% set scaledAmount = absAmount / 1000000 %}
            {% set amountDecimals = scaledAmount == scaledAmount|round(0, 'floor') ? 0 : 1 %}
            {% set formattedAmount = amountPrefix ~ scaledAmount|number_format(amountDecimals, '.', ',') ~ 'mm' %}
        {% elseif absAmount >= 1000 %}
            {% set scaledAmount = absAmount / 1000 %}
            {% set amountDecimals = scaledAmount == scaledAmount|round(0, 'floor') ? 0 : 1 %}
            {% set formattedAmount = amountPrefix ~ scaledAmount|number_format(amountDecimals, '.', ',') ~ 'k' %}
        {% else %}
            {% set amountDecimals = absAmount == absAmount|round(0, 'floor') ? 0 : 2 %}
            {% set formattedAmount = amountPrefix ~ absAmount|number_format(amountDecimals, '.', ',') %}
        {% endif %}

        {% if numericAmount < 0 %}
            {% set formattedAmount = '-' ~ formattedAmount %}
        {% endif %}

        {{ formattedAmount }}
    {% else %}
        {% set formattedAmount = amount|number_format(2, '.', ',') %}
        {% if displayCurrency == 'USD' %}
            ${{ formattedAmount }}
        {% elseif displayCurrency %}
            {{ displayCurrency }} {{ formattedAmount }}
        {% else %}
            {{ formattedAmount }}
        {% endif %}
    {% endif %}
{% else %}
    No amount
{% endif %}
