<?php
/**
 * Site header.
 *
 * @package ALFASMART
 */

if (! defined('ABSPATH')) {
    exit;
}

$light_logo = alfasmart_get_option_image_url('site_logo', 'logo.webp');
$dark_logo  = alfasmart_get_option_image_url('footer_logo', 'logo-footer.webp');
$home_url   = function_exists('pll_home_url') ? pll_home_url(alfasmart_lang()) : home_url('/');

if (! function_exists('alfasmart_normalize_nav_path')) {
    function alfasmart_normalize_nav_path($url)
    {
        $path = wp_parse_url($url, PHP_URL_PATH);
        $path = is_string($path) ? rawurldecode($path) : '/';
        $path = '/' . ltrim($path, '/');
        $path = untrailingslashit($path);

        return strtolower($path ?: '/');
    }
}

if (! function_exists('alfasmart_nav_last_segment')) {
    function alfasmart_nav_last_segment($path)
    {
        $path = trim((string) $path, '/');

        if ('' === $path) {
            return '';
        }

        $segments = explode('/', $path);

        return end($segments);
    }
}

if (! function_exists('alfasmart_nav_paths_match')) {
    function alfasmart_nav_paths_match($target_path, $current_path)
    {
        if ($target_path === $current_path) {
            return true;
        }

        $target_last  = alfasmart_nav_last_segment($target_path);
        $current_last = alfasmart_nav_last_segment($current_path);

        return '' !== $target_last && '' !== $current_last && $target_last === $current_last;
    }
}

if (! function_exists('alfasmart_current_page_matches_nav_title')) {
    function alfasmart_current_page_matches_nav_title($title)
    {
        if (! is_page()) {
            return false;
        }

        $page = get_queried_object();
        if (! $page || empty($page->post_name)) {
            return false;
        }

        $title      = strtolower(trim(wp_strip_all_tags((string) $title)));
        $page_slug  = strtolower((string) $page->post_name);
        $page_title = strtolower(trim(wp_strip_all_tags(get_the_title($page))));
        $about_ar   = html_entity_decode('&#1605;&#1606; &#1606;&#1581;&#1606;', ENT_QUOTES, 'UTF-8');
        $contact_ar = html_entity_decode('&#1575;&#1578;&#1589;&#1604; &#1576;&#1606;&#1575;', ENT_QUOTES, 'UTF-8');
        $home_ar    = html_entity_decode('&#1575;&#1604;&#1585;&#1574;&#1610;&#1587;&#1610;&#1577;', ENT_QUOTES, 'UTF-8');

        if (in_array($title, array('about', 'about us', $about_ar), true)) {
            return false !== strpos($page_slug, 'about') || false !== strpos($page_title, 'about') || $page_title === $about_ar;
        }

        if (in_array($title, array('contact', 'contact us', $contact_ar), true)) {
            return false !== strpos($page_slug, 'contact') || false !== strpos($page_title, 'contact') || $page_title === $contact_ar;
        }

        if (in_array($title, array('home', $home_ar), true)) {
            return is_front_page() || is_home();
        }

        return '' !== $title && $title === $page_title;
    }
}

if (! function_exists('alfasmart_is_current_nav_url')) {
    function alfasmart_is_current_nav_url($url)
    {
        $request_uri  = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : '/';
        $current_path = alfasmart_normalize_nav_path(home_url($request_uri));
        $target_path  = alfasmart_normalize_nav_path($url);
        $home_path    = alfasmart_normalize_nav_path(function_exists('pll_home_url') ? pll_home_url(alfasmart_lang()) : home_url('/'));

        if (($current_path === '/' || $current_path === $home_path) && ($target_path === '/' || $target_path === $home_path)) {
            return is_front_page() || is_home();
        }

        if (alfasmart_nav_paths_match($target_path, $current_path)) {
            return true;
        }

        foreach (array('companies', 'services') as $post_type) {
            $archive = get_post_type_archive_link($post_type);
            if ($archive && alfasmart_nav_paths_match(alfasmart_normalize_nav_path($archive), $target_path)) {
                return is_post_type_archive($post_type) || is_singular($post_type);
            }
        }

        return false;
    }
}

