<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
/**
 * Dynamic render for bw/search — the "Brentwood Search" block.
 *
 * A prominent, always-open site-search bar that mirrors brentwood.ca/search.
 * It deliberately REUSES the site's existing search mechanism rather than
 * inventing a new one:
 *
 *   • The form is a plain GET form to home_url() with an <input name="s">, so
 *     pressing Enter / clicking Search submits to the standard WordPress search
 *     results page — rendered by the child theme's search.php template
 *     (two-column facets + rich result rows, see inc/brentwood-search.php).
 *   • Live "as you type" results reuse the same REST search + client rendering
 *     as the header [bw_search_icon] dropdown (assets/bw-search.js), which is
 *     enqueued here as the block's viewScript. The markup carries
 *     data-bw-inline so that shared script runs in always-open inline mode.
 *
 * Icons come from bw_search_icon_svg() (inc/brentwood-search.php); styling is
 * shared with the header dropdown (assets/bw-search.css) plus a thin
 * block-specific layer (blocks/search/style.css).
 *
 * @var array $attributes
 */

$placeholder = isset( $attributes['placeholder'] ) ? (string) $attributes['placeholder'] : '';
if ( '' === trim( $placeholder ) ) {
	$placeholder = __( 'Search Brentwood…', 'kadence-child' );
}

$button = isset( $attributes['buttonText'] ) ? (string) $attributes['buttonText'] : '';
if ( '' === trim( $button ) ) {
	$button = __( 'Search', 'kadence-child' );
}

$show_button = ! isset( $attributes['showButton'] ) || ! empty( $attributes['showButton'] );
$live        = ! isset( $attributes['liveResults'] ) || ! empty( $attributes['liveResults'] );
$autofocus   = ! empty( $attributes['autofocus'] );

// Prefill the box with the current query on the results page itself, so the
// block doubles as the "refine your search" bar when dropped onto search.php.
$current = get_search_query();

$action = esc_url( home_url( '/' ) );
$rest   = esc_url( rest_url( 'wp/v2/search' ) );
$uid    = wp_unique_id( 'bw-search-' );

// data-bw-search + data-bw-inline hand the widget to assets/bw-search.js in
// always-open inline mode (live results below the bar). When live results are
// turned off, the attribute is omitted and this stays a plain GET form.
$data_attrs = $live
	? ' data-bw-search data-bw-inline data-rest="' . $rest . '"'
	: '';

$wrapper = get_block_wrapper_attributes(
	array(
		'class' => 'bw-search bw-search--inline' . ( $live ? '' : ' bw-search--static' ),
	)
);

ob_start();
?>
<div <?php echo $wrapper; // phpcs:ignore WordPress.Security.EscapeOutput ?><?php echo $data_attrs; // phpcs:ignore WordPress.Security.EscapeOutput -- static attrs, url escaped above ?>>
	<form class="bw-search__bar" role="search" method="get" action="<?php echo $action; // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above ?>">
		<label class="screen-reader-text" for="<?php echo esc_attr( $uid ); ?>"><?php echo esc_html( $button ); ?></label>
		<div class="bw-search__field">
			<span class="bw-search__lead" aria-hidden="true"><?php echo bw_search_icon_svg( 'search' ); // phpcs:ignore WordPress.Security.EscapeOutput -- static SVG ?></span>
			<input
				type="search"
				id="<?php echo esc_attr( $uid ); ?>"
				name="s"
				class="bw-search__input"
				value="<?php echo esc_attr( $current ); ?>"
				placeholder="<?php echo esc_attr( $placeholder ); ?>"
				autocomplete="off"
				aria-label="<?php echo esc_attr( $button ); ?>"
				<?php echo $autofocus ? 'autofocus' : ''; // phpcs:ignore WordPress.Security.EscapeOutput ?>
			/>
		</div>
		<?php if ( $live ) : ?>
			<button type="button" class="bw-search__clear" aria-label="<?php esc_attr_e( 'Clear search', 'kadence-child' ); ?>">
				<?php echo bw_search_icon_svg( 'times-circle' ); // phpcs:ignore WordPress.Security.EscapeOutput -- static SVG ?>
			</button>
		<?php endif; ?>
		<?php if ( $show_button ) : ?>
			<button type="submit" class="bw-search__submit"><?php echo esc_html( $button ); ?></button>
		<?php endif; ?>
	</form>
	<?php if ( $live ) : ?>
		<div class="bw-search__results" role="listbox" aria-live="polite"></div>
	<?php endif; ?>
</div>
<?php
echo ob_get_clean(); // phpcs:ignore WordPress.Security.EscapeOutput
