<?php
/**
 * [findastra] shortcode — renders the search widget container that
 * src/frontend.js attaches to.
 *
 * @package FindAstra
 */

defined( 'ABSPATH' ) || exit;

final class FindAstra_Shortcode {

	public const TAG = 'findastra';

	public function register(): void {
		add_shortcode( self::TAG, array( $this, 'render' ) );
	}

	public function render( $atts ): string {
		$atts = shortcode_atts(
			array(
				'placeholder' => __( 'Search products…', 'findastra' ),
				'limit'       => 12,
			),
			is_array( $atts ) ? $atts : array(),
			self::TAG
		);

		$placeholder = sanitize_text_field( (string) $atts['placeholder'] );
		$limit       = max( 1, min( 50, (int) $atts['limit'] ) );

		ob_start();
		?>
		<div class="findastra-widget"
			data-findastra-widget
			data-limit="<?php echo esc_attr( (string) $limit ); ?>">
			<form class="findastra-widget__form" role="search" autocomplete="off">
				<label class="screen-reader-text" for="findastra-input"><?php echo esc_html( $placeholder ); ?></label>
				<input
					id="findastra-input"
					type="search"
					class="findastra-widget__input"
					placeholder="<?php echo esc_attr( $placeholder ); ?>"
					data-findastra-input
					autocomplete="off"
				/>
				<button type="submit" class="findastra-widget__submit">
					<?php esc_html_e( 'Search', 'findastra' ); ?>
				</button>
			</form>
			<div class="findastra-widget__status" data-findastra-status aria-live="polite"></div>
			<ul class="findastra-widget__results" data-findastra-results role="list"></ul>
		</div>
		<?php
		return (string) ob_get_clean();
	}
}
