{% load i18n unfold %}

{% if sidebar_navigation %}
    <div class="h-0 flex-grow overflow-auto" data-simplebar>
        {% for group in sidebar_navigation %}
            {% if group.items %}
                {% has_nav_item_active group.items as has_active %}

                <div class="hidden mb-2 has-[ol]:has-[li]:block" {% if group.collapsible %}x-data="{navigationOpen: {% if has_active %}true{% else %}false{% endif %}}"{% endif %}>
                    {% if group.separator %}
                        <hr class="border-t border-base-200 mx-6 my-2 dark:border-base-800" />
                    {% endif %}

                    {% if group.title %}
                        <h2 class="font-semibold flex flex-row group items-center mb-1 mx-3 py-1.5 px-3 select-none text-font-important-light text-sm dark:text-font-important-dark {% if group.collapsible %}cursor-pointer hover:text-primary-600 dark:hover:text-primary-500{% endif %}" {% if group.collapsible %}x-on:click="navigationOpen = !navigationOpen"{% endif %}>
                            {{ group.title }}

                            {% if group.collapsible %}
                                <span class="material-symbols-outlined ml-auto text-base-400 transition-all group-hover:text-primary-600 dark:group-hover:text-primary-500" x-bind:class="{'rotate-90': navigationOpen}">
                                    chevron_right
                                </span>
                            {% endif %}
                        </h2>
                    {% endif %}

                    <ol class="flex flex-col gap-1 px-6" {% if group.collapsible %}x-show="navigationOpen"{% endif %}>
                        {% for item in group.items %}
                            {% if item.has_permission %}
                                <li>
                                    <a href="{% if item.link_callback %}{{ item.link_callback }}{% else %}{{ item.link }}{% endif %}" class="flex h-9.5 items-center -mx-3 px-3 rounded hover:text-primary-600 dark:hover:text-primary-500 {% if item.active %}bg-base-100 font-semibold text-primary-600 dark:bg-white/[.06] dark:text-primary-500{% endif %}">
                                        {% if item.icon %}
                                            <span class="material-symbols-outlined md-18 mr-3 w-4.5">
                                                {{ item.icon }}
                                            </span>
                                        {% endif %}

                                        <span class="text-sm">
                                            {{ item.title|safe }}
                                        </span>

                                        {% if item.badge %}
                                            <span class="bg-red-600 font-semibold ml-2 px-1 relative rounded-sm text-xs text-white">
                                                {% if item.badge_callback %}
                                                    {{ item.badge_callback }}
                                                {% else %}
                                                    {{ item.badge }}
                                                {% endif %}
                                            </span>
                                        {% endif %}
                                    </a>
                                </li>
                            {% endif %}
                        {% endfor %}
                    </ol>
                </div>
            {% endif %}
        {% endfor %}
    </div>

    {% if sidebar_show_all_applications %}
        <div class="mt-auto" x-data="{ openAllApplications: false }">
            <a class="cursor-pointer flex items-center px-6 py-3 text-sm dark:text-font-default-dark hover:text-primary-600 dark:hover:text-primary-500" x-on:click="openAllApplications = !openAllApplications">
                <span class="material-symbols-outlined md-18 mr-3">
                    apps
                </span>

                <span>
                    {% trans "All applications" %}
                </span>
            </a>

            <div class="absolute bottom-0 left-0 right-0 top-0 z-50 md:left-72" x-cloak x-show="openAllApplications">
                <div class="absolute bg-base-900 bg-opacity-80 backdrop-blur-sm bottom-0 left-0 right-0 top-0 z-10 w-screen"></div>

                <div class="bg-white flex flex-col h-full overflow-x-hidden overflow-y-auto py-5 px-8 relative text-sm w-80 z-20 dark:bg-base-900 dark:border-r dark:border-base-800" x-on:click.outside="openAllApplications = false" x-on:keydown.escape.window="openAllApplications = false">
                    {% for app in app_list %}
                        <div class="mb-6 last:mb-0">
                            <h2 class="mb-4 font-semibold text-font-important-light truncate dark:text-font-important-dark">
                                {{ app.name }}
                            </h2>

                            <ul>
                                {% for model in app.models %}
                                    <li class="block mb-4 last:mb-0">
                                        <a href="{{ model.admin_url }}" class="block truncate hover:text-primary-600 dark:hover:text-primary-500">
                                            {{ model.name }}
                                        </a>
                                    </li>
                                {% endfor %}
                            </ul>
                        </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    {% endif %}
{% else %}
    <p>{% translate "You don’t have permission to view or edit anything." %}</p>
{% endif %}
