<?php

/* -----------------------------------------------------------
	Walker to ReWrap li submenu parent with button instead of
 -----------------------------------------------------------*/

function wrap_parent_menu_item_buttons($output, $item, $depth, $args)
{
	// write_log($item);

	// $item->classes = array_merge($item->classes, array('menu-item-' . $item->ID));

	// #### MENU HOMEGRADE HEADER
	if ($args->theme_location === "homegrade" && $args->menu_id === "menu-homegrade-navlist" && in_array('menu-item-has-children', $item->classes, true)) {
		$output = '<button type="button" class="menu-item-submenu-toggle" aria-expanded="false" aria-controls="sub-menu-' . $item->ID . '">' . $item->title . '</button>';
	}

	// #### MENU HOMEGRADE FOOTER
	if ($args->theme_location === "homegrade" && $args->menu_class === "menu-homegrade-footer" && in_array('menu-item-has-children', $item->classes, true)) {
		$output = '<h6 class="page-group-title">' . $item->title . '</h6>';
	}



	if ($args->theme_location === "renovateur") {
		$page_icon = get_field('page_icon', $item->object_id);

		if (in_array('menu-item-has-children', $item->classes, true)) {
			$output = '<button type="button" class="menu-item-submenu-toggle" aria-expanded="false" aria-controls="sub-menu-' . $item->ID . '">' . $item->title . '</button>';
		}

		// #### ICONE DE PAGE FOR TOP LEVEL MENU ITEMS
		if (isset($page_icon)) {
			$icon = '<img class="page_icon" src="'  . $page_icon['url'] . '" alt=""/>';
			$link = $output;
			$output = $icon . $link;
		}

		write_log($output);

		// #### TAXONOMY : GET ICON & INFOS ABOUT TAX

		if ($item->type === "taxonomy") {
			$term = get_term_by('id', $item->object_id, 'thematiques');
			$cover = get_field('taxonomy_pictures', "thematiques_" . $item->object_id);

			$output = '<a class="lol" href="' . $item->url . '">';
			$output .= '<img class="thematique-icon" src="' . $cover['icon']['url'] .  '"/>';
			$output .=  '<h6>' . $item->title . "</h6>";
			$output .=  '<p>' . $term->description . "</p>";
			$output .= "</a>";
		}
	}

	if ($args->theme_location === "footer" && in_array('menu-item-has-children', $item->classes, true)) {
		$output = $item->title;
	}
	return $output;
}
add_filter('walker_nav_menu_start_el', 'wrap_parent_menu_item_buttons', 10, 4);






/* -----------------------------------------------------------
	Pass Specific submenus-classnames for menu renovateur
 -----------------------------------------------------------*/



function customize_submenu_classnames($classes, $args, $depth)
{
	// Here we can additionally use menu arguments.
	// if ($args->theme_location === "renovateur") {
	// $default_class_name_key = array_search('sub-menu', $classes);

	// if (false !== $default_class_name_key) {
	//     unset($classes[$default_class_name_key]);
	// }
	// $classes[] = 'header-submenu';
	// $classes[] = "depth-{$depth}";
	// $classes[] = 'sub-menu';
	// $classes[] = 'test';
	// }
	// write_log($classes);
	// write_log($classes);

	return $classes;
}
// add_filter('nav_menu_submenu_css_class', 'customize_submenu_classnames', 10, 3);


/* -----------------------------------------------------------
	FILTER MENU OBJECTS TO BE DISPLAYED
 -----------------------------------------------------------*/


function wpdocs_unset_menu_items($menu_objects, $args)
{
	if ($args->theme_location === "renovateur") {
	}
	return $menu_objects;
}
// add_filter('wp_nav_menu_objects', 'wpdocs_unset_menu_items', 10, 2);