<?php
// bootstrap 5 wp_nav_menu walker with 3rd level support
class bootstrap_5_wp_nav_menu_walker extends Walker_Nav_menu
{
  private $current_item;
  private $dropdown_menu_alignment_values = [
    'dropdown-menu-start',
    'dropdown-menu-end',
    'dropdown-menu-sm-start',
    'dropdown-menu-sm-end',
    'dropdown-menu-md-start',
    'dropdown-menu-md-end',
    'dropdown-menu-lg-start',
    'dropdown-menu-lg-end',
    'dropdown-menu-xl-start',
    'dropdown-menu-xl-end',
    'dropdown-menu-xxl-start',
    'dropdown-menu-xxl-end'
  ];

  function start_lvl(&$output, $depth = 0, $args = null)
  {
    $dropdown_menu_class = ['dropdown-menu'];
    
    // Add alignment classes
    foreach($this->current_item->classes as $class) {
      if(in_array($class, $this->dropdown_menu_alignment_values)) {
        $dropdown_menu_class[] = $class;
      }
    }
    
    // For 2nd level and deeper, add dropdown-submenu class
    if($depth > 0) {
      $dropdown_menu_class[] = 'dropdown-submenu';
    }
    
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent<ul class=\"" . esc_attr(implode(" ", $dropdown_menu_class)) . " depth-$depth\">\n";
  }

  function start_el(&$output, $item, $depth = 0, $args = null, $id = 0)
  {
    $this->current_item = $item;
    $indent = ($depth) ? str_repeat("\t", $depth) : '';
    $li_attributes = '';
    $class_names = $value = '';

    $classes = empty($item->classes) ? array() : (array) $item->classes;
    $classes[] = 'nav-item';
    $classes[] = 'nav-item-' . $item->ID;
    
    // Add dropdown class for parent items
    if ($args->walker->has_children) {
      $classes[] = 'dropdown';
      
      // For 2nd level and deeper, add dropdown-submenu class
      if($depth > 0) {
        $classes[] = 'dropdown-submenu';
      }
    }

    $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
    $class_names = ' class="' . esc_attr($class_names) . '"';

    $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
    $id = strlen($id) ? ' id="' . esc_attr($id) . '"' : '';

    $output .= $indent . '<li ' . $id . $value . $class_names . $li_attributes . '>';

    $attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
    $attributes .= !empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : '';
    $attributes .= !empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
    $attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';

    $active_class = ($item->current || $item->current_item_ancestor || in_array("current_page_parent", $item->classes, true) || in_array("current-post-ancestor", $item->classes, true)) ? 'active' : '';
    
    // Different classes for different levels
    if($depth === 0) {
      $nav_link_class = 'nav-link ';
    } elseif($depth === 1) {
      $nav_link_class = 'dropdown-item  ';
    } else {
      $nav_link_class = 'dropdown-item ';
    }
    
    // Add dropdown attributes for parent items
    $dropdown_attributes = ($args->walker->has_children) ? ' data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"' : '';
    $attributes .= ' class="' . $nav_link_class . $active_class . '"' . $dropdown_attributes;

    $item_output = $args->before;
    $item_output .= '<a' . $attributes . '>';
    $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
    
    // Add dropdown arrow for parent items
    if($args->walker->has_children && $depth < 2) {
      $item_output .= '<span class="dropdown-toggle-arrow"><i class="bi bi-chevron-down"></i></span>';
    }
    
    $item_output .= '</a>';
    $item_output .= $args->after;

    $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
  }
}

// register a new menu
register_nav_menu('main-menu', 'Main menu');