<?php
/**
 * Template for displaying search forms
 *
 * @link https://developer.wordpress.org/reference/functions/wp_unique_id/
 * @link https://developer.wordpress.org/reference/functions/get_search_form/
 *
 * @package errigal
 * @since 1.0.0
 *
 * @param array $args {
 *     Optional. Associative array of arguments for search form.
 *
 *     @type string $aria_label Optional. ARIA label for search form. Useful to distinguish
 *                              multiple search forms on the same page and improve accessibility.
 * }
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Accessibility_Class as Accessibility_Class,
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
	Errigal_Shared_Class as Shared_Class,
};
use Errigal\Icon\Errigal_Icon as Icon;
use Errigal\Search\Errigal_Search_Form as Search_Form;
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Site_Info as Site_Info,
	Errigal_Utility as Utility,
	Errigal_Validator as Validator,
};

$args = $args ?? array();
$args = wp_parse_args(
	$args,
	array(
		Search_Form::ARIA_LABEL => '',
	)
);

$search_form_aria_label = $args[ Search_Form::ARIA_LABEL ];

Html_Builder::create()->create_form(
	array(
		Html_Option::ATTRS => Utility::array_merge_deep(
			array(
				Html_Attr::CLASS_NAME => 'search-form',
				Html_Attr::ACTION     => Site_Info::get_home_url(),
				Html_Attr::METHOD     => Html_Attr::VALUE_GET,
				Html_Attr::ROLE       => 'search',
			),
			Validator::is_non_empty_string( $search_form_aria_label )
				? array(
					Html_Attr::ARIA_LABEL => $search_form_aria_label,
				)
				: array()
		),
	),
)->render_element(
	function ( $html_builder ) {
		if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
			return;
		}

		$search_form_id = wp_unique_id( 'search-form-' );

		$html_builder->create_label(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::FOR        => $search_form_id,
					Html_Attr::CLASS_NAME => Accessibility_Class::SCREEN_READER_TEXT,
				),
			)
		)->render_element(
			Search_Form::get_label()
		);
		$html_builder->create_div(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => 'search-field-container',
				),
			)
		)->render_element(
			function ( $html_builder ) use ( $search_form_id ) {
				if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
					return;
				}

				$search_query = get_search_query();

				$html_builder->create_input(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::TYPE        => 'text',
							Html_Attr::ID          => $search_form_id,
							Html_Attr::NAME        => 's',
							Html_Attr::CLASS_NAME  => 'search-field',
							Html_Attr::REQUIRED    => '',
							Html_Attr::VALUE       => $search_query,
							Html_Attr::PLACEHOLDER => __( 'Search&hellip;', 'errigal' ),
						),
					)
				)->render_element();
				$html_builder->create_button(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::CLASS_NAME => array(
								'search-clear-button',
								Validator::is_non_empty_string( $search_query )
									? Shared_Class::SHOW
									: null,
							),
							Html_Attr::TYPE       => Html_Attr::VALUE_BUTTON,
							Html_Attr::ARIA_LABEL => __( 'Clear keyword', 'errigal' ),
						),
					)
				)->render_element(
					Icon::get_icon( Icon::CROSS_CIRCLE_FILL, Icon::SIZE_SM )
				);
			}
		);
		$html_builder->create_button(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => array(
						Shared_Class::ICON_BUTTON,
						'search-submit',
					),
					Html_Attr::TYPE       => Html_Attr::VALUE_SUBMIT,
					Html_Attr::ARIA_LABEL => Search_Form::get_label(),
				),
			)
		)->render_element(
			Icon::get_icon( Icon::SEARCH, Icon::SIZE_MD )
		);
	}
);
