<?php
/**
 * The Template for displaying sale tag for product
 *
 * @package YayPricing\Templates
 *
 * @param $min_percent_discount
 * @param $max_percent_discount
 * @param $has_image_gallery
 * @param $show_sale_off_amount
 * @param $product
 */

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

$percent_discounts = array_unique( array( $min_percent_discount, $max_percent_discount ) );

$show_up_to = count( $percent_discounts ) > 1;

$percent_discounts = array_filter(
	$percent_discounts,
	function( $value ) {
		return ! empty( $value );
	}
);

$sale_text = \YAYDP\Settings\YAYDP_Product_Pricing_Settings::get_instance()->get_sale_tag_text();
$background_color = \YAYDP\Settings\YAYDP_Product_Pricing_Settings::get_instance()->get_sale_tag_bg_color();
$text_color = \YAYDP\Settings\YAYDP_Product_Pricing_Settings::get_instance()->get_sale_tag_text_color();

$data_attributes = '';
if ( ! empty( $is_variable_product ) && ! empty( $variation_rules_data ) && is_product() ) {
	$data_attributes = ' data-variable-product="1" data-variation-rules="' . esc_attr( wp_json_encode( $variation_rules_data ) ) . '"';
	$initial_rule_names = array();
	foreach ( $matching_rules as $rule ) {
		$initial_rule_names[] = $rule->get_name();
	}
	$data_attributes .= ' data-initial-rules="' . esc_attr( wp_json_encode( $initial_rule_names ) ) . '"';
	$sale_tag_template = \YAYDP\Settings\YAYDP_Product_Pricing_Settings::get_instance()->get_sale_tag_text();
	$round_value_for_template = 0;
	if ( ! empty( $percent_discounts ) ) {
		\yaydp_sort_array( $percent_discounts );
		$max_discount = end( $percent_discounts );
		if ( round( $max_discount, 10 ) !== floor( $max_discount ) ) {
			$round_value_for_template = ceil( $max_discount );
		} else {
			$round_value_for_template = floor( $max_discount );
		}
	}
	$data_attributes .= ' data-sale-tag-template="' . esc_attr( $sale_tag_template ) . '"';
	$data_attributes .= ' data-discount-amount="' . esc_attr( $round_value_for_template ) . '"';
}
?>
<div class="yaydp-sale-tag<?php echo empty( $is_custom ) ? '' : ' yaydp-custom-sale-tag'; ?>"<?php echo $data_attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - $data_attributes is built with esc_attr() values ?> style="<?php echo esc_attr( $has_image_gallery ? 'right: 50px' : '' ); ?>;background-color:<?php echo esc_attr( $background_color ); ?>;border-color:<?php echo esc_attr( $background_color ); ?>;color:<?php echo esc_attr( $text_color ); ?>;">
	<?php
	$round_value = 0;
	if ( ! empty( $percent_discounts ) ) :
		\yaydp_sort_array( $percent_discounts );
		$max_discount = end( $percent_discounts );

		if ( round( $max_discount, 10 ) !== floor( $max_discount ) ) {
			$round_value = ceil( $max_discount );
		} else {
			$round_value = floor( $max_discount );
		}
	endif;
	if ( ! empty( $round_value ) ) {
		$sale_text = str_replace( '{amount}', "$round_value%", __( $sale_text, 'yaypricing' ) );
	}
	$rule_names = [];
	foreach ( $matching_rules as $rule ) {
		$rule_names[] = $rule->get_name();
	}
	$sale_text = str_replace( '{rule_name}', implode( ', ', $rule_names ), __( $sale_text, 'yaypricing' ) );
	echo esc_html( $sale_text );
	?>
</div>
<?php