<?php
/**
 * Bootstrap 5 NavWalker for WordPress
 * 
 * Renders wp_nav_menu() output as Bootstrap 5 compatible markup
 * with dropdown support for the offcanvas menu.
 */

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

class WPBS_Nav_Walker extends Walker_Nav_Menu {

    public function start_lvl(&$output, $depth = 0, $args = null) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class=\"dropdown-menu\">\n";
    }

    public function start_el(&$output, $item, $depth = 0, $args = null, $id = 0) {
        $indent = ($depth) ? str_repeat("\t", $depth) : '';
        
        $classes = empty($item->classes) ? [] : (array) $item->classes;
        $classes[] = 'nav-item';
        
        // Check if item has children
        $has_children = in_array('menu-item-has-children', $classes);
        if ($has_children) {
            $classes[] = 'dropdown';
        }
        
        // Active state
        if (in_array('current-menu-item', $classes) || in_array('current-menu-ancestor', $classes)) {
            $classes[] = 'active';
        }
        
        $class_names = implode(' ', array_filter($classes));
        $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
        
        $id_attr = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth);
        $id_attr = $id_attr ? ' id="' . esc_attr($id_attr) . '"' : '';
        
        $output .= $indent . '<li' . $id_attr . $class_names . '>';
        
        // Link attributes
        $atts = [];
        $atts['title']  = !empty($item->attr_title) ? $item->attr_title : '';
        $atts['target'] = !empty($item->target) ? $item->target : '';
        $atts['rel']    = !empty($item->xfn) ? $item->xfn : '';
        $atts['href']   = !empty($item->url) ? $item->url : '#';
        
        if ($depth === 0 && $has_children) {
            $atts['class']          = 'nav-link dropdown-toggle';
            $atts['role']           = 'button';
            $atts['data-bs-toggle'] = 'dropdown';
            $atts['aria-expanded']  = 'false';
        } elseif ($depth > 0) {
            $atts['class'] = 'dropdown-item';
        } else {
            $atts['class'] = 'nav-link';
        }
        
        // Active class on link
        if (in_array('current-menu-item', (array) $item->classes)) {
            $atts['class'] .= ' active';
            $atts['aria-current'] = 'page';
        }
        
        $atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args, $depth);
        
        $attributes = '';
        foreach ($atts as $attr => $value) {
            if (!empty($value)) {
                $attributes .= ' ' . $attr . '="' . esc_attr($value) . '"';
            }
        }
        
        $title = apply_filters('the_title', $item->title, $item->ID);
        $title = apply_filters('nav_menu_item_title', $title, $item, $args, $depth);
        
        $item_output = $args->before ?? '';
        $item_output .= '<a' . $attributes . '>';
        $item_output .= ($args->link_before ?? '') . $title . ($args->link_after ?? '');
        $item_output .= '</a>';
        $item_output .= $args->after ?? '';
        
        $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
    }

    /**
     * Fallback when no menu is assigned
     */
    public static function fallback($args) {
        if (!current_user_can('edit_theme_options')) return;
        
        echo '<ul class="navbar-nav ms-auto">';
        echo '<li class="nav-item">';
        echo '<a class="nav-link" href="' . esc_url(admin_url('nav-menus.php')) . '">';
        echo esc_html__('Set up menu', 'wpbs-theme');
        echo '</a></li></ul>';
    }
}