if (! function_exists('alfasmart_is_current_menu_item')) {
    function alfasmart_is_current_menu_item($item)
    {
        if (! $item) {
            return false;
        }

        $object    = $item->object ?? '';
        $object_id = isset($item->object_id) ? (int) $item->object_id : 0;
        $type       = $item->type ?? '';
        $current_id = get_queried_object_id();
        $item_title = strtolower(trim(wp_strip_all_tags($item->title ?? '')));

        $arabic_home_title = html_entity_decode('&#1575;&#1604;&#1585;&#1574;&#1610;&#1587;&#1610;&#1577;', ENT_QUOTES, 'UTF-8');
        if ((is_front_page() || is_home()) && in_array($item_title, array('home', $arabic_home_title), true)) {
            return true;
        }

        if (alfasmart_current_page_matches_nav_title($item->title ?? '')) {
            return true;
        }

        if ('post_type' === $type && 'page' === $object && $object_id) {
            $page_ids = array($object_id);

            if (function_exists('pll_get_post')) {
                foreach (array('en', 'ar') as $lang) {
                    $translation_id = pll_get_post($object_id, $lang);
                    if ($translation_id) {
                        $page_ids[] = (int) $translation_id;
                    }
                }
            }

            $page_ids = array_values(array_unique(array_filter(array_map('absint', $page_ids))));

            if ($current_id && in_array((int) $current_id, $page_ids, true)) {
                return true;
            }

            if (is_page($page_ids)) {
                return true;
            }
        }

        if ('post_type' === $type && in_array($object, array('companies', 'services'), true) && $object_id) {
            return is_singular($object) && (int) $current_id === $object_id;
        }

        if ('post_type_archive' === $type && $object) {
            return is_post_type_archive($object) || is_singular($object);
        }

        if ('custom' === $type && ! empty($item->url)) {
            foreach (array('companies', 'services') as $post_type) {
                $archive = get_post_type_archive_link($post_type);
                if ($archive && alfasmart_nav_paths_match(alfasmart_normalize_nav_path($archive), alfasmart_normalize_nav_path($item->url))) {
                    return is_post_type_archive($post_type) || is_singular($post_type);
                }
            }
        }

        return ! empty($item->url) && alfasmart_is_current_nav_url($item->url);
    }
}
?>
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php wp_body_open(); ?>
<div class="as-site-shell">
    <header class="as-fixed-header" data-as-header>
        <div class="container as-section-container as-header-inner">
            <a class="as-brand-link" href="<?php echo esc_url($home_url); ?>" aria-label="<?php echo esc_attr(get_bloginfo('name')); ?> home">
                <img src="<?php echo esc_url($light_logo); ?>" data-light-logo="<?php echo esc_url($light_logo); ?>" data-dark-logo="<?php echo esc_url($dark_logo); ?>" alt="<?php echo esc_attr(get_bloginfo('name')); ?>" class="as-brand-logo">
            </a>

            <nav class="as-main-nav" aria-label="<?php esc_attr_e('Primary navigation', 'alfasmart'); ?>">
                <?php
                $items = array();
                if ('ar' === alfasmart_lang()) {
                    $ar_menu = wp_get_nav_menu_object('Primary Menu AR');
                    $items   = $ar_menu ? wp_get_nav_menu_items($ar_menu->term_id) : array();
                }

                if (! $items && has_nav_menu('primary')) {
                    $locations = get_nav_menu_locations();
                    $menu_id   = $locations['primary'] ?? 0;
                    $items     = $menu_id ? wp_get_nav_menu_items($menu_id) : array();
                }

                if ($items) {
                    foreach ((array) $items as $item) {
                        $is_active = alfasmart_is_current_menu_item($item);
                        printf(
                            '<a href="%s" class="as-nav-link%s"%s%s>%s</a>',
                            esc_url($item->url),
                            $is_active ? ' as-nav-active' : '',
                            $is_active ? ' aria-current="page"' : '',
                            $is_active ? ' data-current-nav="true"' : '',
                            esc_html($item->title)
                        );
                    }
                } else {
                    foreach (alfasmart_nav_links() as $link) {
                        $is_active = alfasmart_is_current_nav_url($link['url']) || alfasmart_current_page_matches_nav_title($link['label']);
                        printf(
                            '<a href="%s" class="as-nav-link%s"%s%s>%s</a>',
                            esc_url($link['url']),
                            $is_active ? ' as-nav-active' : '',
                            $is_active ? ' aria-current="page"' : '',
                            $is_active ? ' data-current-nav="true"' : '',
                            esc_html($link['label'])
                        );
                    }
                }
                ?>
            </nav>

            <div class="as-header-actions">
                <button class="as-icon-button as-theme-toggle" type="button" aria-label="<?php esc_attr_e('Toggle theme', 'alfasmart'); ?>">
                    <i class="fa-regular fa-moon" aria-hidden="true"></i>
                </button>

                <?php if (alfasmart_get_option('show_language_switcher', 1)) : ?>
                    <div class="as-language-wrap">
                        <button class="as-language-button" type="button" aria-label="<?php esc_attr_e('Change language', 'alfasmart'); ?>" data-as-language>
                            <?php echo esc_html(strtoupper(function_exists('pll_current_language') ? pll_current_language('slug') : 'en')); ?>
                            <i class="fa-solid fa-chevron-down" aria-hidden="true"></i>
                        </button>
                        <div class="as-language-menu" aria-label="<?php esc_attr_e('Language options', 'alfasmart'); ?>">
                            <?php if (function_exists('pll_the_languages')) : ?>
                                <?php
                                $languages = pll_the_languages(array('raw' => 1, 'hide_if_empty' => 0));
                                foreach ($languages as $language) :
                                    ?>
                                    <a href="<?php echo esc_url($language['url']); ?>">
                                        <?php echo esc_html($language['name']); ?>
                                        <?php if (! empty($language['current_lang'])) : ?>
                                            <i class="fa-solid fa-check" aria-hidden="true"></i>
                                        <?php endif; ?>
                                    </a>
                                <?php endforeach; ?>
                            <?php else : ?>
                                <button type="button">English <i class="fa-solid fa-check" aria-hidden="true"></i></button>
                                <button type="button">Arabic</button>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endif; ?>

                <button class="as-menu-button" type="button" aria-label="<?php esc_attr_e('Open menu', 'alfasmart'); ?>" data-as-menu>
                    <i class="fa-solid fa-bars as-menu-open-icon" aria-hidden="true"></i>
                    <i class="fa-solid fa-xmark as-menu-close-icon" aria-hidden="true"></i>
                </button>
            </div>
        </div>
    </header>

    <main>
