<?php
/**
 * Search module — loads search.js only when AJAX search is enabled.
 *
 * @package Kutby
 * @since   2.5.0
 */

declare(strict_types=1);

namespace Kutby\Modules;

use Kutby\Assets\AssetManifestManager;
use Kutby\Contracts\ModuleInterface;
use Kutby\Support\Environment;
use Kutby\ThemeOptions\Manager;

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

/**
 * Class SearchModule
 */
final class SearchModule implements ModuleInterface {

	/**
	 * {@inheritdoc}
	 */
	public function register(): void {
		add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue' ), 20 );
	}

	/**
	 * {@inheritdoc}
	 */
	public function boot(): void {}

	/**
	 * Conditionally enqueue search script.
	 *
	 * @return void
	 */
	public function maybe_enqueue(): void {
		if ( Environment::is_dev() ) {
			return;
		}

		if ( ! Manager::get( 'search.ajax', false ) ) {
			return;
		}

		// Prefer dedicated file when not bundled; foundation stub.
		wp_enqueue_script(
			'kutby-search',
			KUTBY_ASSETS_URI . '/js/search.js',
			array( 'kutby-theme' ),
			KUTBY_VERSION,
			array(
				'strategy'  => 'defer',
				'in_footer' => true,
			)
		);
	}
}
