<?php
/**
 * FansPaysite asset enqueuing.
 *
 * Loads Fomantic UI as individual components (~300 KB) instead of the
 * monolithic bundle (~1.6 MB CSS + 394 KB JS) for better Core Web Vitals.
 *
 * @package FansPaysite
 * @since   2.2
 */

/**
 * Enqueue the main theme stylesheet with cache-busting version.
 */
function fanspaysite_styles() {
	$path = get_stylesheet_directory() . '/style.css';
	$version = file_exists( $path ) ? filemtime( $path ) : '2.2';
	wp_enqueue_style( 'fanspaysite-style', get_stylesheet_uri(), array(), $version );
	wp_style_add_data( 'fanspaysite-style', 'rtl', 'replace' );
}
add_action( 'wp_enqueue_scripts', 'fanspaysite_styles' );

/**
 * Enqueue scripts and styles.
 *
 * Fomantic UI is loaded as individual component files for the modules
 * actually used by the theme. This reduces CSS from ~1.6 MB to ~300 KB
 * and JS from ~394 KB to ~80 KB.
 */
function fanspaysite_enqueue_scripts() {
	$tpl = get_template_directory_uri();

	// Font Awesome CSS
	wp_enqueue_style( 'font-awesome', $tpl . '/lib/fa/css/all.min.css', array(), '5.15.4' );

	// Fomantic UI — component CSS (only the modules this theme uses)
	$fomantic_css = array(
		'site', 'reset', 'menu', 'dropdown', 'button', 'divider',
		'image', 'label', 'comment', 'header', 'segment', 'form',
		'message', 'transition',
	);
	foreach ( $fomantic_css as $component ) {
		wp_enqueue_style(
			'fomantic-' . $component,
			$tpl . '/lib/fomantic/components/' . $component . '.min.css',
			array(),
			'2.9.3'
		);
	}

	// Fomantic UI — component JS (only dropdown + its dependency transition)
	wp_enqueue_script( 'fomantic-site', $tpl . '/lib/fomantic/components/site.min.js', array( 'jquery' ), '2.9.3', true );
	wp_enqueue_script( 'fomantic-transition', $tpl . '/lib/fomantic/components/transition.min.js', array( 'jquery', 'fomantic-site' ), '2.9.3', true );
	wp_enqueue_script( 'fomantic-dropdown', $tpl . '/lib/fomantic/components/dropdown.min.js', array( 'jquery', 'fomantic-transition' ), '2.9.3', true );

	// Theme menu handler
	$menu_path = get_template_directory() . '/js/menu.js';
	$menu_ver  = file_exists( $menu_path ) ? filemtime( $menu_path ) : '2.2';
	wp_enqueue_script( 'fanspaysite-menu', $tpl . '/js/menu.js', array( 'jquery', 'fomantic-dropdown' ), $menu_ver, true );

	// Archive mode JS — handles fullpage navigation and auto-pagination for all modes.
	if ( is_home() || is_archive() || is_search() ) {
		$archive_path = get_template_directory() . '/js/archive.js';
		$archive_ver  = file_exists( $archive_path ) ? filemtime( $archive_path ) : '2.3';
		wp_enqueue_script( 'fanspaysite-archive', $tpl . '/js/archive.js', array(), $archive_ver, true );
	}
}
add_action( 'wp_enqueue_scripts', 'fanspaysite_enqueue_scripts' );

/**
 * Preload critical font files to reduce icon rendering delay.
 */
function fanspaysite_preload_fonts() {
	$woff2 = get_template_directory_uri() . '/lib/fa/webfonts/fa-solid-900.woff2';
	echo '<link rel="preload" href="' . esc_url( $woff2 ) . '" as="font" type="font/woff2" crossorigin>' . "\n";
}
add_action( 'wp_head', 'fanspaysite_preload_fonts', 2 );

/**
 * Register editor styles for the block editor.
 */
function fanspaysite_add_editor_styles() {
	add_editor_style( 'editor-style.css' );
}
add_action( 'admin_init', 'fanspaysite_add_editor_styles' );
