{% macro render_menu_item(item, translation_domain, active) %}
    {% if item.type == 'divider' %}
        {{ item.label|trans(domain = translation_domain) }}
    {% else %}
        {% set menu_params = { menuIndex: item.menu_index, submenuIndex: item.submenu_index } %}
        {% set path =
            item.type == 'link' ? item.url :
            item.type == 'route' ? path(item.route, item.params) :
            item.type == 'entity' ? path('easyadmin', { entity: item.entity, action: 'list' }|merge(menu_params)|merge(item.params)) :
            item.type == 'empty' ? '#' : '' %}

        {# if the URL generated for the route belongs to the backend, regenerate
           the URL to include the menu_params to display the selected menu item
           (this is checked comparing the beginning of the route URL with the backend homepage URL)
        #}
        {% if item.type == 'route' and (path starts with path('easyadmin')) %}
            {% set path = path(item.route, menu_params|merge(item.params)) %}
        {% endif %}

        <a href="{{ path }}"
           class="nav-link {{ item.css_class|default('') }} {{ active ? 'active' }}"
           {% if item.target|default(false) %}target="{{ item.target }}"{% endif %}
                {% if item.rel|default(false) %}rel="{{ item.rel }}"{% endif %}>
            {% if item.icon is not empty %}<i class="fa fa-fw {{ item.icon }}"></i>{% endif %}
            <p>{{ item.label|trans(domain = translation_domain) }}
                {% if item.children|default([]) is not empty %}<i class="right fas fa-angle-left"></i>{% endif %}
                {% if item.params.count_menu_item is defined and count_menu_items(item.params.count_menu_item) > 0 %}<span class="badge badge-{{ item.params.alert is defined ? 'danger' : 'info' }} right">{{ count_menu_items(item.params.count_menu_item) }}</span>{% endif %}
            </p>
        </a>
    {% endif %}
{% endmacro %}

{% import _self as helper %}

{% block main_menu_before %}{% endblock %}

{% set _translation_domain = (_entity_config.translation_domain)|default(easyadmin_config('translation_domain'))|default('messages') %}
<ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">

    {% set _menu_items = easyadmin_config('design.menu') %}

    {% block main_menu %}
        {% for item in _menu_items %}
            {% block menu_item %}
                {% set is_selected_menu = app.request.query.get('menuIndex')|default(-1) == item.menu_index %}
                {% set is_selected_submenu = is_selected_menu and app.request.query.get('submenuIndex')|default(-1) != -1 %}
                {% if easyadmin_is_granted(item.permission) %}

                    <li class="{{ item.type == 'divider' ? 'nav-header' : 'nav-item' }}  {{ item.children is not empty ? 'treeview' }} {{ is_selected_menu ? 'menu-open' }}">
                        {{ helper.render_menu_item(item, _translation_domain, is_selected_menu) }}

                        {% if item.children|default([]) is not empty %}
                            <ul class="nav nav-treeview">
                                {% for subitem in item.children %}
                                    {% block menu_subitem %}
                                        {% if easyadmin_is_granted(subitem.permission) %}
                                            <li class="nav-item ">
                                                {{ helper.render_menu_item(subitem, _translation_domain, is_selected_menu and app.request.query.get('submenuIndex')|default(-1) == subitem.submenu_index ? true) }}
                                            </li>
                                        {% endif %}
                                    {% endblock menu_subitem %}
                                {% endfor %}
                            </ul>
                        {% endif %}
                    </li>
                {% endif %}
            {% endblock menu_item %}
        {% endfor %}
    {% endblock main_menu %}
</ul>

{% block main_menu_after %}{% endblock %}
