@props([
    'megaSidebar',
    'badges',
    'groupKey',
    'items',
    'hrSections' => [],
    'groupLabel' => '',
])

@php
    $adminHeaderShown = false;

    $megaSearchText = function (array $item): string {
        $text = (string) ($item['label'] ?? '');
        if (! empty($item['label_key'])) {
            $text .= ' '.__($item['label_key'], [], 'en').' '.__($item['label_key'], [], 'ar');
        }

        return $text;
    };
@endphp
<div class="erp-mega-browse-search" data-erp-mega-search>
    <div class="erp-mega-browse-search__input">
        <i class="fe fe-search" aria-hidden="true"></i>
        <input
            type="search"
            class="form-control form-control-sm"
            data-erp-mega-search-input
            placeholder="{{ __('nav.search_module_placeholder', ['module' => $groupLabel]) }}"
            aria-label="{{ __('nav.search_module_placeholder', ['module' => $groupLabel]) }}"
        >
    </div>

    @if ($groupKey === 'hr' && $hrSections !== [])
        <div class="erp-mega-browse-grid" role="navigation" aria-label="{{ __('nav.group.hr') }}">
            @foreach ($hrSections as $section)
                <section class="erp-mega-browse-card {{ $section['key'] === 'recruitment' ? 'erp-mega-browse-card--wide' : '' }}"
                    data-hr-section="{{ $section['key'] }}">
                    <div class="erp-mega-browse-card__title">
                        <i class="{{ $section['icon'] ?? 'fe fe-grid' }}" aria-hidden="true"></i>
                        <span>{{ $section['label'] }}</span>
                    </div>
                    <div class="erp-mega-browse-card__divider" aria-hidden="true"></div>
                    <nav class="erp-mega-browse-card__links">
                        @foreach ($section['items'] as $item)
                            @php $url = $megaSidebar->resolveUrl($item); @endphp
                            @continue($url === null)
                            <a href="{{ $url }}"
                                class="mega-nav-link mega-nav-link--dense {{ $megaSidebar->isItemActive($item) ? 'is-active' : '' }}"
                                data-mega-search-item
                                data-mega-search-text="{{ $megaSearchText($item) }}">
                                <span class="text-truncate">{{ $item['label'] }}</span>
                                @if (isset($badges[(string) ($item['route'] ?? '')]))
                                    <span class="badge bg-warning text-dark">{{ $badges[(string) $item['route']] }}</span>
                                @endif
                            </a>
                        @endforeach
                    </nav>
                </section>
            @endforeach
        </div>
    @else
        <div class="erp-mega-section-title">{{ __('nav.mega.browse') }}</div>
        <div class="d-flex flex-column gap-1">
            @foreach ($items as $item)
                @php
                    $url = $megaSidebar->resolveUrl($item);
                    $rn = (string) ($item['route'] ?? '');
                @endphp
                @continue($url === null)
                @if (str_starts_with($rn, 'admin.') && ! $adminHeaderShown)
                    @php $adminHeaderShown = true; @endphp
                    <div class="dropdown-divider my-2"></div>
                    <div class="erp-mega-section-title mb-1">{{ __('nav.mega.platform_admin') }}</div>
                @endif
                <a href="{{ $url }}" class="mega-nav-link {{ $megaSidebar->isItemActive($item) ? 'is-active' : '' }}"
                    data-mega-search-item
                    data-mega-search-text="{{ $megaSearchText($item) }}">
                    <span>{{ $item['label'] }}</span>
                    @if (isset($badges[$rn]))
                        <span class="badge bg-warning text-dark ms-1">{{ $badges[$rn] }}</span>
                    @endif
                </a>
            @endforeach
        </div>
    @endif

    <div class="erp-mega-browse-search__empty text-muted small px-2 py-3 text-center" data-erp-mega-search-empty hidden>
        {{ __('nav.search_empty') }}
    </div>
</div>
