@php
    $crossDefs = config('product_catalog_cross_nav.links', []);
    if (! is_array($crossDefs) || count($crossDefs) === 0) {
        $crossDefs = [
            ['key' => 'prints', 'path' => '/products', 'label_trans' => 'catalog.cross_nav_prints'],
            ['key' => 'tools_printables', 'path' => '/products/tools-and-printables', 'label_trans' => 'catalog.cross_nav_tools'],
            ['key' => 'books', 'path' => '/products/Montessori-book-series', 'label_trans' => 'catalog.cross_nav_books'],
        ];
    }

    $printsCatId = (int) config('product_catalog_cross_nav.prints_category_id', 13);

    $currentKey = null;
    $segments = request()->segments();
    $productsIdx = array_search('products', $segments, true);

    if ($productsIdx !== false) {
        $slugAfterProducts = $segments[$productsIdx + 1] ?? null;
        $slugLower = strtolower((string) $slugAfterProducts);

        if ($slugLower === 'montessori-book-series') {
            $currentKey = 'books';
        } elseif ($slugLower === 'tools-and-printables' || $slugLower === 'tools-of-ilmonstery') {
            $currentKey = 'tools_printables';
        } elseif ($slugAfterProducts === null && request()->filled('category_id') && (int) request()->query('category_id') === $printsCatId) {
            $currentKey = 'prints';
        }
    }

    $crossLinks = [];
    foreach ($crossDefs as $def) {
        if (empty($def['key']) || empty($def['path']) || empty($def['label_trans'])) {
            continue;
        }
        if ($currentKey !== null && ($def['key'] ?? '') === $currentKey) {
            continue;
        }

        $href = url($def['path']);
        if (($def['key'] ?? '') === 'prints') {
            $href .= '?' . http_build_query(['category_id' => $printsCatId]);
        }

        $crossLinks[] = [
            'url' => $href,
            'label' => trans($def['label_trans']),
        ];
    }
@endphp

@if(count($crossLinks) > 0)
    <div class="imt-catalog-cross-nav imt-catalog-cross-nav--compact">
        <p class="imt-catalog-cross-nav__kicker text-center font-13 font-weight-bold text-gray600">
            {{ trans('catalog.cross_nav_heading') }}
        </p>
        <nav class="imt-catalog-cross-nav__strip d-flex flex-wrap align-items-center justify-content-center"
             aria-label="{{ trans('catalog.cross_nav_aria') }}">
            @foreach($crossLinks as $link)
                <a href="{{ $link['url'] }}" class="imt-catalog-cross-nav__btn text-decoration-none text-center">{{ $link['label'] }}</a>
            @endforeach
        </nav>
    </div>
@endif
