<?php
/**
 * Compare Icon Widget - Render Methods
 *
 * @package GGM_Commerce
 */

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

/**
 * Render the premium widget content.
 *
 * @param GGM_Commerce_Widget_Compare_Icon $widget The widget instance.
 */
function ggmc_compare_icon_render_premium_content( $widget ) {
	$settings = $widget->get_settings_for_display();

	// Get comparison count.
	$comparison = GGM_Commerce_Product_Comparison::instance();
	$count      = $comparison->get_count();

	// Get comparison page URL.
	$compare_url = '';
	if ( 'yes' === $settings['link_to_compare'] ) {
		if ( ! empty( $settings['compare_page']['url'] ) ) {
			$compare_url = $settings['compare_page']['url'];
		} else {
			$compare_url = $comparison->get_comparison_page_url();
		}
	}

	$tag        = 'div';
	$link_attrs = '';

	if ( ! empty( $compare_url ) ) {
		$tag        = 'a';
		$link_attrs = ' href="' . esc_url( $compare_url ) . '"';

		if ( ! empty( $settings['compare_page']['is_external'] ) ) {
			$link_attrs .= ' target="_blank"';
		}
		if ( ! empty( $settings['compare_page']['nofollow'] ) ) {
			$link_attrs .= ' rel="nofollow"';
		}
	}

	?>
	<<?php echo esc_html( $tag ); ?> class="ggmc-compare-icon" data-compare-icon<?php echo $link_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
		<?php ggmc_render_icon( $settings['icon'], array( 'aria-hidden' => 'true' ) ); ?>
		<?php if ( 'yes' === $settings['show_count'] ) : ?>
			<span class="ggmc-compare-count-badge" <?php echo 0 === $count ? 'style="display:none;"' : ''; ?>>
				<?php echo esc_html( $count ); ?>
			</span>
		<?php endif; ?>
	</<?php echo esc_html( $tag ); ?>>
	<?php
}
