<?php
/**
 * Bootstrap navbar-walker
 *
 * @package wpstream_theme
 */

/**
 * Bootstrap 5 WP Nav Menu Walker.
 */
class WpStream_Bootstrap_5_Wp_Nav_Menu_Walker extends Walker_Nav_Menu {

	/**
	 * Current menu item.
	 *
	 * @var object
	 */
	private $current_item;

	/**
	 * Dropdown menu alignment values.
	 *
	 * @var array
	 */
	private $dropdown_menu_alignment_values = array(
		'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',
	);

	/**
	 * Menu ul open tag start
	 *
	 * @param string $output Used to append additional content (passed by reference).
	 * @param int    $depth  Depth of category. Used for tab indentation.
	 * @param array  $args   An array of arguments.
	 * @return void
	 */
	public function start_lvl( &$output, $depth = 0, $args = null ) {
		$dropdown_menu_class[] = '';
		foreach ( $this->current_item->classes as $class ) {
			if ( in_array( $class, $this->dropdown_menu_alignment_values, true ) ) {
				$dropdown_menu_class[] = $class;
			}
		}
		$indent  = str_repeat( "\t", $depth );
		$submenu = ( $depth > 0 ) ? ' sub-menu' : '';

		

    	$menu_id = isset($args->menu_id) ? $args->menu_id : '';

        $output .= "\n$indent<ul id=\"" . esc_attr( $menu_id ) . "\" class=\"dropdown-menu$submenu " . esc_attr( implode( ' ', $dropdown_menu_class ) ) . " depth_$depth\">\n";
  	}

	/**
	 * Start the li element output.
	 *
	 * @param string &$output      Used to append additional content (passed by reference).
	 * @param object $item         The current menu item.
	 * @param int    $depth        Depth of the current menu item.
	 * @param array  $args         An array of arguments.
	 * @param int    $id           ID of the current menu item.
	 */


	 public 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;
	
		// Check if the item has children
		$has_children = !empty($args->walker->has_children);
	
		$classes[] = $has_children ? 'dropdown' : '';
		$classes[] = 'nav-item';
		$classes[] = 'nav-item-' . $item->ID;
		// Remove 'dropdown-menu dropdown-menu-end' classes from second level items
		if ($depth > 0 && $has_children) {
			// This line adds 'dropdown-menu dropdown-menu-end' class to sub-menus; you might want to remove or adjust it
			// For example, you can remove the class addition here entirely or modify it based on your needs
		}
	
		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
		$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 . '>';
		
		$icon = get_post_meta($item->ID, '_menu_item_icon', true);
      

	
		$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' : '';
		$nav_link_class = ( $depth > 0 ) ? 'dropdown-item ' : 'nav-link ';
		$attributes    .= $has_children ? ' class="' . $nav_link_class . $active_class . ' dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"' : ' class="' . $nav_link_class . $active_class . '"';
		//data-bs-toggle="dropdown" was before moving to hober
		$item_output  = $args->before;
		$item_output .= '<a' . $attributes . '>';
		
		if (!empty($icon)) {
	
			$item_output .=  wpstream_theme_get_svg_icon_menu ($icon) ;
        }
		
		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
		$item_output .= '</a>';
		$item_output .= $args->after;
	
		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
	}
	
	
}
