<?php
declare(strict_types=1);

/**
 * Theme Support & Constants.
 *
 * @package WTE_Business_Card
 * @since   1.0.0
 */

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

// ── Constants ──────────────────────────────────────────────
define( 'WTE_THEME_VERSION', wp_get_theme()->get( 'Version' ) );
define( 'WTE_THEME_DIR', get_template_directory() );
define( 'WTE_THEME_URI', get_template_directory_uri() );
define( 'WTE_MIN_WP_VERSION', '6.5' );
define( 'WTE_MIN_PHP_VERSION', '8.1' );

// ── Theme Setup ────────────────────────────────────────────
function wte_theme_setup(): void {

	// Load text domain.
	load_theme_textdomain( 'wte_', WTE_THEME_DIR . '/languages' );

	// HTML5 support.
	add_theme_support( 'html5', [
		'search-form',
		'comment-form',
		'comment-list',
		'gallery',
		'caption',
		'style',
		'script',
		'navigation-widgets',
	] );

	// Title tag.
	add_theme_support( 'title-tag' );

	// Custom logo.
	add_theme_support( 'custom-logo', [
		'height'      => 120,
		'width'       => 400,
		'flex-height' => true,
		'flex-width'  => true,
	] );

	// Post thumbnails.
	add_theme_support( 'post-thumbnails' );

	// Custom background (minimal).
	add_theme_support( 'custom-background', [
		'default-color' => 'ffffff',
	] );

	// Register navigation menus.
	register_nav_menus( [
		'primary' => __( 'メインナビゲーション', 'wte_' ),
	] );

	// Content width.
	if ( ! isset( $GLOBALS['content_width'] ) ) {
		$GLOBALS['content_width'] = 1200;
	}
}
add_action( 'after_setup_theme', 'wte_theme_setup', 10 );

// ── Remove Unnecessary Head Elements ───────────────────────
function wte_cleanup_head(): void {
	// Remove WordPress version.
	remove_action( 'wp_head', 'wp_generator' );

	// Remove emoji scripts/styles.
	remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
	remove_action( 'wp_print_styles', 'print_emoji_styles' );
	remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
	remove_action( 'admin_print_styles', 'print_emoji_styles' );

	// Remove oEmbed discovery links.
	remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
	remove_action( 'wp_head', 'wp_oembed_add_host_js' );

	// Remove RSD/WLW links.
	remove_action( 'wp_head', 'rsd_link' );
	remove_action( 'wp_head', 'wlwmanifest_link' );

	// Remove shortlink.
	remove_action( 'wp_head', 'wp_shortlink_wp_head' );

	// Remove REST API link.
	remove_action( 'wp_head', 'rest_output_link_wp_head' );

	// Remove feed links (single page site).
	remove_action( 'wp_head', 'feed_links', 2 );
	remove_action( 'wp_head', 'feed_links_extra', 3 );
}
add_action( 'after_setup_theme', 'wte_cleanup_head', 20 );

// ── Disable Emoji DNS Prefetch ─────────────────────────────
function wte_disable_emoji_dns_prefetch( array $urls, string $relation_type ): array {
	if ( 'dns-prefetch' === $relation_type ) {
		$urls = array_filter( $urls, function ( $url ) {
			return ! str_contains( (string) $url, 'wp.org/images/core/emoji' );
		} );
	}
	return $urls;
}
add_filter( 'wp_resource_hints', 'wte_disable_emoji_dns_prefetch', 10, 2 );
