<?php
/**
 * Larder functions and definitions.
 *
 * @package larder
 */

declare( strict_types = 1 );

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'larder_register_pattern_categories' ) ) {
	/**
	 * Register block pattern categories.
	 */
	function larder_register_pattern_categories(): void {
		register_block_pattern_category(
			'larder',
			array(
				'label' => esc_html__( 'Larder', 'larder' ),
			)
		);

		register_block_pattern_category(
			'larder-pages',
			array(
				'label' => esc_html__( 'Larder pages', 'larder' ),
			)
		);
	}
}
add_action( 'init', 'larder_register_pattern_categories' );

if ( ! function_exists( 'larder_theme_support' ) ) {
	/**
	 * Register theme supports for accessibility and HTML5.
	 */
	function larder_theme_support(): void {
		add_theme_support( 'html5', array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
			'style',
			'script',
			'navigation-widgets',
		) );
		add_theme_support( 'responsive-embeds' );
		add_theme_support( 'wp-block-styles' );
	}
}
add_action( 'after_setup_theme', 'larder_theme_support' );

if ( ! function_exists( 'larder_register_block_styles' ) ) {
	/**
	 * Register custom block styles.
	 */
	function larder_register_block_styles(): void {
		register_block_style(
			'core/group',
			array(
				'name'  => 'larder-card',
				'label' => esc_html__( 'Card', 'larder' ),
			)
		);

		register_block_style(
			'core/separator',
			array(
				'name'  => 'larder-ornament',
				'label' => esc_html__( 'Ornament', 'larder' ),
			)
		);

		register_block_style(
			'core/image',
			array(
				'name'  => 'larder-rounded',
				'label' => esc_html__( 'Rounded', 'larder' ),
			)
		);
	}
}
add_action( 'init', 'larder_register_block_styles' );

if ( ! function_exists( 'larder_editor_styles' ) ) {
	/**
	 * Enqueue editor styles to match front-end appearance.
	 */
	function larder_editor_styles(): void {
		add_theme_support( 'editor-styles' );
		add_editor_style( 'style.css' );
		add_editor_style( 'assets/css/custom.css' );
	}
}
add_action( 'after_setup_theme', 'larder_editor_styles' );

if ( ! function_exists( 'larder_enqueue_styles' ) ) {
	/**
	 * Enqueue front-end styles.
	 */
	function larder_enqueue_styles(): void {
		wp_enqueue_style(
			'larder-custom',
			get_theme_file_uri( 'assets/css/custom.css' ),
			array(),
			wp_get_theme()->get( 'Version' )
		);
	}
}
add_action( 'wp_enqueue_scripts', 'larder_enqueue_styles' );

if ( ! function_exists( 'larder_mobile_menu' ) ) {
	/**
	 * Enqueue mobile navigation and search overlay script.
	 */
	function larder_mobile_menu(): void {
		wp_enqueue_script(
			'larder-mobile-menu',
			get_theme_file_uri( 'assets/js/mobile-menu.js' ),
			array(),
			wp_get_theme()->get( 'Version' ),
			true
		);

		wp_localize_script(
			'larder-mobile-menu',
			'larderSearch',
			array(
				'label'       => esc_html__( 'Search', 'larder' ),
				'close'       => esc_html__( 'Close search', 'larder' ),
				'placeholder' => esc_html__( 'Type to search…', 'larder' ),
			)
		);
	}
}
add_action( 'wp_enqueue_scripts', 'larder_mobile_menu' );

if ( ! function_exists( 'larder_woocommerce_support' ) ) {
	/**
	 * Declare WooCommerce support and feature compatibility.
	 */
	function larder_woocommerce_support(): void {
		add_theme_support( 'woocommerce', array(
			'thumbnail_image_width' => 600,
			'single_image_width'    => 600,
			'gallery_thumbnail_image_width' => 100,
			'product_grid'          => array(
				'default_rows'    => 3,
				'min_rows'        => 1,
				'default_columns' => 3,
				'min_columns'     => 1,
				'max_columns'     => 4,
			),
		) );
		add_theme_support( 'wc-product-gallery-zoom' );
		add_theme_support( 'wc-product-gallery-slider' );
	}
}
add_action( 'after_setup_theme', 'larder_woocommerce_support' );

if ( ! function_exists( 'larder_gallery_thumb_nav' ) ) {
	/**
	 * Enqueue thumbnail scroll navigation script on single product pages.
	 */
	function larder_gallery_thumb_nav(): void {
		if ( ! function_exists( 'is_product' ) || ! is_product() ) {
			return;
		}

		wp_enqueue_script(
			'larder-gallery-thumb-nav',
			get_theme_file_uri( 'assets/js/gallery-thumb-nav.js' ),
			array(),
			wp_get_theme()->get( 'Version' ),
			true
		);

		wp_localize_script(
			'larder-gallery-thumb-nav',
			'larderThumbNav',
			array(
				'prev' => esc_html__( 'Previous thumbnails', 'larder' ),
				'next' => esc_html__( 'Next thumbnails', 'larder' ),
			)
		);
	}
}
add_action( 'wp_enqueue_scripts', 'larder_gallery_thumb_nav' );

if ( ! function_exists( 'larder_skip_link' ) ) {
	/**
	 * Add skip-to-content link for keyboard and screen reader users.
	 */
	function larder_skip_link(): void {
		printf(
			'<a class="skip-link" href="#wp--skip-link--target">%s</a>',
			esc_html__( 'Skip to content', 'larder' )
		);
	}
}
add_action( 'wp_body_open', 'larder_skip_link' );

if ( ! function_exists( 'larder_remove_wc_hook_output' ) ) {
	/**
	 * Remove WooCommerce's hook-based upsells and related products output.
	 * The block template handles these sections instead.
	 */
	function larder_remove_wc_hook_output(): void {
		remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
		remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
	}
}
add_action( 'wp', 'larder_remove_wc_hook_output' );
