<?php

/**
 * Class for all wcboost products compare template modification
 *
 * @version 1.0
 */

function remove_compare_button() {
	// Compare button.
	if( ! class_exists( 'WCBoost\ProductsCompare\Frontend' ) ) {
		return;
	}
	remove_action( 'woocommerce_after_add_to_cart_form', [ \WCBoost\ProductsCompare\Frontend::instance(), 'single_add_to_compare_button' ] );
	remove_action( 'woocommerce_after_shop_loop_item', [ \WCBoost\ProductsCompare\Frontend::instance(), 'loop_add_to_compare_button' ], 15 );
}

add_action( 'wp', 'remove_compare_button' );

/**
 * Update a single cart item.
 *
 * @since 1.0.0
 *
 * @return void
 */
function products_compare_button_icon( $svg, $icon ) {
	if( $icon == 'arrows' ) {
		$svg = '<svg viewBox="0 0 32 32">
		<path d="M26.55 18.5c-0.3-0.3-0.75-0.3-1.050 0s-0.3 0.75 0 1.050l4.6 4.8c0 0-0.050 0-0.050 0s0 0 0 0l-29.3 0.1c-0.4 0-0.75 0.35-0.75 0.75s0.35 0.75 0.75 0.75c0 0 0 0 0 0l29.25-0.1c0 0 0.050 0 0.050 0l-4.55 4.9c-0.3 0.3-0.25 0.8 0.050 1.050 0.15 0.15 0.35 0.2 0.5 0.2 0.2 0 0.4-0.1 0.55-0.25l4.95-5.3c0.65-0.8 0.65-2 0-2.75l-5-5.2z"></path>
		<path d="M5.4 13.7c0.15 0.15 0.35 0.25 0.55 0.25s0.35-0.050 0.5-0.2c0.3-0.3 0.3-0.75 0.050-1.050l-4.55-4.8c0 0 0.050 0 0.050 0s0 0 0 0l29.25-0.1c0.4 0 0.75-0.35 0.75-0.75s-0.35-0.75-0.75-0.75c0 0 0 0 0 0l-29.25 0.050c0 0 0 0-0.050 0l4.8-5.050c0.3-0.3 0.25-0.75-0.050-1.050s-0.75-0.25-1.050 0.050l-5.15 5.35v0.050c-0.65 0.8-0.65 2 0 2.75l4.9 5.25z"></path>
		</svg>';
	}

	return $svg;
}

add_filter( 'wcboost_products_compare_button_icon', 'products_compare_button_icon', 10, 2 );

/**
 * Ajaxify update count compare
 *
 * @since 1.0
 *
 * @param array $fragments
 *
 * @return array
 */
function products_compare_add_to_compare_fragments( $data ) {
	$data['.menu-item-compare .mini-cart-counter'] = '<span class="mini-cart-counter">'. \WCBoost\ProductsCompare\Plugin::instance()->list->count_items() . '</span>';

	return $data;
}

add_filter( 'wcboost_products_compare_add_to_compare_fragments', 'products_compare_add_to_compare_fragments', 10, 1 );