<?php

$menu = wp_get_nav_menu_object('Шапка сайта');

if ($menu) {
    $menu_items = wp_get_nav_menu_items($menu->term_id);
    $current_lang = function_exists('pll_current_language') ? pll_current_language() : null;

    if ($menu_items) {
        $clean_title = function($title) {
            return trim(preg_replace('/\s*\([^)]*\)/', '', $title));
        };

        $current_region = 'RU';

        $navigation_schema = array(
            "@context" => "https://schema.org",
            "@type" => "SiteNavigationElement",
            "name" => "Главная навигация сайта",
            "description" => "Основное меню навигации по сайту",
            "inLanguage" => $current_lang ?: 'ru',
            "areaServed" => $current_region,
            "url" => array()
        );
        ?>

        <nav class="main-navigation primary-navigation h-full"
             role="navigation"
             aria-label="Основное меню сайта"
             itemscope
             itemtype="https://schema.org/SiteNavigationElement"
             data-region="<?php echo esc_attr($current_region); ?>"
             data-lang="<?php echo esc_attr($current_lang ?: 'ru'); ?>">

            <p class="screen-reader-text">Главное меню</p>

            <ul class="max-[1300px]:gap-[16px] max-[1140px]:gap-[8px] max-[1050px]:gap-[16px] flex items-center h-full gap-[30px] justify-between nav-menu main-menu" role="menubar" aria-label="Главная навигация">
                <?php
                $position = 1;

                foreach ($menu_items as $index => $item) {
                    if ($current_lang === 'gym' && $item->type === 'post_type_archive' && $item->object === 'training') {
                        continue;
                    }
                    if ($item->type === 'taxonomy') {
                        continue;
                    }
                    $url = $item->url;
                    $title = $clean_title($item->title);

                    if ($current_lang && $item->object == 'page' && function_exists('pll_get_post')) {
                        $translated_page_id = pll_get_post($item->object_id, $current_lang);
                        if ($translated_page_id) {
                            $url = get_permalink($translated_page_id);
                            $translated_title = get_the_title($translated_page_id);
                            if ($translated_title) {
                                $title = $clean_title($translated_title);
                            }
                        }
                    }

                    $last_modified = '';
                    if ($item->object == 'page') {
                        $last_modified = get_the_modified_time('c', $item->object_id);
                    }

                    $is_current = false;
                    $aria_current = '';

                    if (is_page()) {
                        global $post;
                        if ($item->object == 'page') {
                            if (($current_lang && function_exists('pll_get_post') && pll_get_post($item->object_id, $current_lang) == $post->ID) ||
                                $item->object_id == $post->ID) {
                                $is_current = true;
                                $aria_current = ' aria-current="page"';
                            }
                        }
                    }

                    $css_classes = array(
                        'menu-item',
                        'menu-item-' . $item->ID,
                        'menu-item-type-' . $item->type,
                        'menu-item-object-' . $item->object,
                        'menu-order-' . $item->menu_order
                    );

                    if ($is_current) {
                        $css_classes[] = 'current-menu-item';
                        $css_classes[] = 'current_page_item';
                        $css_classes[] = 'border-b border-b-[2px] !border-b-[#e21e24] dark:!border-b-[#fff]';
                    }

                    if (!empty($item->classes)) {
                        $css_classes = array_merge($css_classes, array_filter($item->classes));
                    }

                    $page_data = array(
                        "@type" => "WebPage",
                        "name" => $title,
                        "url" => $url,
                        "position" => $position
                    );

                    if ($last_modified) {
                        $page_data['dateModified'] = $last_modified;
                    }

                    $navigation_schema['url'][] = $page_data;
                    ?>

                    <li class="h-full transition border-b border-b-[2px] border-b-[transparent] hover:border-b-[#e21e24] dark:hover:border-b-[#fff] <?php echo esc_attr(implode(' ', $css_classes)); ?>" role="none">
                        <a href="<?php echo esc_url($url); ?>"
                           class="menu-link  !no-underline h-full flex items-center"
                           role="menuitem"
                           itemprop="url"
                           tabindex="0"<?php echo $aria_current; ?>
                           title="<?php echo esc_attr('Перейти на страницу: ' . $title); ?>"
                           data-menu-item="<?php echo esc_attr($title); ?>"
                           data-menu-position="<?php echo $position; ?>"
                           onclick="if(typeof yaCounter !== 'undefined') { yaCounter.reachGoal('menu_click', {item: '<?php echo esc_js($title); ?>'}); }"
                           <?php if ($item->target): ?>target="<?php echo esc_attr($item->target); ?>"<?php endif; ?>
                           <?php if ($item->attr_title): ?>title="<?php echo esc_attr($item->attr_title); ?>"<?php endif; ?>>
                            <span class="translate-y-[2px]" itemprop="name"><?php echo esc_html($title); ?></span>
                        </a>
                    </li>

                    <?php
                    if ($index < count($menu_items) - 1): ?>
                        <li class="menu-separator" role="none" aria-hidden="true">
                            <span class="separator">
                                <svg width="7" height="15" viewBox="0 0 7 15" fill="none" xmlns="http://www.w3.org/2000/svg">
                                    <path d="M0.574219 14.4345L5.55022 0.5625H6.92622L1.93422 14.4345H0.574219Z"
                                          class="fill-[#E0E0E0] dark:fill-[#F8F8F8] dark:opacity-20" />
                                </svg>
                            </span>
                        </li>
                    <?php endif;

                    $position++;
                } ?>
            </ul>
        </nav>

        <?php
        ?>
        <script type="application/ld+json">
        <?php echo json_encode($navigation_schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>
        </script>

        <?php
    }
}
?>