{% macro submenuItem(name, url, anchorlink, tooltip) %}
    {% if name|slice(0,1) != '_' %}
        <li role="menuitem" title="{{ tooltip|default("")|translate|e('html_attr') }}">
            <a class="item" href="{% if anchorlink %}#{% else %}index.php?{% endif %}{{ url|urlRewriteWithParameters|slice(1) }}" tabindex="5">
                {{ name|translate }}
            </a>
        </li>
    {% endif %}
{% endmacro %}

{% macro groupedItem(name, group, anchorlink) %}
    <li role="menuitem" title="{{ name|translate|e('html_attr') }}">
        <div piwik-menudropdown show-search="true" menu-title="{{ name|translate|e('html_attr') }}">
            {% for item in group.getItems %}
                <a class="item menuItem"
                   href='{% if anchorlink %}#?{% else %}index.php?{% endif %}{{ item.url|urlRewriteWithParameters|slice(1) }}'
                   title="{% if item.tooltip %}{{ item.tooltip|e('html_attr') }}{% endif %}">
                    {{ item.name|translate }}
                </a>
            {% endfor %}
        </div>
    </li>
{% endmacro %}

{% macro getId(urlParameters) -%}
    {% if urlParameters is iterable -%}
        {{ urlParameters|urlRewriteWithParameters }}
    {%- endif %}
{%- endmacro %}

{% macro menu(menu, anchorlink, cssClass) %}
    <div id="secondNavBar" class="{{ cssClass }}">
        <div id="search" ng-cloak>
            <div piwik-quick-access class="borderedControl"></div>
        </div>
        <ul role="menu" aria-label="{{ 'CoreHome_MainNavigation'|translate|e('html_attr') }}" class="navbar">
            {% for level1,level2 in menu %}
                {% set hasSubmenuItem = false %}
                {% for name,urlParameters in level2 %}
                    {% if urlParameters._url is defined and urlParameters._url is not iterable %}
                        {% set hasSubmenuItem = true %}
                    {% elseif name|slice(0,1) != '_' %}
                        {% set hasSubmenuItem = true %}
                    {% endif %}
                {% endfor %}

                {% if hasSubmenuItem %}
                    <li role="menuitem" class="menuTab" id="{% if level2._url is defined and level2._url is not empty %}{{ _self.getId(level2._url) }}{% endif %}">
                        <a class="item" href="" tabindex="5">
                            <span class="menu-icon {{ level2._icon|default('icon-arrow-right') }}"></span>{{ level1|translate }}
                        </a>

                        <ul role="menu" title="{{ level1|translate|e('html_attr') }}">
                            {% for name,urlParameters in level2 %}
                                {% if urlParameters._url is defined and urlParameters._url is not iterable %}
                                    {{ _self.groupedItem(name,urlParameters._url, anchorlink) }}
                                {% elseif name|slice(0,1) != '_' %}
                                    {{ _self.submenuItem(name,urlParameters._url, anchorlink, urlParameters._tooltip) }}
                                {% endif %}
                            {% endfor %}
                        </ul>
                    </li>
                {% endif %}
            {% endfor %}
        </ul>
    </div>
{% endmacro %}
