<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

/**
 * Glance Live Search - Elementor widget.
 *
 * Registers the search bar as a native Elementor widget. Rendering
 * delegates to do_shortcode('[glance_search]') so behaviour stays
 * in sync with the shortcode and the Gutenberg block.
 *
 * Activation: automatic when Elementor is active.
 */
class GLSE_Elementor_Widget extends \Elementor\Widget_Base {

    public function get_name(): string {
        return 'glance-live-search';
    }

    public function get_title(): string {
        return __( 'Glance Live Search', 'glance-live-search' );
    }

    public function get_icon(): string {
        return 'eicon-search';
    }

    public function get_categories(): array {
        return [ 'general' ];
    }

    public function get_keywords(): array {
        return [ 'search', 'woocommerce', 'live', 'glance', 'ajax', 'product' ];
    }

    protected function register_controls(): void {
        $this->start_controls_section(
            'section_content',
            [
                'label' => __( 'Settings', 'glance-live-search' ),
                'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'placeholder',
            [
                'label'       => __( 'Placeholder', 'glance-live-search' ),
                'type'        => \Elementor\Controls_Manager::TEXT,
                'placeholder' => __( 'Search for products…', 'glance-live-search' ),
                'description' => __( 'Leave empty to use the default placeholder text.', 'glance-live-search' ),
                'dynamic'     => [ 'active' => false ],
            ]
        );

        $this->end_controls_section();
    }

    protected function render(): void {
        $placeholder = str_replace( [ '[', ']' ], '', (string) $this->get_settings_for_display( 'placeholder' ) );

        echo do_shortcode(
            '' !== $placeholder
                ? '[glance_search placeholder="' . esc_attr( $placeholder ) . '"]'
                : '[glance_search]'
        );
    }
}
