/**
 * Public JavaScript for WebEquipe PDF Search
 *
 * @package WebEquipe\PDFSearch
 * @since 1.0.0
 */

(function ($) {
	'use strict';

	/**
	 * Initialize public functionality
	 */
	function init() {
		// Add PDF result styling
		markPdfResults();

		// Optional: Live search functionality
		// This can be enabled via a setting if needed
		// initLiveSearch();
	}

	/**
	 * Mark PDF results in search results
	 */
	function markPdfResults() {
		// This is handled server-side, but we can add client-side enhancements
		$( '.webequipe-pdf-result' ).each(
			function () {
				var $result = $( this );

				// Add click handler to make entire result clickable
				$result.on(
					'click',
					function (e) {
						var $link = $result.find( 'a' ).first();
						if ($link.length && ! $( e.target ).is( 'a' )) {
							window.location.href = $link.attr( 'href' );
						}
					}
				);
			}
		);
	}

	/**
	 * Initialize live search (optional feature)
	 */
	function initLiveSearch() {
		var $searchInput = $( '.webequipe-pdf-search-input' );
		var searchTimeout;

		$searchInput.on(
			'input',
			function () {
				clearTimeout( searchTimeout );
				var query = $( this ).val();

				if (query.length < 3) {
					return;
				}

				searchTimeout = setTimeout(
					function () {
						// Perform AJAX search
						// This would require additional backend implementation
					},
					300
				);
			}
		);
	}

	// Initialize on document ready
	$( document ).ready(
		function () {
			init();
		}
	);

})( jQuery );
