<?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
 */

/**
 * Whether bundled libraries should be loaded from a CDN instead of local files.
 *
 * Disabled by default to preserve WordPress.org-friendly local bundling.
 *
 * @since 2.6
 * @return bool
 */
function fanspaysite_use_cdn_libraries() {
	return (bool) get_option( 'fanspaysite_use_cdn_libraries', false );
}

/**
 * Resolve the URL for a bundled theme library asset.
 *
 * @since 2.6
 * @param string $asset     Asset identifier.
 * @param string $component Optional Fomantic component name.
 * @return string
 */
function fanspaysite_get_library_asset_url( $asset, $component = '' ) {
	$tpl       = get_template_directory_uri();
	$component = sanitize_key( $component );

	if ( fanspaysite_use_cdn_libraries() ) {
		switch ( $asset ) {
			case 'font-awesome-css':
				return 'https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@7.1.0/css/all.min.css';
			case 'font-awesome-solid-woff2':
				return 'https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@7.1.0/webfonts/fa-solid-900.woff2';
			case 'fa-picker-css':
				return 'https://cdn.jsdelivr.net/npm/fontawesome-iconpicker@3.2.0/dist/css/fontawesome-iconpicker.min.css';
			case 'fa-picker-js':
				return 'https://cdn.jsdelivr.net/npm/fontawesome-iconpicker@3.2.0/dist/js/fontawesome-iconpicker.min.js';
			case 'fomantic-css':
				return 'https://cdn.jsdelivr.net/npm/fomantic-ui@2.9.4/dist/components/' . $component . '.min.css';
			case 'fomantic-js':
				return 'https://cdn.jsdelivr.net/npm/fomantic-ui@2.9.4/dist/components/' . $component . '.min.js';
		}
	}

	switch ( $asset ) {
		case 'font-awesome-css':
			return $tpl . '/lib/fa/css/all.min.css';
		case 'font-awesome-solid-woff2':
			return $tpl . '/lib/fa/webfonts/fa-solid-900.woff2';
		case 'fa-picker-css':
			return $tpl . '/lib/fa-picker/css/fontawesome-iconpicker.min.css';
		case 'fa-picker-js':
			return $tpl . '/lib/fa-picker/js/fontawesome-iconpicker.min.js';
		case 'fomantic-css':
			return $tpl . '/lib/fomantic/components/' . $component . '.min.css';
		case 'fomantic-js':
			return $tpl . '/lib/fomantic/components/' . $component . '.min.js';
		default:
			return '';
	}
}

/**
 * Add a preconnect hint when CDN-hosted assets are enabled.
 *
 * @since 2.6
 * @param array  $urls          Existing resource hints.
 * @param string $relation_type Hint type.
 * @return array
 */
function fanspaysite_cdn_resource_hints( $urls, $relation_type ) {
	if ( ! fanspaysite_use_cdn_libraries() || 'preconnect' !== $relation_type ) {
		return $urls;
	}

	$urls[] = array(
		'href'        => 'https://cdn.jsdelivr.net',
		'crossorigin' => 'anonymous',
	);

	return $urls;
}
add_filter( 'wp_resource_hints', 'fanspaysite_cdn_resource_hints', 10, 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() {
	$font_awesome_version = '7.1.0';
	$fomantic_version     = '2.9.4';

	// Font Awesome CSS.
	wp_enqueue_style( 'font-awesome', fanspaysite_get_library_asset_url( 'font-awesome-css' ), array(), $font_awesome_version );

	// 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,
			fanspaysite_get_library_asset_url( 'fomantic-css', $component ),
			array(),
			$fomantic_version
		);
	}

	// Fomantic UI - component JS (only dropdown + its dependency transition).
	wp_enqueue_script( 'fomantic-site', fanspaysite_get_library_asset_url( 'fomantic-js', 'site' ), array( 'jquery' ), $fomantic_version, true );
	wp_enqueue_script( 'fomantic-transition', fanspaysite_get_library_asset_url( 'fomantic-js', 'transition' ), array( 'jquery', 'fomantic-site' ), $fomantic_version, true );
	wp_enqueue_script( 'fomantic-dropdown', fanspaysite_get_library_asset_url( 'fomantic-js', 'dropdown' ), array( 'jquery', 'fomantic-transition' ), $fomantic_version, 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', get_template_directory_uri() . '/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', get_template_directory_uri() . '/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 = fanspaysite_get_library_asset_url( 'font-awesome-solid-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' );