<?php
    /**
     * Live Search Section
     * 
     * @package Blog Bee
     * @since 1.0.0
     */
    namespace BBE\Customizer\Section;

    use BBE\Helpers as Helpers;
    use BBE\Customizer\Section\Section as Section;
    use BBE\Customizer\Theme_Defaults as Theme_Defaults;

    use function esc_html__;

    if( ! class_exists( __NAMESPACE__ . '\\Live_Search' ) ) {
        /**
         * Live_Search class
         */
        class Live_Search extends Section {
            /**
             * Section Id
             * 
             * @since 1.0.0
             * @var string
             */
            public static $id = 'bbe-live-search';

            /**
             * Register controls
             * 
             * @since 1.0.0
             */
            public function register_controls() {
                $this->tab = 'general';
                $this->add_section( 'live_search_section' );
                $this->add_control( 'live_search_section_tab' );
                $this->add_control( 'live_search_count' );
                $this->add_control( 'live_search_show_featured_image' );
                $this->tab = 'design';
                $this->add_control( 'live_search_typography' );
            }

            /**
             * Settings
             * 
             * @since 1.0.0
             */
            public function get_settings( $id = '' ) {
                $settings = [
                    'live_search_section_tab' =>  [
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'live_search_count' =>  [
                        'sanitize_callback' =>  'absint',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'live_search_show_featured_image' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_toggle' ],
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                    'live_search_typography' =>  [
                        'sanitize_callback' =>  [ $this, 'sanitize_typography' ],
                        'transport' =>  'postMessage',
                        'default'   =>  Theme_Defaults::get_defaults( $id )
                    ],
                ];
                return $id ? $settings[ $id ] : $settings;
            }

            /**
             * Controls
             * 
             * @since 1.0.0
             */
            public function get_controls( $id = '' ) {
                $controls = [
                    'live_search_section' =>  [
                        'title' =>  esc_html__( 'Live Search', 'blog-bee' ),
                        'panel' =>  'global_panel',
                        'priority'  =>  50
                    ],
                    'live_search_section_tab' =>  [
                        'type'  =>  'section-tab',
                        'fields'    =>  Helpers::get_customizer_section_tabs()
                    ],
                    'live_search_count' =>  [
                        'label' =>  esc_html__( 'Result Count', 'blog-bee' ),
                        'description' =>  esc_html__( 'Determines how many results to show.', 'blog-bee' ),
                        'type'  =>  'bbe-number',
                        'show_unit' =>  false,
                        'input_attrs'   =>  [
                            'min'   =>  2,
                            'max'   =>  10,
                            'step'  =>  1
                        ],
                    ],
                    'live_search_show_featured_image' =>  [
                        'label' =>  esc_html__( 'Show featured image', 'blog-bee' ),
                        'type'  =>  'toggle-button'
                    ],
                    'live_search_typography' =>  [
                        'label' =>  esc_html__( 'Title Typography', 'blog-bee' ),
                        'type'  =>  'typography'
                    ],
                ];
                return $id ? $controls[ $id ] : $controls;
            }

            /**
             * Defaults
             * 
             * @since 1.0.0
             */
            public function set_defaults() {
                Theme_Defaults::update_theme_defaults( [
                    'live_search_section_tab' =>  'general',
                    'live_search_count' =>  4,
                    'live_search_show_featured_image' =>  true,
                    'live_search_typography' =>  $this->get_typography([
                        'font_weight'   =>  '500',
                        'font_size'     =>  $this->get_responsive( '15px', '15px', '15px' )
                    ]),
                ] );
            }

            /**
             * Get dynamic css args
             * 
             * @since 1.0.0
             */
            public function get_dynamic_css_args(): array {
                $dynamic_args = [
                    'live_search_typography'    =>  [
                        'value' =>  Theme_Defaults::get_theme_option( 'live_search_typography' ),
                        'default' =>  Theme_Defaults::get_defaults( 'live_search_typography' ),
                        'selector'  =>  'body .bbe-header .bbe-live-search .bbe-article .entry-title, body .bbe-header .bbe-live-search .bbe-article .entry-title a'
                    ],
                ];
                return $dynamic_args;
            }

            /*
             * Render HTML
             * 
             * @since 1.0.0
             */
            public function render_html() {
                $block_class[] = self::$id;

                ?>
                    <div class="<?php echo esc_attr( implode( ' ', $block_class ) ); ?>">

                        <div class="article-wrap"></div>

                        <div class="ajax-wrapper">

                            <span class="no-results-label hidden"><?php echo esc_html__( 'No results found.', 'blog-bee' ); ?></span>

                            <button class="ajax-button" type="button">

                                <span class="label"><?php echo esc_html__( 'View All Results', 'blog-bee' ); ?></span>

                            </button>

                        </div>

                    </div><!-- .bbe-live-search -->

                <?php
            }
        }
        Live_Search::get_instance();
    }