<?php

declare(strict_types=1);

/**
 * Hauptnavigation aus data/navigation.json. Props: $current (Slug).
 * Burger-Toggle + Dropdowns übernimmt assets/js/nav.js.
 * Dropdowns funktionieren auch ohne JS (CSS :hover / :focus-within); JS ergänzt
 * nur aria-expanded, Escape und Klick-außerhalb-schließt.
 * CTAs: am Desktop rendert sie der Header (site-header__actions), im
 * Burger-Modus stehen sie am Ende des Menü-Overlays (site-nav__ctas) —
 * dieselbe Quelle (navigation.json → ctas), CSS blendet je Breakpoint
 * genau eine der beiden Stellen ein.
 */
$nav = json_load('navigation');

$href = static function (string $slug): string {
    // Anker-Slugs ("#aktuelles") zeigen auf Startseiten-Sektionen.
    return str_starts_with($slug, '#') ? url() . $slug : url($slug);
};
?>
            <nav class="site-nav" aria-label="Hauptnavigation">
                <button class="site-nav__toggle" type="button" aria-expanded="false" aria-controls="site-nav-list">
                    <span class="site-nav__toggle-bar" aria-hidden="true"></span>
                    <span class="visually-hidden">Menü öffnen</span>
                </button>
                <ul class="site-nav__list" id="site-nav-list">
<?php foreach ($nav['main'] as $item): ?>
<?php if (!empty($item['children'])): ?>
<?php
    $submenuId = 'submenu-' . preg_replace('/[^a-z0-9]+/', '-', strtolower($item['label']));
    $childActive = false;
    foreach ($item['children'] as $child) {
        if ($child['slug'] === $current) {
            $childActive = true;
            break;
        }
    }
?>
                    <li class="site-nav__item site-nav__item--has-submenu">
                        <button class="site-nav__sublink" type="button" aria-expanded="false" aria-controls="<?= e($submenuId) ?>"<?= $childActive ? ' data-current' : '' ?>><?= e($item['label']) ?><?= icon('chevron-down', 'site-nav__chevron') ?></button>
                        <ul class="site-nav__submenu" id="<?= e($submenuId) ?>">
<?php foreach ($item['children'] as $child): ?>
                            <li<?= !empty($child['separator']) ? ' class="site-nav__submenu-sep"' : '' ?>>
                                <a href="<?= e($href($child['slug'])) ?>"<?= $child['slug'] === $current ? ' aria-current="page"' : '' ?>><?= e($child['label']) ?></a>
                            </li>
<?php endforeach; ?>
                        </ul>
                    </li>
<?php else: ?>
                    <li class="site-nav__item">
                        <a href="<?= e($href($item['slug'])) ?>"<?= $item['slug'] === $current ? ' aria-current="page"' : '' ?>><?= e($item['label']) ?></a>
                    </li>
<?php endif; ?>
<?php endforeach; ?>
<?php if (!empty($nav['ctas'])): ?>
                    <li class="site-nav__ctas">
<?php foreach ($nav['ctas'] as $cta): ?>
<?php component('cta', ['cta' => $cta]); ?>
<?php endforeach; ?>
                    </li>
<?php endif; ?>
                </ul>
            </nav>
