<?php

/**
 * Theme setup.
 */


function tailpress_setup()
{


	// ##### Setup 🡒 Navigation

	register_nav_menus(
		array(
			'homegrade' => __('Menu Homegrade', 'homegrade-theme__texte-backoffice'),
			'renovateur' => __('Menu Rénovateur', 'homegrade-theme__texte-backoffice'),
			'footer' => __('Footer', 'homegrade-theme__texte-backoffice'),
		)
	);

	// ##### 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_theme_support('disable-custom-colors');
	// add_theme_support('disable-custom-font-sizes');
}

add_action('after_setup_theme', 'tailpress_setup');



/* -----------------------------------------------------------
	Enqueue Theme assets 🡒 Front
 -----------------------------------------------------------*/

function homegrade_enqueue_scripts()
{
	$theme = wp_get_theme();
	// wp_enqueue_script('rank-math-integration',  get_template_directory_uri() . '/assets/rank-math-integration.js', ['wp-hooks', 'rank-math-analyzer'], false, true);
	wp_enqueue_script('gsap', get_template_directory_uri() . '/assets/gsap/gsap.min.js', array(), $theme->get('Version'));
	wp_enqueue_script('swiperjs', get_template_directory_uri() . '/assets/swiper/swiper-bundle_11.min.js', array(), $theme->get('Version'));
	wp_enqueue_style('swipercss', get_template_directory_uri() . '/assets/swiper/swiper-bundle.min.css', array(), $theme->get('Version'));
	wp_enqueue_style('homegrade-main-css', get_template_directory_uri() . '/css/app.css', array(), $theme->get('Version'));

	// #PASSING TRANSLATIONS
	wp_register_script('main-app-js', get_template_directory_uri() . '/js/app.js', array('gsap', 'jquery'), $theme->get('Version'));

	$main_app_js_dynamic_datas = array(
		'yes' => __('Oui', 'homegrade-theme__texte-fonctionnel'),
		'no' => __('Non', 'homegrade-theme__texte-fonctionnel'),
		'neutral' => __('Neutre', 'homegrade-theme__texte-fonctionnel'),
		'deleteFilter' => __('Supprimer le filtre', 'homegrade-theme__texte-fonctionnel'),
		'template_directory_uri' => get_template_directory_uri(),
		'current_thematique' => get_the_terms(get_the_ID(), 'thematiques')[0] ?? null,
		'privilegier' => __('À privilégier', 'homegrade-theme__texte-fonctionnel'),
		'prudence' => __('Prudence', 'homegrade-theme__texte-fonctionnel'),
		'ecarter' => __('À écarter', 'homegrade-theme__texte-fonctionnel'),
	);
	wp_localize_script('main-app-js', 'mainAppJsDynamicDatas', $main_app_js_dynamic_datas);

	wp_enqueue_script('main-app-js');
}
add_action('wp_enqueue_scripts', 'homegrade_enqueue_scripts');



/* -----------------------------------------------------------
Enqueue Theme assets 🡒 Back
-----------------------------------------------------------*/
add_action('enqueue_block_editor_assets', 'homegrade_enqueue_scripts');

function homegrade_enqueue_back_scripts()
{
	wp_enqueue_script('metiers-theme-editor-js', get_stylesheet_directory_uri()  . '/js/editor.js', array(), $theme->get('Version'));
}

add_action('enqueue_block_editor_assets', 'homegrade_enqueue_scripts');


// ALSO SEE add_editor_style('css/editor-style.css') line 41



/**
 * Get asset path.
 *
 * @param string $path Path to asset.
 *
 * @return string
 */
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'
 -----------------------------------------------------------*/
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);



add_filter('rank_math/sitemap/enable_caching', '__return_false');
