@php
    $user = auth()->user();

    $tmHref = function (?string $route) {
        if (! $route || $route === '#') return '#';
        return \Illuminate\Support\Facades\Route::has($route) ? route($route) : '#';
    };
    $tmCan = fn (array $i) => (! isset($i['permission']) || $user->can($i['permission']))
        && (empty($i['super']) || $user->isSuperAdmin());
    // Active when the current route shares the first segment with an item's route.
    $tmActive = function (?string $route) {
        if (! $route || $route === '#') return false;
        return request()->routeIs(\Illuminate\Support\Str::before($route, '.').'*');
    };

    // Resolve which menus / columns are visible for this user.
    $menus = collect(config('topmenu'))->map(function ($menu) use ($tmCan) {
        if (empty($menu['columns'])) {
            $menu['_visible'] = $tmCan($menu);

            return $menu;
        }
        $menu['columns'] = collect($menu['columns'])->map(function ($col) use ($tmCan) {
            $col['items'] = array_values(array_filter($col['items'], $tmCan));

            return $col;
        })->filter(fn ($col) => ! empty($col['items']))->values()->all();
        $menu['_visible'] = ! empty($menu['columns']);

        return $menu;
    })->filter(fn ($m) => $m['_visible'])->values();
@endphp
<nav class="sms-topmenu">
    <div class="sms-topmenu-inner">
        @foreach($menus as $menu)
            @if(empty($menu['columns']))
                <a class="tm-link {{ $tmActive($menu['route']) ? 'active' : '' }}" href="{{ $tmHref($menu['route']) }}" style="--tm-accent: {{ $menu['color'] ?? '#6366f1' }};">
                    <span class="tm-link-ic"><i class="bi {{ $menu['icon'] }}"></i></span><span>{{ $menu['label'] }}</span>
                </a>
            @else
                @php $active = collect($menu['columns'])->flatMap(fn ($c) => $c['items'])->contains(fn ($i) => $tmActive($i['route'])); @endphp
                @php $accent = $menu['color'] ?? '#6366f1'; @endphp
                <div class="dropdown" style="--tm-accent: {{ $accent }};">
                    <button class="tm-link {{ $active ? 'active' : '' }} dropdown-toggle" data-bs-toggle="dropdown" data-bs-auto-close="true">
                        <span class="tm-link-ic"><i class="bi {{ $menu['icon'] }}"></i></span><span>{{ $menu['label'] }}</span>
                    </button>
                    <div class="dropdown-menu shadow-lg tm-mega">
                        <div class="tm-mega-head">
                            <i class="bi {{ $menu['icon'] }}"></i>
                            <span>{{ $menu['label'] }}</span>
                        </div>
                        <div class="tm-mega-cols">
                            @foreach($menu['columns'] as $col)
                                <div class="tm-col">
                                    <div class="tm-col-head">{{ $col['heading'] }}</div>
                                    @foreach($col['items'] as $item)
                                        <a class="dropdown-item {{ $tmActive($item['route']) ? 'active' : '' }}" href="{{ $tmHref($item['route']) }}">
                                            <i class="bi bi-chevron-right tm-bullet"></i><span>{{ $item['text'] }}</span>
                                        </a>
                                    @endforeach
                                </div>
                            @endforeach
                        </div>
                    </div>
                </div>
            @endif
        @endforeach
    </div>
</nav>
