<?php
/**
 * Free sticky add-to-cart bar.
 *
 * @package DreamFast_CF
 *
 * @var \WC_Product $product
 * @var string      $image_url
 * @var string      $price_html
 * @var bool        $is_purchasable
 * @var string      $product_type
 */

namespace DreamFast\CommerceFloat;

defined( 'ABSPATH' ) || exit;

$dreamfast_cf_animation = sanitize_key( (string) Settings::get( 'cart_animation', 'slide' ) );
$dreamfast_cf_position  = sanitize_key( (string) Settings::get( 'cart_position', 'auto' ) );
if ( ! in_array( $dreamfast_cf_position, array( 'auto', 'top', 'bottom' ), true ) ) {
	$dreamfast_cf_position = 'auto';
}

$dreamfast_cf_button_icon = Settings::get_cart_button_icon_html();
$dreamfast_cf_icon_pos    = sanitize_key( (string) Settings::get( 'cart_button_icon_pos', 'before' ) );
if ( ! in_array( $dreamfast_cf_icon_pos, array( 'before', 'after' ), true ) ) {
	$dreamfast_cf_icon_pos = 'before';
}

$dreamfast_cf_theme_classes = Settings::get_cart_theme_classes();

$dreamfast_cf_template_args = isset( $args ) && is_array( $args ) ? $args : array();

/**
 * Filter the sticky cart bar primary call-to-action label.
 *
 * @since 1.1.0
 *
 * @param string               $text    Button label, defaults to the product add-to-cart text.
 * @param \WC_Product          $product Current product.
 * @param array<string, mixed> $args    Template data used to build the bar.
 */
$dreamfast_cf_button_text = apply_filters( 'dreamfast_cf_sticky_cart_button_text', $product->single_add_to_cart_text(), $product, $dreamfast_cf_template_args );
$dreamfast_cf_button_text = is_scalar( $dreamfast_cf_button_text ) ? (string) $dreamfast_cf_button_text : $product->single_add_to_cart_text();

/**
 * Filter the sticky cart bar label shown when the product needs option selection.
 *
 * @since 1.1.0
 *
 * @param string               $text    Button label.
 * @param \WC_Product          $product Current product.
 * @param array<string, mixed> $args    Template data used to build the bar.
 */
$dreamfast_cf_select_text = apply_filters( 'dreamfast_cf_sticky_cart_select_options_text', __( 'Select options', 'dreamfast-commerce-float' ), $product, $dreamfast_cf_template_args );
$dreamfast_cf_select_text = is_scalar( $dreamfast_cf_select_text ) ? (string) $dreamfast_cf_select_text : __( 'Select options', 'dreamfast-commerce-float' );

/**
 * Filter the sticky cart bar label shown when the product cannot be purchased.
 *
 * @since 1.1.0
 *
 * @param string               $text    Unavailable label.
 * @param \WC_Product          $product Current product.
 * @param array<string, mixed> $args    Template data used to build the bar.
 */
$dreamfast_cf_unavailable_text = apply_filters( 'dreamfast_cf_sticky_cart_unavailable_text', __( 'Unavailable', 'dreamfast-commerce-float' ), $product, $dreamfast_cf_template_args );
$dreamfast_cf_unavailable_text = is_scalar( $dreamfast_cf_unavailable_text ) ? (string) $dreamfast_cf_unavailable_text : __( 'Unavailable', 'dreamfast-commerce-float' );
?>
<div
	id="df-sf-sticky-cart"
	class="df-sf-sticky-cart df-sf-sticky-cart--<?php echo esc_attr( $dreamfast_cf_animation ); ?> df-sf-sticky-cart--<?php echo esc_attr( $dreamfast_cf_position ); ?> <?php echo esc_attr( $dreamfast_cf_theme_classes ); ?>"
	role="region"
	aria-label="<?php esc_attr_e( 'Quick purchase bar', 'dreamfast-commerce-float' ); ?>"
	aria-hidden="true"
>
	<div class="df-sf-sticky-cart__inner">
		<div class="df-sf-sticky-cart__product">
			<img
				class="df-sf-sticky-cart__thumb"
				src="<?php echo esc_url( $image_url ); ?>"
				alt="<?php echo esc_attr( $product->get_name() ); ?>"
				width="48"
				height="48"
				loading="lazy"
				decoding="async"
			/>
			<div class="df-sf-sticky-cart__meta">
				<span class="df-sf-sticky-cart__title"><?php echo esc_html( $product->get_name() ); ?></span>
				<span class="df-sf-sticky-cart__price"><?php echo wp_kses_post( $price_html ); ?></span>
			</div>
		</div>

		<?php if ( $is_purchasable && 'simple' === $product_type ) : ?>
			<button
				type="button"
				class="df-sf-sticky-cart__btn df-sf-sticky-cart__btn--icon-<?php echo esc_attr( $dreamfast_cf_icon_pos ); ?>"
				data-df-sf-action="add-to-cart"
			>
				<?php if ( '' !== $dreamfast_cf_button_icon && 'before' === $dreamfast_cf_icon_pos ) : ?>
					<span class="df-sf-sticky-cart__btn-icon" aria-hidden="true"><?php echo $dreamfast_cf_button_icon; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Trusted local SVG map. ?></span>
				<?php endif; ?>
				<?php echo esc_html( $dreamfast_cf_button_text ); ?>
				<?php if ( '' !== $dreamfast_cf_button_icon && 'after' === $dreamfast_cf_icon_pos ) : ?>
					<span class="df-sf-sticky-cart__btn-icon df-sf-sticky-cart__btn-icon--after" aria-hidden="true"><?php echo $dreamfast_cf_button_icon; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Trusted local SVG map. ?></span>
				<?php endif; ?>
			</button>
		<?php elseif ( $is_purchasable ) : ?>
			<button type="button" class="df-sf-sticky-cart__btn" data-df-sf-action="scroll-to-form">
				<?php if ( '' !== $dreamfast_cf_button_icon ) : ?>
					<span class="df-sf-sticky-cart__btn-icon" aria-hidden="true"><?php echo $dreamfast_cf_button_icon; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Trusted local SVG map. ?></span>
				<?php endif; ?>
				<?php echo esc_html( $dreamfast_cf_select_text ); ?>
			</button>
		<?php else : ?>
			<span class="df-sf-sticky-cart__unavailable"><?php echo esc_html( $dreamfast_cf_unavailable_text ); ?></span>
		<?php endif; ?>
	</div>
</div>
