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

use StoreEngine\Utils\Helper;
use StoreEngine\Utils\Template;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

global $product;

$cart_item             = storeengine_cart()->get_cart_item_by_product( get_the_ID() );
$product_count_in_cart = $cart_item ? $cart_item->quantity : 0;
$prices                = $product ? $product->get_prices() : [];
$num_prices            = count( $prices );

if ( empty( $prices ) || ! $num_prices ) {
	return;
}
?>

<?php if ( 'variable' === $product->get_type() ) : ?>
	<a href="<?php echo esc_url( get_the_permalink( $product->get_id() ) ); ?>" class="storeengine-btn storeengine-btn--preset-blue storeengine-btn--view-options"><?php esc_html_e( 'View Options', 'storeengine' ); ?></a>
<?php else : ?>
	<form class="storeengine-ajax-add-to-cart-form storeengine-ajax-add-to-cart-form--loop" action="#" method="post">
		<?php
		if ( 1 < $num_prices && 'price-range' === Helper::get_settings( 'product_archive_multi_price_display', 'dropdown' ) ) {
			usort( $prices, fn ( $a, $b ) => $b->get_price() - $a->get_price() );
			$max = reset( $prices );
			$min = end( $prices );
			?>
			<div class="storeengine-ajax__amount">
				<div class="storeengine-product__simple-price">
					<?php echo wp_kses_post( \StoreEngine\Utils\Formatting::format_price_range( $max->get_price(), $min->get_price() ) ); ?>
				</div>
			</div>
			<div class="storeengine-add-to-cart-buttons">
				<a href="<?php echo esc_url( get_the_permalink( $product->get_id() ) ); ?>" class="storeengine-btn storeengine-btn--preset-blue storeengine-btn--view-options"><?php esc_html_e( 'View Options', 'storeengine' ); ?></a>
			</div>
			<?php
		} else { ?>
			<div class="storeengine-ajax__amount">
				<?php
				if ( 1 === $num_prices ) {
					Template::get_template( 'loop/price.php', [
						'price' => current( $prices ),
					] );
				} else {
					Template::get_template( 'loop/prices.php', [
						'prices' => $prices,
					] );
				}
				?>
			</div>
			<div class="storeengine-add-to-cart-buttons">
				<?php wp_nonce_field( 'storeengine_add_to_cart', 'storeengine_nonce' ); ?>
				<input type="hidden" name="product_id" value="<?php the_ID(); ?>">
				<?php if ( Helper::get_settings( 'enable_direct_checkout' ) || 1 === $num_prices && current( $prices )->is_type( ['subscription', 'installment' ] ) ) : ?>
					<button class="storeengine-btn storeengine-btn--preset-blue storeengine-btn--direct-checkout" type="submit"
							data-action="buy_now"><?php esc_html_e( 'Buy Now', 'storeengine' ); ?></button>
				<?php else : ?>
					<button class="storeengine-btn storeengine-btn--preset-blue storeengine-btn--add-to-cart" type="submit"
							data-action="add_to_cart">
						<?php
						if ( $product_count_in_cart > 0 ) {
							// translators: %d. Item quantity for a product in the cart.
							printf( esc_html__( '%d in cart', 'storeengine' ), esc_html( $product_count_in_cart ) );
						} else {
							esc_html_e( 'Add to Cart', 'storeengine' );
						}
						?>
					</button>
				<?php endif; ?>
			</div>
		<?php } ?>
	</form>
<?php endif; ?>

