<?php
/**
 * WordPress Widget - Glance Live Search
 */

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

class GLSE_Widget extends WP_Widget {

    /**
     * Register the widget with WordPress.
     *
     * @since 1.4.0
     */
    public function __construct() {
        parent::__construct(
            'glse_search_widget',
            __( 'Glance Live Search', 'glance-live-search' ),
            [
                'description' => __( 'WooCommerce product search bar with AJAX live search.', 'glance-live-search' ),
                'classname'   => 'widget_glse_search',
            ]
        );
    }

    /**
     * Outputs the widget on the front end.
     *
     * @since 1.4.0
     */
    public function widget( $args, $instance ): void {
        $title       = ! empty( $instance['title'] ) ? $instance['title'] : '';
        $placeholder = ! empty( $instance['placeholder'] ) ? $instance['placeholder'] : '';

        echo wp_kses_post( $args['before_widget'] );

        if ( $title ) {
            echo wp_kses_post( $args['before_title'] ) . esc_html( $title ) . wp_kses_post( $args['after_title'] );
        }

        $placeholder_safe = str_replace( [ '[', ']' ], '', $placeholder );
        $sc_atts = $placeholder_safe ? ' placeholder="' . esc_attr( $placeholder_safe ) . '"' : '';
        echo do_shortcode( '[glance_search' . $sc_atts . ']' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Shortcode output is escaped in the template.

        echo wp_kses_post( $args['after_widget'] );
    }

    /**
     * Widget edit form in admin (Appearance → Widgets).
     *
     * @since 1.4.0
     */
    public function form( $instance ): void {
        $title       = $instance['title'] ?? '';
        $placeholder = $instance['placeholder'] ?? '';
        ?>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
                <?php esc_html_e( 'Title (optional)', 'glance-live-search' ); ?>
            </label>
            <input class="widefat"
                   id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
                   name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
                   type="text"
                   value="<?php echo esc_attr( $title ); ?>">
        </p>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'placeholder' ) ); ?>">
                <?php esc_html_e( 'Placeholder (optional)', 'glance-live-search' ); ?>
            </label>
            <input class="widefat"
                   id="<?php echo esc_attr( $this->get_field_id( 'placeholder' ) ); ?>"
                   name="<?php echo esc_attr( $this->get_field_name( 'placeholder' ) ); ?>"
                   type="text"
                   value="<?php echo esc_attr( $placeholder ); ?>"
                   placeholder="<?php esc_attr_e( 'Search for a product…', 'glance-live-search' ); ?>">
        </p>
        <?php
    }

    /**
     * Saves the widget settings.
     *
     * @since 1.4.0
     */
    public function update( $new_instance, $old_instance ): array {
        return [
            'title'       => sanitize_text_field( $new_instance['title'] ?? '' ),
            'placeholder' => sanitize_text_field( $new_instance['placeholder'] ?? '' ),
        ];
    }
}
