<?php
/**
 * Template for cart section
 *
 * @package Brandy\Templates\Builder\Elements
 */

use Brandy\Customizer\Elements\Cart;
use Brandy\Utils\Helpers;

	$element    = $args['element'];
	$section_id = $element['id'];
	$settings   = $element['settings'];
	$attributes = array(
		'data-builder'    => $args['builder'],
		'data-section-id' => $section_id,
	);

	if ( $settings['auto_open_mini_cart'] ?? true ) {
		$attributes['data-auto-open-mini-cart'] = 'true';
	}
	global $brandy_cart_settings;
	$brandy_cart_settings = $settings;
	?>
<?php
if ( ! function_exists( 'brandy_render_mini_cart_notice' ) ) {
	function brandy_render_mini_cart_notice() {
		?>
		<div class="brandy-mini-cart-wrapper">
			<div class="brandy-mini-cart-top">
				<div class="brandy-mini-cart__title">
					<h2 class="brandy-mini-cart__title__text"><?php esc_html_e( 'Your cart', 'brandy' ); ?></h2>
					<button class="brandy-mini-cart__close" type="button" tabindex="0" title="Close mini cart"><span class="sr-only">Close panel</span><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true" class="h-6 w-6"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"></path></svg></button>
				</div>
				<div class="brandy-mini-cart__notice">
					<div>
					<svg width="70" height="70" viewBox="0 0 70 70" fill="none" xmlns="http://www.w3.org/2000/svg">
					<rect width="70" height="70" rx="16" fill="#8337FF"/>
					<path d="M54.9618 37.7016C53.4579 37.7016 52.4553 36.5768 52.4553 35.01C52.4553 33.4433 53.4779 32.2984 54.9618 32.2984C56.4456 32.2984 57.5084 33.4433 57.5084 35.01C57.5084 36.5768 56.4657 37.7016 54.9618 37.7016ZM54.9618 42C58.9521 42 62 39.0273 62 35.01C62 30.9928 58.9521 28 54.9618 28C50.9714 28 47.9436 30.9727 47.9436 35.01C47.9436 39.0473 50.9915 42 54.9618 42ZM39.9228 37.7016C38.4189 37.7016 37.3962 36.5768 37.3962 35.01C37.3962 33.4433 38.4189 32.2984 39.9228 32.2984C41.4267 32.2984 42.4493 33.4433 42.4493 35.01C42.4493 36.5768 41.4467 37.7016 39.9228 37.7016ZM39.9228 42C43.8931 42 46.941 39.0273 46.961 35.01C46.961 30.9727 43.8931 28 39.9228 28C35.9525 28 32.8845 30.9727 32.8845 35.01C32.8845 39.0473 35.9525 42 39.9228 42ZM14.8979 42C16.4619 42 17.7453 41.2367 18.6877 39.429L20.8132 35.4519V38.8264C20.8132 40.8149 22.0965 42 24.0817 42C25.6457 42 26.7887 41.3171 27.8916 39.429L32.7843 31.1736C33.847 29.3458 33.085 28 30.739 28C29.4757 28 28.6535 28.4017 27.9116 29.7877L24.5429 36.1148V30.4907C24.5429 28.8034 23.7408 28 22.277 28C21.0939 28 20.1716 28.5022 19.4497 29.9082L16.2815 36.1148V30.5509C16.2815 28.7432 15.5395 28 13.7549 28H10.0854C8.70182 28 8 28.6428 8 29.8278C8 31.0129 8.74192 31.6958 10.0854 31.6958H11.5893V38.8063C11.5893 40.8149 12.9328 42 14.8979 42Z" fill="white"/>
					</svg>
					</div>
					<h3><?php esc_html_e( 'WooCommerce plugin required', 'brandy' ); ?></h3>
					<p><?php printf( esc_html__( 'Please install %s plugin to use this feature', 'brandy' ), '<strong>WooCommerce</strong>' ); ?></p>
					<a class="wp-block-button__link" href="<?php echo esc_url( admin_url( 'plugin-install.php?s=woocommerce&tab=search&type=term' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Go to Plugins', 'brandy' ); ?></a>
				</div>
			</div>
		</div>
		<?php
	}
}
?>
<div class="brandy-cart-element <?php echo esc_attr( brandy_get_editable_class() ); ?>" <?php brandy_print_dom_attributes( $attributes ); ?>>
	<div class="brandy-element-wrapper">
		<?php
		$href = '#';
		if ( 'cart_page' == $settings['click_effect'] ) {
			$href = '/cart';
		}
		$items_count = 0;
		if ( \is_wc_installed() ) {
			$items_count = \WC()->cart->get_cart_contents_count();
		}
		?>
		<div class="brandy-cart-wrapper">
			<a href="<?php echo esc_url( $href ); ?>" data-click_effect="<?php echo esc_attr( $settings['click_effect'] ); ?>" class="brandy-cart-a trigger-tooltip trigger-icon" aria-label="View cart">
				<span class="brandy-cart-icon-wrap">
					<?php
						$icon = Cart::get_icon( $settings['icon'], $settings['icon_style'] );
						brandy_render_icon( $icon );
					?>
					<?php
						brandy_render_badge( $items_count, '', 'brandy-cart-qtybadge' );
					?>
					
				</span>
				<?php
				$cart_label_attributes = array(
					'class' => 'brandy-cart-label',
				);
				$display               = $settings['label']['display'] ?? array(
					'desktop' => false,
					'tablet'  => true,
					'mobile'  => null,
				);
				if ( ! Helpers::get_device_value( $display, 'desktop' ) ) {
					$cart_label_attributes['class'] .= ' is-tooltip';
				}
				if ( ! Helpers::get_device_value( $display, 'tablet' ) ) {
					$cart_label_attributes['class'] .= ' hidden-md';
				}
				if ( ! Helpers::get_device_value( $display, 'mobile' ) ) {
					$cart_label_attributes['class'] .= ' hidden-sm';
				}
				?>
				<span <?php brandy_print_dom_attributes( $cart_label_attributes ); ?>><?php echo esc_html( $settings['cart_label'] ?? 'Cart' ); ?></span>
			</a>
			<?php if ( 'dropdown' == $settings['click_effect'] ) : ?>
				<div class="brandy-cart-dropdown brandy-mini-cart" type=<?php echo esc_attr( $settings['click_effect'] ); ?>>
					<?php
					if ( ! \is_wc_installed() ) {
						brandy_render_mini_cart_notice();
					}
					if ( function_exists( 'woocommerce_mini_cart' ) ) {
						\woocommerce_mini_cart();
					}
					?>
				</div>
			<?php endif; ?>
		</div>
	</div>
	<?php
	if ( is_customize_preview() ) {
		get_template_part(
			'template-parts/common/edit-section-button',
			'',
			array(
				'part_id' => $section_id,
			)
		);
	}
	?>
	
	<?php
	// Store cart settings globally for footer rendering (only once to avoid duplicates)
	if ( 'slide_in' == $settings['click_effect'] ) {
		global $brandy_cart_drawer_settings;
		// Only add if not already set to prevent duplicates
		if ( ! isset( $brandy_cart_drawer_settings ) ) {
			$brandy_cart_drawer_settings = array(
				'open_direction' => $settings['open_direction'] ?? 'right',
				'settings'        => $settings,
			);
		}
	}
	?>
</div>
