{% macro boolean(value) %}
    {% if value %}
        {% trans %}bool.true{% endtrans %}
    {% else %}
        {% trans %}bool.false{% endtrans %}
    {% endif %}
{% endmacro %}

{% macro attachment_icon(attachment, attachment_helper, class = "fa-fw fas fa-3x", link = true) %}
    {% set disabled = attachment.secure and not is_granted("show_secure", attachment) %}
    {% if not attachment_helper or attachment_helper.fileExisting(attachment) %}
        {% if link and not disabled %}
            <a target="_blank" data-turbo="false" rel="noopener" href="{{ attachment|entityURL('file_view') }}">
        {% endif %}
        {% if attachment.picture %}
            <img class="hoverpic" {{ stimulus_controller('elements/hoverpic') }} data-thumbnail="{{ attachment|entityURL('file_view') }}" src="{{ attachment|entityURL('file_view') }}">
        {% else %}
            <i class="text-dark {{ class }} {{ ext_to_fa_icon(attachment.extension) }}"></i>
        {% endif %}
        {% if link and not disabled %}
            </a>
        {% endif %}
    {% elseif not attachment_helper.fileExisting(attachment) %}
        <i class="{{ class }} fa-exclamation-triangle text-danger" title="{% trans %}attachment.file_not_found{% endtrans %}"></i>
    {% endif %}
{% endmacro %}

{% macro string_to_tags(string, class="badge bg-info") %}
    {% for tag in string|split(',') %}
        <a href="{{ path('part_list_tags', {'tag': tag | trim | url_encode}) }}" class="{{ class }}" >{{ tag | trim }}</a>
    {% endfor %}
{% endmacro %}

{% macro m_status_to_badge(status, class="badge") %}
    {% if status is not empty %}
        {% set color = " bg-secondary" %}

        {% if status == "active" %}
            {% set color = " bg-success" %}
        {% elseif status == "nrfnd" %}
            {% set color = " bg-warning" %}
        {% elseif status == "eol" %}
            {% set color = " bg-warning" %}
        {% elseif status == "discontinued" %}
            {% set color = " bg-danger" %}
        {% endif %}

        <span class="{{ class ~ color}}" title="{{ ("m_status." ~ status ~ ".help") | trans }}">
            <i class="fa-fw fas fa-info-circle"></i>
            {{ ("m_status." ~ status) | trans }}
        </span>
    {% endif %}
{% endmacro %}

{% macro structural_entity_link(entity, link_type = "list_parts") %}
    {# @var entity \App\Entity\Base\StructuralDBElement #}
    {% if entity %}
        <ul class="structural_link d-inline">
            {% for e in entity.pathArray %}
                <li>
                    {% if link_type is not empty and e.id is not null %}
                        <a href="{{ e | entityURL(link_type) }}">{{ e.name }}</a>
                    {% else %}
                        {{ e.name }}
                    {% endif %}
                </li>
            {% endfor %}
        </ul>
    {% else %}
        -
    {% endif %}
{% endmacro %}

{% macro entity_icon(entity_or_type, classes = "", style = "") %}
    {% set map = {
        "attachment_type": ["fa-solid fa-file-alt", "attachment_type.label"],
        "category": ["fa-solid fa-tags", "category.label"],
        "currency": ["fa-solid fa-coins", "currency.label"],
        "device": ["fa-solid fa-archive", "device.label"],
        "footprint": ["fa-solid fa-microchip", "footprint.label"],
        "group": ["fa-solid fa-users", "group.label"],
        "label_profile": ["fa-solid fa-qrcode", "label_profile.label"],
        "manufacturer": ["fa-solid fa-industry", "manufacturer.label"],
        "measurement_unit": ["fa-solid fa-balance-scale", "measurement_unit.label"],
        "storelocation": ["fa-solid fa-cube", "storelocation.label"],
        "supplier": ["fa-solid fa-truck", "supplier.label"],
        "user": ["fa-solid fa-user", "user.label"],
    } %}

    {% if entity_or_type is entity %}
        {% set type = entity_type(entity_or_type) %}
    {% else %}
        {% set type = entity_or_type %}
    {% endif %}

    {% if type is not null and map[type] is defined %}
        {% set icon = map[type][0] %}
        {% set label = map[type][1] %}
    {% else %}
        {% set icon = "fa-solid fa-question" %}
        {% set label = "Unknown type " ~ type %}
    {% endif %}

    <i class="fa-fw {{ icon }} {{ classes }}" style="{{ style }}" title="{{ label | trans }}"></i>
{% endmacro %}

{% macro breadcrumb_entity_link(entity, link_type = "list_parts", icon = "") %}
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb py-2 px-3 rounded" style="background-color: var(--bs-gray-200);">
            {% if icon is not empty %}
                <i class="{{ icon }} fa-fw me-1" style="line-height: inherit;"></i>
            {% else %}
                {{ _self.entity_icon(entity, "me-1", "line-height: inherit;") }}
            {% endif %}
            {% for e in entity.pathArray %}
                <li class="breadcrumb-item {% if loop.last %}active{% endif %}">
                    {% if link_type is not empty and not loop.last and e.id is not null %}
                        <a href="{{ e | entityURL(link_type) }}">{{ e.name }}</a>
                    {% else %}
                        {{ e.name }}
                    {% endif %}
                </li>
            {% endfor %}
        </ol>
    </nav>
{% endmacro %}

{% macro bool_icon(bool) %}
    {% if bool %}
        <i class="fas fa-check-circle fa-fw" title="{% trans %}Yes{% endtrans %}"></i>
    {% else %}
        <i class="fas fa-times-circle fa-fw" title="{% trans %}No{% endtrans %}"></i>
    {% endif %}
{% endmacro %}

{% macro date_user_combination(entity, lastModified, datetime_format = "short") %}
    {% if lastModified == true %}
        {{ entity.lastModified | format_datetime(datetime_format) }}
    {% else %}
        {{ entity.addedDate | format_datetime(datetime_format) }}
    {% endif %}
    {% if is_granted('show_users', entity) %}
        {% if lastModified == true %}
            {% set user = getLastEditingUser(entity) %}
        {% else %}
            {% set user = getCreatingUser(entity) %}
        {% endif %}

        {% if user is not null %}
            {% if user.fullName is not empty %}
                <a href="{{ path('user_info', {"id": user.id}) }}" title="@{{ user.name }}"><i>({{ user.fullName }})</i></a>
            {% else %}
                <a href="{{ path('user_info', {"id": user.id}) }}" title="@{{ user.name }}"><i>(@{{ user.name }})</i></a>
            {% endif %}
        {% endif %}
    {% endif %}
{% endmacro %}

{% macro parameters_table(parameters) %}
    <table class="table table-hover table-striped table-sm">
        <thead>
        <tr>
            <th>{% trans %}specifications.property{% endtrans %}</th>
            <th>{% trans %}specifications.value{% endtrans %}</th>
        </tr>
        </thead>
        <tbody>
        {% for param in parameters %}
            <tr>
                <td>{{ param.name }} {% if param.symbol is not empty %}<span class="latex" data-controller="common--latex">${{ param.symbol }}$</span>{% endif %}</td>
                <td>{{ param.formattedValue }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
{% endmacro parameters_table %}