<?php

/**
 * Theme setup.
 */
function tailpress_setup()
{

	// ##### Setup 🡒 Navigation

	register_nav_menus(
		array(
			'primary' => __('Primary Menu', 'tailpress'),
		)
	);

	// ##### Setup 🡒 Ajout des themes supports

	add_theme_support(
		'html5',
		array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
		)
	);
	add_theme_support('title-tag');
	add_theme_support('custom-logo');
	add_theme_support('post-thumbnails');

	add_theme_support('align-wide');
	add_theme_support('wp-block-styles');

	add_theme_support('editor-styles');
	add_editor_style('css/editor-style.css');
	add_theme_support('widgets');
	// add_post_type_support( 'page', 'excerpt' );
}

add_action('after_setup_theme', 'tailpress_setup');



// ##### Menu Walker 
// if (!function_exists('register_navwalker')) :
// 	function register_navwalker() {
// 			require('includes/navwalker.php');
// 	}
// endif;
// add_action('after_setup_theme', 'register_navwalker');




// ##### Walker to ReWrap li submenu parent with button instead of <a>
function wrap_parent_menu_item_buttons($output, $item, $depth, $args)
{

	if ($args->theme_location === "primary" && 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>';
	}
	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);





// ##### Enqueue Theme assets 🡒 Front

function tailpress_enqueue_scripts()
{
	$theme = wp_get_theme();

	wp_enqueue_style('tailpress', tailpress_asset('css/app.css'), array(), $theme->get('Version'));
	wp_enqueue_script('tailpress', tailpress_asset('js/app.js'), array(), $theme->get('Version'));
}


// ##### Enqueue Theme assets 🡒 Back

function enqueue_gutenberg_back_styles()
{
	wp_enqueue_style('tailpress_back', tailpress_asset('css/app.css'), array());
}
add_action('enqueue_block_editor_assets', 'enqueue_gutenberg_back_styles');
add_action('wp_enqueue_scripts', 'tailpress_enqueue_scripts');

function tailpress_asset($path)
{
	if (wp_get_environment_type() === 'production') {
		return get_stylesheet_directory_uri() . '/' . $path;
	}

	return add_query_arg('time', time(),  get_stylesheet_directory_uri() . '/' . $path);
}

/**
 * Adds option 'li_class' to 'wp_nav_menu'.
 *
 * @param string  $classes String of classes.
 * @param mixed   $item The curren item.
 * @param WP_Term $args Holds the nav menu arguments.
 *
 * @return array
 */
function tailpress_nav_menu_add_li_class($classes, $item, $args, $depth)
{
	if (isset($args->li_class)) {
		$classes[] = $args->li_class;
	}

	if (isset($args->{"li_class_$depth"})) {
		$classes[] = $args->{"li_class_$depth"};
	}

	return $classes;
}

add_filter('nav_menu_css_class', 'tailpress_nav_menu_add_li_class', 10, 4);

/**
 * Adds option 'submenu_class' to 'wp_nav_menu'.
 *
 * @param string  $classes String of classes.
 * @param mixed   $item The curren item.
 * @param WP_Term $args Holds the nav menu arguments.
 *
 * @return array
 */
function tailpress_nav_menu_add_submenu_class($classes, $args, $depth)
{
	if (isset($args->submenu_class)) {
		$classes[] = $args->submenu_class;
	}

	if (isset($args->{"submenu_class_$depth"})) {
		$classes[] = $args->{"submenu_class_$depth"};
	}

	return $classes;
}

add_filter('nav_menu_submenu_css_class', 'tailpress_nav_menu_add_submenu_class', 10, 3);
