@php
use Illuminate\Support\Facades\Route;
use App\Services\AddonService\IAddonService;
$addonService = app(IAddonService::class);
$currentRouteName = Route::currentRouteName() ?? '';
@endphp

<ul class="menu-sub">
  @if (isset($menu))
    @foreach ($menu as $submenu)
      {{-- Check addon availability --}}
      @if(isset($submenu->addon))
        @php
          if(!$addonService->isAddonEnabled($submenu->addon)){
            continue;
          }
        @endphp
      @endif

      {{-- Hide SaaS-only menus from tenant context --}}
      @if(isset($submenu->addon) && $submenu->addon === 'MultiTenancyCore')
        @php
          if(function_exists('isTenant') && isTenant()){
            continue;
          }
        @endphp
      @endif

      @php
        $activeClass = null;
        $active = $configData["layout"] === 'vertical' ? 'active open' : 'active';
        $hasSubmenu = isset($submenu->submenu);

        if ($currentRouteName === ($submenu->slug ?? null)) {
            $activeClass = $hasSubmenu ? $active : 'active';

            // Additional check for query parameters in URL
            if (isset($submenu->url) && str_contains($submenu->url, '?')) {
                $urlParts = parse_url($submenu->url);
                if (isset($urlParts['query'])) {
                    parse_str($urlParts['query'], $submenuParams);
                    $currentParams = request()->query();

                    $paramsMatch = true;
                    foreach ($submenuParams as $key => $value) {
                        if (!isset($currentParams[$key]) || $currentParams[$key] != $value) {
                            $paramsMatch = false;
                            break;
                        }
                    }

                    if (!$paramsMatch) {
                        $activeClass = null;
                    }
                }
            }
        }
        elseif ($hasSubmenu) {
          if (is_array($submenu->slug ?? null)) {
            foreach($submenu->slug as $slug){
              if (str_contains($currentRouteName, $slug) && str_starts_with($currentRouteName, $slug)) {
                  $activeClass = $active;
              }
            }
          }
          elseif (isset($submenu->slug) && str_contains($currentRouteName, $submenu->slug) && str_starts_with($currentRouteName, $submenu->slug)) {
            $activeClass = $active;
          }
        }
      @endphp

      <li class="menu-item {{ $activeClass }}" wire:key="submenu-{{ $parentIndex }}-{{ $loop->index }}">
        <a href="{{ isset($submenu->url) ? url($submenu->url) : 'javascript:void(0)' }}"
           class="{{ $hasSubmenu ? 'menu-link menu-toggle' : 'menu-link' }}"
           @if(isset($submenu->target) && !empty($submenu->target)) target="_blank" @endif>
          @if (isset($submenu->icon))
            <i class="{{ $submenu->icon }}"></i>
          @endif
          <div>{{ isset($submenu->name) ? __($submenu->name) : '' }}</div>
          @isset($submenu->badge)
            <div class="badge bg-{{ $submenu->badge[0] }} rounded-pill ms-auto">{{ $submenu->badge[1] }}</div>
          @endisset
        </a>

        {{-- Nested submenu --}}
        @if ($hasSubmenu)
          @include('livewire.partials.sidebar-submenu', ['menu' => $submenu->submenu, 'parentIndex' => $parentIndex . '-' . $loop->index])
        @endif
      </li>
    @endforeach
  @endif
</ul>
