<?php 
/*
** Process Filters Result
*/
include_once( plugin_dir_path( __FILE__ ) . 'swg-ajax-filter-action.php' );

// Enqueue Ajax Filter Script
add_action( 'wp_enqueue_scripts', 'swg_filter_ajax_product_listing' );
function swg_filter_ajax_product_listing(){
	if( is_post_type_archive( 'product' ) || is_tax( 'product_cat' ) || is_tax( 'product_tag' ) || is_tax( 'product_brand' ) ){
		$loadmore_style = ( emarket_options()->getCpanelValue( 'product_loadmore_style' ) ) ? emarket_options()->getCpanelValue( 'product_loadmore_style' ) : 0;
		$is_category = is_tax( 'product_cat' ) ? 1 : 0;
		$is_brand = is_tax( 'product_brand' ) ? 1 : 0;
		
		$current_cat = is_tax( 'product_cat' ) ? get_queried_object()->slug : '';
		$current_brand = is_tax( 'product_brand' ) ? get_queried_object()->slug : '';
		wp_enqueue_script('swg-filter', get_template_directory_uri() . '/js/filter-ajax.js', array( 'jquery', 'wp-util' ), null, true);
		$swatches = ( function_exists( 'sw_wooswatches_construct' ) && get_option( 'sw_wooswatches_enable_listing' ) === 'yes' ) ? 1 : 0;
		wp_localize_script( 'swg-filter', 'swg_filter', ['loadmore_style' => $loadmore_style, 'is_category' => $is_category, 'is_brand' => $is_brand, 'current_cat' => $current_cat, 'current_brand' => $current_brand, 'swatches' => $swatches ] );
	}
}

add_action( 'woocommerce_after_main_content', 'swg_filter_ajax_customize_footer_script' );
function swg_filter_ajax_customize_footer_script(){
	?>
		<script type="text/template" id="swg-filter-ajax-loading">
			<div class="product-loader"></div>
		</script>
	<?php 
}

add_filter( 'wp_list_categories', 'swg_add_data_attributes_to_category_links', 10, 2 );
function swg_add_data_attributes_to_category_links( $output, $args ) {
    if ( isset( $args['taxonomy'] ) && $args['taxonomy'] === 'product_cat' ) {
        $output = preg_replace(
            '/<a href="([^"]+)"/',
            '<a href="$1" data-slug="$1"',
            $output
        );
		$tax = get_taxonomy( 'product_cat' );
		$slug = $tax->rewrite;
		$slug = $slug['slug'];
        $output = str_replace( get_site_url() . '/' . $slug, '', $output ); // Clean up URLs
    }
    return $output;
}

// Modify layered nav widget links
add_filter( 'woocommerce_layered_nav_term_html', 'swg_add_data_attributes_to_layered_nav', 10, 4 );
function swg_add_data_attributes_to_layered_nav( $term_html, $term, $link, $count ) {
    $taxonomy = $term->taxonomy;
    $slug = $term->slug;
    $term_html = preg_replace(
        '/<a href="([^"]+)"/',
        '<a href="$1" data-taxonomy="' . esc_attr( $taxonomy ) . '" data-slug="' . esc_attr( $slug ) . '"',
        $term_html
    );
    return $term_html;
}
/*
** Create Widget Clear Filter
*/
add_action( 'widgets_init', 'register_active_filters_widget' );
function register_active_filters_widget() {
    register_widget( 'SWG_Woo_Active_Filters_Widget' );
}

class SWG_Woo_Active_Filters_Widget extends WP_Widget {
    public function __construct() {
        parent::__construct(
            'woo_active_filters',
            __( 'SWG WooCommerce Active Filters', 'emarket' ),
            array( 'description' => __( 'Displays currently active WooCommerce filters.', 'emarket' ) )
        );
    }

    public function widget( $args, $instance ) {
        if ( ! is_shop() && ! is_product_category() ) {
            return; // Only show on shop or category pages
        }
		
		if( !function_exists( 'swg_filter_ajax_product_listing' ) ){
			return;
		}
		
        echo $args['before_widget'];
        if ( ! empty( $instance['title'] ) ) {
            echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
        }

        ?>
        <div class="swg-woo-active-filters">
            <ul class="active-filters-list"></ul>
            <button class="clear-all-filters"><?php esc_html_e( 'Clear All', 'emarket' ); ?></button>
        </div>
        <?php

        echo $args['after_widget'];
    }

    public function form( $instance ) {
        $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Active Filters', 'emarket' );
        ?>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'emarket' ); ?></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>
<?php
	}
}