<?php
/**
 * Theme setup and frontend assets.
 *
 * @package Fragrance_Trade
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/** Register theme features and menus. */
function fragrance_trade_setup() {
	load_theme_textdomain( 'fragrance-trade', get_template_directory() . '/languages' );
	add_theme_support( 'title-tag' );
	add_theme_support( 'custom-logo', array( 'height' => 96, 'width' => 260, 'flex-height' => true, 'flex-width' => true ) );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption', 'style', 'script' ) );
	add_image_size( 'fragrance-capability', 760, 720, true );

	register_nav_menus(
		array(
			'primary' => __( '主导航', 'fragrance-trade' ),
		)
	);
}
add_action( 'after_setup_theme', 'fragrance_trade_setup' );

/**
 * Add Bootstrap 5 nav-item / nav-link classes to primary menu.
 *
 * Without these classes the offcanvas mobile menu items may not render
 * properly because Bootstrap's offcanvas CSS relies on them for display.
 */
function fragrance_trade_nav_menu_css_class( $classes, $item, $args ) {
	if ( 'primary' === $args->theme_location ) {
		$classes[] = 'nav-item';
	}
	return $classes;
}
add_filter( 'nav_menu_css_class', 'fragrance_trade_nav_menu_css_class', 10, 3 );

function fragrance_trade_nav_menu_link_attributes( $atts, $item, $args ) {
	if ( 'primary' === $args->theme_location ) {
		$atts['class'] = isset( $atts['class'] ) ? $atts['class'] . ' nav-link' : 'nav-link';
	}
	return $atts;
}
add_filter( 'nav_menu_link_attributes', 'fragrance_trade_nav_menu_link_attributes', 10, 3 );

/**
 * Return a cache-busting version for a local theme asset.
 *
 * @param string $relative_path Path relative to the theme root.
 * @param string $fallback      Version used when the file cannot be read.
 * @return string
 */
function fragrance_trade_asset_version( $relative_path, $fallback ) {
	$file = get_theme_file_path( '/' . ltrim( $relative_path, '/' ) );
	return file_exists( $file ) ? (string) filemtime( $file ) : $fallback;
}

/** Load theme styles and scripts through the WordPress dependency system. */
function fragrance_trade_enqueue_assets() {
	$version = wp_get_theme()->get( 'Version' );

	wp_enqueue_style( 'fragrance-bootstrap', get_template_directory_uri() . '/assets/vendor/bootstrap/bootstrap.min.css', array(), fragrance_trade_asset_version( 'assets/vendor/bootstrap/bootstrap.min.css', '5.3.8' ) );
	wp_enqueue_style( 'fragrance-base', get_stylesheet_uri(), array( 'fragrance-bootstrap' ), fragrance_trade_asset_version( 'style.css', $version ) );
	wp_enqueue_style( 'fragrance-theme', get_template_directory_uri() . '/assets/css/theme.css', array( 'fragrance-base' ), fragrance_trade_asset_version( 'assets/css/theme.css', $version ) );

	wp_enqueue_script( 'fragrance-bootstrap', get_template_directory_uri() . '/assets/vendor/bootstrap/bootstrap.bundle.min.js', array(), fragrance_trade_asset_version( 'assets/vendor/bootstrap/bootstrap.bundle.min.js', '5.3.8' ), true );
	wp_enqueue_script( 'fragrance-theme', get_template_directory_uri() . '/assets/js/theme.js', array( 'fragrance-bootstrap' ), fragrance_trade_asset_version( 'assets/js/theme.js', $version ), true );
}
add_action( 'wp_enqueue_scripts', 'fragrance_trade_enqueue_assets' );
