<?php

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

use Prodigy\Includes\Content\Prodigy_Brands_Parser;
use Prodigy\Includes\Content\Prodigy_Request_Maker;
use Prodigy\Includes\Frontend\Cart_Error_Handler;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Active_Filter_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Brands_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Breadcrumbs_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Cart_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Cart_Page_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Categories_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Categories_Filter_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Category_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Filter_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Main_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_My_Account_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Price_Filter_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Add_To_Cart_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Attributes_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Brands_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Breadcrumbs_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Image_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Price_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Rating_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Review_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Short_Description_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Sku_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Subscriptions_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Tags_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Thumbnails_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Tiered_Prices_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Title_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Product_Variants_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Products_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Related_Products_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Search_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Shop_Mobile_Sortable_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Shop_Products_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Shop_Sortable_Data_Mapper;
use Prodigy\Includes\Frontend\Mappers\Prodigy_Thank_Page_Data_Mapper;
use Prodigy\Includes\Frontend\Prodigy_Product_Template_Builder;
use Prodigy\Includes\Frontend\Prodigy_Product_Template_Item;
use Prodigy\Includes\Frontend\Prodigy_Public;
use Prodigy\Includes\Helpers\Prodigy_Cookies;
use Prodigy\includes\helpers\Prodigy_Elementor;
use Prodigy\Includes\Helpers\Prodigy_Formatting;
use Prodigy\Includes\Helpers\Prodigy_Platform_Settings;
use Prodigy\Includes\Helpers\Prodigy_Template;
use Prodigy\Includes\Helpers\Prodigy_Url_Helper;
use Prodigy\Includes\Helpers\Prodigy_Widget_Options;
use Prodigy\Includes\Models\Prodigy_Brands;
use Prodigy\includes\models\Prodigy_Order;
use Prodigy\Includes\Models\Prodigy_Products;
use Prodigy\Includes\Prodigy;
use Prodigy\Includes\Prodigy_Cart;
use Prodigy\Includes\Prodigy_Pagination;
use Prodigy\Includes\Prodigy_Product_Comments;
use Prodigy\Includes\Prodigy_User;
use Prodigy\Includes\Support\Addons\Elementor\Widgets\Product\Prodigy_Tabs;
use Prodigy\Includes\Support\Customizer\Prodigy_Customizer;

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


// deactivate new block editor
function phi_theme_support() {
	remove_theme_support( 'widgets-block-editor' );
}

add_action( 'after_setup_theme', 'phi_theme_support' );


if ( ! function_exists( 'getallheaders' ) ) {
	/**
	 * Set headers
	 *
	 * @return array
	 */
	function getallheaders(): array {
		$headers = array();
		foreach ( $_SERVER as $name => $value ) {
			if ( substr( $name, 0, 5 ) === 'HTTP_' ) {
				$headers[ str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) ) ] = $value;
			}
		}

		return $headers;
	}
}

/**
 * Global
 */
if ( ! function_exists( 'prodigy_output_content_wrapper' ) ) {
	/**
	 * Output the start of the page wrapper.
	 */
	function prodigy_output_content_wrapper() {
		Prodigy_Template::prodigy_get_template( 'global/wrapper-start.php' );
	}
}

if ( ! function_exists( 'prodigy_output_content_wrapper_end' ) ) {
	function prodigy_output_content_wrapper_end() {
		Prodigy_Template::prodigy_get_template( 'global/wrapper-end.php' );
	}
}


if ( ! function_exists( 'prodigy_product_get_captcha' ) ) {
	function prodigy_product_get_captcha() {
		add_action(
			'wp_enqueue_scripts',
			function () {
				wp_enqueue_script( 'google-recaptcha', 'https://www.google.com/recaptcha/api.js?&render=explicit', array(), null, true );
			}
		);
	}
}


/**
 * Template shortcode search
 */
if ( ! function_exists( 'prodigy_get_template_shortcode_search' ) ) {
	function prodigy_get_template_shortcode_search( $settings ) {
		$mapper = new Prodigy_Search_Data_Mapper();
		$params = $mapper->get_default_parameters( $settings );
		Prodigy_Template::prodigy_get_template( 'shortcode/search.php', $params );
	}
}

if ( ! function_exists( 'prodigy_get_template_shortcode_thank_you' ) ) {
	/**
	 * @throws Exception
	 */
	function prodigy_get_template_shortcode_thank_you( $data ) {
		$data['order_token']     = isset( $_GET['order_token'] ) ? sanitize_text_field( wp_unslash( $_GET['order_token'] ) ) : false;
		$is_redemption_store     = isset( $_GET['is_redemption_store'] ) ? sanitize_key( wp_unslash( $_GET['is_redemption_store'] ) ) : '';
		$data['approval_needed'] = isset( $_GET['approval-needed'] ) ? sanitize_key( wp_unslash( $_GET['approval-needed'] ) ) : '';
		$data['is_elementor ']   = ( ! isset( $_GET['action'] ) || $_GET['action'] !== 'elementor' );
		$mapper                  = new Prodigy_Thank_Page_Data_Mapper();
		$params                  = $mapper->get_default_parameters( $data );

		wp_enqueue_script(
			'feature-products',
			plugin_dir_url( PRODIGY_PLUGIN_PATH . '/includes/frontend/shortcodes/js/feature-products.js' ) . 'feature-products.js',
			array( 'jquery' ),
			PRODIGY_VERSION
		);

		Prodigy_Template::prodigy_get_template( 'shortcode/thank-you.php', $params );

		if ( $is_redemption_store ) {
			$order_model = new Prodigy_Order();
			$order_model->update_status_orders(
				$data['order_token'],
				$order_model::PRODIGY_ORDER_COMPLETED
			);

			$user        = new Prodigy_User();
			$refresh_key = Prodigy_Cookies::get_cookie( Prodigy_User::REFRESH_COOKIE_NAME );
			$user->remove_impersonation_user( $refresh_key );
			$user->hosted_system_logout_process( $refresh_key );
			$user->logout();
			$user->delete_user_cookie();
			$user->clear_external_checkout_cookie();
		}
	}
}

if ( ! function_exists( 'prodigy_get_template_product_subscriptions' ) ) {
	function prodigy_get_template_product_subscriptions( $data ) {
		$mapper     = new Prodigy_Product_Subscriptions_Data_Mapper();
		$parameters = $mapper->get_default_parameters( $data );
		Prodigy_Template::prodigy_get_template( 'single-product/subscriptions.php', $parameters );
	}
}

if ( ! function_exists( 'prodigy_get_template_shortcode_price_filter' ) ) {
	function prodigy_get_template_shortcode_price_filter( $data ) {
		$mapper       = new Prodigy_Price_Filter_Data_Mapper();
		$params       = $mapper->get_default_parameters( $data );
		$cache_prefix = defined( 'WP_ROCKET_VERSION' ) ? '?ver=' . PRODIGY_VERSION . '&cb=' . time() : '';

		wp_enqueue_script(
			'ion.rangeSlider.min',
			Prodigy::plugin_url() . '/web/templates/js/ion.rangeSlider.min.js' . $cache_prefix,
			array( 'jquery' ),
			PRODIGY_VERSION,
			false
		);

		wp_enqueue_script(
			'price-filter',
			plugin_dir_url( PRODIGY_PLUGIN_PATH . '/includes/frontend/shortcodes/js/price-filter.js' ) . 'price-filter.js' . $cache_prefix,
			array(
				'jquery',
				'public',
			),
			PRODIGY_VERSION,
			false
		);
		wp_localize_script( 'price-filter', Prodigy_Public::PRICE_FILTER_SCRIPT_DATA_NAME, $params );
		Prodigy_Template::prodigy_get_template( 'shortcode/price-filter.php', $params );
	}
}

if ( ! function_exists( 'prodigy_get_template_shortcode_my_account' ) ) {
	/**
	 * @param array $data
	 *
	 * @return void
	 * @throws Exception
	 */
	function prodigy_get_template_shortcode_my_account( array $data ) {
		$mapper       = new Prodigy_My_Account_Data_Mapper( new Prodigy_Cookies() );
		$params       = $mapper->get_default_parameters( $data );
		$cache_prefix = defined( 'WP_ROCKET_VERSION' ) ? '?ver=' . PRODIGY_VERSION . '&cb=' . time() : '';

		wp_enqueue_script(
			'my-account',
			plugin_dir_url( PRODIGY_PLUGIN_PATH . '/includes/frontend/shortcodes/js/my-account.js' ) . 'my-account.js' . $cache_prefix,
			array( 'jquery' ),
			PRODIGY_VERSION,
			false
		);

		$scriptData = array(
			'nonce' => wp_create_nonce( 'store-nonce' ),
		);

		wp_localize_script( 'my-account', Prodigy_Public::MY_ACCOUNT_SCRIPT_DATA_NAME, $scriptData );

		Prodigy_Template::prodigy_get_template( 'shortcode/my-account.php', $params );
	}
}


if ( ! function_exists( 'prodigy_shortcode_template_my_account_main_content' ) ) {
	function prodigy_shortcode_template_my_account_main_content( $data ) {
		Prodigy_Template::prodigy_get_template( 'shortcode/partials/my-account-widget-content.php', $data );
	}
}


if ( ! function_exists( 'prodigy_get_template_shortcode_categories_filter' ) ) {

	function prodigy_get_template_shortcode_categories_filter( $data ) {
		if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'filter-nonce' ) ) {
			$data['query'] = build_query( $_GET ?? array() );
		}

		$mapper = new Prodigy_Categories_Filter_Data_Mapper();
		$params = $mapper->get_default_parameters( $data );
		Prodigy_Template::prodigy_get_template( 'shortcode/categories-filter.php', $params );
	}
}


if ( ! function_exists( 'prodigy_get_template_shortcode_cart' ) ) {
	function prodigy_get_template_shortcode_cart( $data ) {
		$mapper       = new Prodigy_Cart_Data_Mapper();
		$params       = $mapper->get_default_parameters( $data );
		$cache_prefix = defined( 'WP_ROCKET_VERSION' ) ? '?ver=' . PRODIGY_VERSION . '&cb=' . time() : '';

		if ( Prodigy_Elementor::is_elementor_enabled() && ! Prodigy_Elementor::is_editor_mode() ) {
			wp_enqueue_script(
				'cart-widget',
				plugin_dir_url( PRODIGY_PLUGIN_PATH . '/includes/frontend/shortcodes/js/cart-widget.js' ) . 'cart-widget.js' . $cache_prefix,
				array( 'jquery' ),
				PRODIGY_VERSION,
				false
			);
		}

		$initial_cart_count = 0;
		foreach ( $params['cart_products'] as $item ) {
			if ( is_array( $item ) && isset( $item['count_items'] ) ) {
				$initial_cart_count += (int) $item['count_items'];
			}
		}

		$scriptData = array(
			'dropdown'            => (int) $params['is_dropdown'],
			'nonce'               => wp_create_nonce( 'store-nonce' ),
			'initial_cart_count'  => $initial_cart_count,
		);

		wp_localize_script( 'cart-widget', Prodigy_Public::CART_SCRIPT_DATA_NAME, $scriptData );

		$cart_type = $params['attr_shortcode']['cart_content_gen_type'] ?? '';
		$template  = 'shortcode/cart.php';
		if ( $cart_type === 'slide-cart' ) {
			$template = 'shortcode/cart-modal-slide.php';
		}

		Prodigy_Template::prodigy_get_template( $template, $params );
	}
}

if ( ! function_exists( 'prodigy_get_template_shortcode_breadcrumbs' ) ) {
	function prodigy_get_template_shortcode_breadcrumbs( $data ) {
		$mapper = new Prodigy_Breadcrumbs_Data_Mapper();
		$params = $mapper->get_default_parameters( $data );
		Prodigy_Template::prodigy_get_template( 'shortcode/breadcrumbs.php', $params );
	}
}

if ( ! function_exists( 'prodigy_get_template_shortcode_attributes_filter_layout' ) ) {
	function prodigy_get_template_shortcode_attributes_filter_layout( $data = array() ) {
		$mapper = new Prodigy_Filter_Data_Mapper();
		$params = $mapper->get_default_parameters( $data );
		Prodigy_Template::prodigy_get_template( 'shortcode/filter-layout.php', $params );
	}
}


if ( ! function_exists( 'prodigy_get_template_shortcode_active_filters' ) ) {
	function prodigy_get_template_shortcode_active_filters( $data ) {
		$mapper       = new Prodigy_Active_Filter_Data_Mapper();
		$params       = $mapper->get_default_parameters( $data );
		$cache_prefix = defined( 'WP_ROCKET_VERSION' ) ? '?ver=' . PRODIGY_VERSION . '&cb=' . time() : '';

		wp_enqueue_script(
			'active-filter',
			plugin_dir_url( PRODIGY_PLUGIN_PATH . '/includes/frontend/shortcodes/js/active-filter.js' ) . 'active-filter.js' . $cache_prefix,
			array( 'jquery' ),
			PRODIGY_VERSION,
			false
		);
		wp_localize_script(
			'active-filter',
			'prodigy_active_filter',
			array( 'current_type_tag' => Prodigy::get_prodigy_tag_type() )
		);

		Prodigy_Template::prodigy_get_template( 'shortcode/active-filters.php', $params );
	}
}

/**
 * Render feed_categories shortcode
 */
if ( ! function_exists( 'prodigy_shortcode_get_template_feed_categories' ) ) {
	function prodigy_shortcode_get_template_feed_categories( $data ) {
		Prodigy_Template::prodigy_get_template( 'shortcode/feed_categories.php', $data );
	}
}

if ( ! function_exists( 'prodigy_shortcode_get_template_banner' ) ) {
	function prodigy_shortcode_get_template_banner( $data ) {
		Prodigy_Template::prodigy_get_template( 'shortcode/banner.php', $data );
	}
}

/**
 * Render our_categories shortcode
 */
if ( ! function_exists( 'prodigy_get_template_category_link' ) ) {
	function prodigy_get_template_category_link( $data ) {
		$mapper = new Prodigy_Category_Data_Mapper();
		$params = $mapper->get_default_parameters( $data );
		Prodigy_Template::prodigy_get_template( 'shortcode/category_link.php', $params );
	}
}

/**
 * Render cart shortcode (page)
 */
if ( ! function_exists( 'prodigy_get_template_cart_page' ) ) {
	function prodigy_get_template_cart_page( $params ) {
		$mapper         = new Prodigy_Cart_Page_Data_Mapper();
		$default_params = $mapper->get_default_parameters( $params );

		Prodigy_Template::prodigy_get_template( 'shortcode/pages/cart/common.php', $default_params );
	}
}


if ( ! function_exists( 'prodigy_get_product_brands_template' ) ) {
	/**
	 * @param array $params
	 *
	 * @return void
	 * @throws Exception
	 */
	function prodigy_get_product_brands_template( array $params ) {
		global $post;
			$mapper         = new Prodigy_Product_Brands_Data_Mapper( new Prodigy_Brands_Parser() );
			$default_params = array();

		if ( isset( $post->ID ) && Prodigy_Platform_Settings::is_enable_brands_app() ) {
			$id_remote_product = get_post_meta( $post->ID, Prodigy::PRODIGY_REMOTE_PRODUCT_ID, true );
			if ( isset( $id_remote_product ) ) {
				$default_params = $mapper->get_default_parameters( array( 'product_id' => $id_remote_product ) );
			}

			$brands_settings = array(
				'brands_list' => $default_params['data'] ?? array(),
				'widget_type' => $mapper::WIDGET_TYPE,
				'brand_text'  => $params['prg_product_brands_dropdown_default_text'],
			);
			$brands_json     = wp_json_encode( $brands_settings );
			wp_add_inline_script(
				'public',
				'(function(){var d=' . $brands_json . ';if(typeof ' . Prodigy_Public::PUBLIC_SCRIPT_DATA_NAME . ' !== "undefined"){' . Prodigy_Public::PUBLIC_SCRIPT_DATA_NAME . '.brands_settings=d;}window.brands_settings=d;})();',
				'before'
			);
		}

		if ( ( ( isset( $default_params['data'] ) && ! empty( $default_params['data'] ) ) || Prodigy_Elementor::is_editor_mode() ) ) {
			Prodigy_Template::prodigy_get_template( 'single-product/brands.php', $default_params );
		}
	}
}



/**
 * Render brand layout
 */
if ( ! function_exists( 'prodigy_get_products_brands_template' ) ) {

	/**
	 * @param array $params
	 *
	 * @return void
	 * @throws Exception
	 */
	function prodigy_get_products_brands_template( array $params ) {
		$mapper                               = new Prodigy_Brands_Data_Mapper( new Prodigy_Brands(), new Prodigy_Brands_Parser() );
		$default_params                       = $mapper->get_default_parameters( $params );
		$default_params['is_enable_swatches'] = ! isset( $params['prg_brand_enable_swatches'] ) || Prodigy_Widget_Options::$bool_mapper[ $params['prg_brand_enable_swatches'] ];

		$brands_settings = array(
			'brand_text'  => $params['prg_brand_dropdown_default_text'],
			'brand_type'  => $params['prg_brand_enable_swatches'],
			'widget_type' => $mapper::WIDGET_TYPE,
		);
		$brands_json     = wp_json_encode( $brands_settings );
		wp_add_inline_script(
			'public',
			'(function(){var d=' . $brands_json . ';if(typeof ' . Prodigy_Public::PUBLIC_SCRIPT_DATA_NAME . ' !== "undefined"){' . Prodigy_Public::PUBLIC_SCRIPT_DATA_NAME . '.brands_settings=d;}window.brands_settings=d;})();',
			'before'
		);

		Prodigy_Template::prodigy_get_template( 'products-brands.php', $default_params );
	}
}

/**
 * Render subscription shortcode (page)
 */
if ( ! function_exists( 'prodigy_get_template_shortcode_subscription' ) ) {
	function prodigy_get_template_shortcode_subscription( $data ) {
		Prodigy_Template::prodigy_get_template( 'shortcode/theme/subscription.php', $data );
	}
}

/**
 * Render products price block
 */
if ( ! function_exists( 'prodigy_products_price_block_template' ) ) {
	function prodigy_products_price_block_template( $data ) {
		Prodigy_Template::prodigy_get_template( 'shop/products-price.php', $data );
	}
}

/**
 * Render products widgets template
 */
if ( ! function_exists( 'prodigy_get_template_products' ) ) {
	function prodigy_get_template_products( $options ) {
		if ( isset( $_GET['_wpnonce'] ) && ! wp_verify_nonce( sanitize_key( wp_unslash( $_GET['_wpnonce'] ) ), 'filter-nonce' ) ) {
			return array();
		}
		$options['pg']        = isset( $_GET['pg'] ) ? sanitize_key( wp_unslash( $_GET['pg'] ) ) : 1;
		$products_data_mapper = new Prodigy_Products_Data_Mapper();
		$params               = $products_data_mapper->get_default_parameters( $options );
		if ( $params['display'] === Prodigy_Main_Data_Mapper::SLIDER ) {
			Prodigy_Template::prodigy_get_template( 'shortcode/products.php', $params );
		} else {
			$item_per_page = 0;
			if ( $params['display'] === Prodigy_Main_Data_Mapper::GRID ) {
				$item_per_page = Prodigy_Pagination::get_per_page_by_options();
			}

			$params['pagination'] = $products_data_mapper->get_pagination( (int) $options['pg'], $item_per_page, $params['products'] );
			Prodigy_Template::prodigy_get_template( 'shortcode/products-grid.php', $params );
		}
	}
}

/**
 * Render the categories' widget.
 *
 * @param array $config Configuration settings for the widget.
 */
if ( ! function_exists( 'prodigy_get_template_categories' ) ) {
	/**
	 * @param $settings
	 *
	 * @return void
	 */
	function prodigy_get_template_categories( $settings = array() ) {
		$settings               = is_array( $settings ) ? $settings : array();
		$categories_data_mapper = new Prodigy_Categories_Data_Mapper();
		$params                 = $categories_data_mapper->get_default_parameters( $settings );
		$cache_prefix           = defined( 'WP_ROCKET_VERSION' ) ? '?ver=' . PRODIGY_VERSION . '&cb=' . time() : '';

		if ( $params['display'] === Prodigy_Main_Data_Mapper::SLIDER ) {
			Prodigy_Template::prodigy_get_template( 'shortcode/categories.php', $params );
			wp_enqueue_script( 'categories', plugin_dir_url( PRODIGY_PLUGIN_PATH . '/includes/frontend/shortcodes/js/categories.js' ) . 'categories.js' . $cache_prefix, array( 'jquery' ), PRODIGY_VERSION, false );
		} else {
			Prodigy_Template::prodigy_get_template( 'shortcode/categories_grid.php', $params );
		}
	}
}

/**
 * Render products shortcode
 */
if ( ! function_exists( 'prodigy_get_template_shortcode_products' ) ) {
	function prodigy_get_template_shortcode_products( $data ) {
		$data         = is_array( $data ) ? $data : array();
		$data_mapper  = new Prodigy_Related_Products_Data_Mapper();
		$options      = $data_mapper->get_default_parameters( (array) $data );
		$cache_prefix = defined( 'WP_ROCKET_VERSION' ) ? '?ver=' . PRODIGY_VERSION . '&cb=' . time() : '';

		if ( isset( $data['display'] ) && $data['display'] === Prodigy_Main_Data_Mapper::GRID ) {
			Prodigy_Template::prodigy_get_template( 'shortcode/related-grid-products.php', $options );
		} else {
			Prodigy_Template::prodigy_get_template( 'shortcode/related-slider-products.php', $options );
			wp_enqueue_script( 'feature-products', plugin_dir_url( PRODIGY_PLUGIN_PATH . '/includes/frontend/shortcodes/js/feature-products.js' ) . 'feature-products.js' . $cache_prefix, array( 'jquery' ), PRODIGY_VERSION, false );
		}
	}
}


if ( ! function_exists( 'prodigy_get_thank_you_page' ) ) {
	function prodigy_get_thank_you_page() {
		echo do_shortcode( '[prodigy_thank_you_page]' );
	}
}

if ( ! function_exists( 'prodigy_mobile_sortable' ) ) {
	/**
	 * @param $settings
	 *
	 * @return void
	 */
	function prodigy_mobile_sortable( $settings = array() ) {
		$categories_data_mapper = new Prodigy_Shop_Mobile_Sortable_Data_Mapper();
		$params                 = $categories_data_mapper->get_default_parameters( $settings );
		Prodigy_Template::prodigy_get_template( 'shop/mobile-sortable.php', $params );
	}
}

if ( ! function_exists( 'prodigy_shop_get_breadcrumbs_template' ) ) {
	function prodigy_shop_get_breadcrumbs_template() {
		echo do_shortcode( '[prodigy_breadcrumbs_list]' );
	}
}

if ( ! function_exists( 'prodigy_get_active_filters' ) ) {
	function prodigy_get_active_filters( $instance ) {
		if ( ! empty( $instance ) ) {
			$str = http_build_query( $instance, '', ' ' );
			echo do_shortcode( '[prodigy_active_filter ' . $str . ']' );
		}
	}
}

if ( ! function_exists( 'prodigy_shop_get_template_sortable' ) ) {
	/**
	 * @param $settings
	 *
	 * @return void
	 */
	function prodigy_shop_get_template_sortable( $settings = array() ) {
		$categories_data_mapper = new Prodigy_Shop_Sortable_Data_Mapper();
		$params                 = $categories_data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'shop/sortable.php', $params );
	}
}

if ( ! function_exists( 'prodigy_shop_get_content_template' ) ) {
	function prodigy_shop_get_content_template( $content ) {
		Prodigy_Template::prodigy_get_template( 'shop/content.php', $content );
	}
}

if ( ! function_exists( 'prodigy_get_template_without_result_template' ) ) {
	function prodigy_get_template_without_result_template() {
		Prodigy_Template::prodigy_get_template( 'shop/no-search-results.php' );
	}
}


if ( ! function_exists( 'prodigy_get_template_products_loop' ) ) {
	function prodigy_get_template_products_loop( $parameters ) {
		$data_mapper   = new Prodigy_Shop_Products_Data_Mapper();
		$template_data = $data_mapper->get_default_parameters( $parameters );
		Prodigy_Template::prodigy_get_template( 'shop/products-loop.php', $template_data );
	}
}


if ( ! function_exists( 'prodigy_get_template_products_loop_rating' ) ) {
	function prodigy_get_template_products_loop_rating( $data ) {
		Prodigy_Template::prodigy_get_template( 'shop/products-loop-rating.php', $data );
	}
}

if ( ! function_exists( 'prodigy_get_pagination_template' ) ) {
	function prodigy_get_pagination_template() {
		Prodigy_Template::prodigy_get_template( 'shop/pagination.php' );
	}
}

if ( ! function_exists( 'prodigy_shop_sidebar_template' ) ) {
	/**
	 * Render shop sidebar
	 *
	 * @return void
	 */
	function prodigy_shop_sidebar_template() {
		$customizer_shop_options = get_option( 'prodigy_shop_settings' );
		$prodigy_shop_sidebar    = $customizer_shop_options['prodigy_shop_sidebar_display'] ?? Prodigy_Customizer::PRODIGY_SHOW_SIDEBAR;
		$options                 = compact( 'prodigy_shop_sidebar' );

		if ( $prodigy_shop_sidebar ) {
			Prodigy_Template::prodigy_get_template( 'shop/sidebar.php', $options );
		}
	}
}

if ( ! function_exists( 'prodigy_photoswipe' ) ) {
	function prodigy_photoswipe() {
		Prodigy_Template::prodigy_get_template( 'single-product/photoswipe.php' );
	}
}

if ( ! function_exists( 'prodigy_product_template_short_description' ) ) {
	/**
	 * Output the start of the page wrapper.
	 */
	function prodigy_product_template_short_description( $settings = array() ) {
		$data_mapper = new Prodigy_Product_Short_Description_Data_Mapper();
		$parameters  = $data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'single-product/short-description.php', $parameters );
	}
}

if ( ! function_exists( 'prodigy_product_template_title' ) ) {
	/**
	 * Output the product title.
	 *
	 * @param array $settings
	 *
	 * @throws DOMException
	 */
	function prodigy_product_template_title( $settings = array() ) {
		$data_mapper = new Prodigy_Product_Title_Data_Mapper();
		$parameters  = $data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'single-product/title.php', $parameters );
	}
}

if ( ! function_exists( 'prodigy_product_template_range_price' ) ) {
	/**
	 * Output the product price range.
	 */
	function prodigy_product_template_range_price( $settings = array() ) {
		if ( Prodigy_Platform_Settings::is_redemption_store_status() ) {
			$data_mapper = new Prodigy_Product_Price_Data_Mapper();
			$parameters  = $data_mapper->get_default_parameters( (array) $settings );
			Prodigy_Template::prodigy_get_template( 'single-product/range-price.php', $parameters );
		}
	}
}

if ( ! function_exists( 'prodigy_product_template_quick_view_title' ) ) {
	/**
	 * Output the product quick view title.
	 */
	function prodigy_product_template_quick_view_title() {
		Prodigy_Template::prodigy_get_template( 'single-product/quick_view_title.php' );
	}
}


if ( ! function_exists( 'prodigy_product_template_variants' ) ) {
	/**
	 * Output the product variants.
	 */
	function prodigy_product_template_variants( $settings = array() ) {
		$data_mapper = new Prodigy_Product_Variants_Data_Mapper();
		$parameters  = $data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'single-product/variants-layout.php', $parameters );
	}
}

if ( ! function_exists( 'prodigy_product_get_logo_template' ) ) {
	/**
	 * @param array $params
	 *
	 * @return void
	 */
	function prodigy_product_get_logo_template( array $params ) {
		$uuid = 'logo-' . get_the_ID() . '-1';
		if ( isset( $params['params'] ) ) {
			$params['params']['uid'] = $uuid;
		}
		if ( isset( $params['params']['settings'] ) ) {
			$params['params']['settings']['uid'] = $uuid;
		}
		Prodigy_Template::prodigy_get_template( 'single-product/logo/product-logo.php', $params );
	}
}

if ( ! function_exists( 'prodigy_product_template_meta' ) ) {
	/**
	 * Output the product meta.
	 */
	function prodigy_product_template_meta() {
		Prodigy_Template::prodigy_get_template( 'single-product/meta.php' );
	}
}

if ( ! function_exists( 'prodigy_product_template_add_to_cart' ) ) {
	/**
	 * Output the product cart.
	 */
	function prodigy_product_template_add_to_cart( $settings = array() ) {
		$productTemplate = new Prodigy_Product_Template_Builder( new Prodigy_Product_Template_Item() );

		$data_mapper = new Prodigy_Product_Add_To_Cart_Data_Mapper( $productTemplate );
		$parameters  = $data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'single-product/add-to-cart.php', $parameters );
	}
}

if ( ! function_exists( 'prodigy_product_template_additional_info' ) ) {
	/**
	 * Output the product sku.
	 */
	function prodigy_product_template_additional_info( $settings = array() ) {
		Prodigy_Template::prodigy_get_template( 'single-product/product-additional-info.php', $settings );
	}
}

if ( ! function_exists( 'prodigy_product_template_sku' ) ) {
	/**
	 * Output the product sku.
	 */
	function prodigy_product_template_sku( $settings = array() ) {
		$data_mapper = new Prodigy_Product_Sku_Data_Mapper();
		$parameters  = $data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'single-product/product-sku.php', $parameters );
	}
}

if ( ! function_exists( 'prodigy_product_template_tags' ) ) {
	/**
	 * Output the product tags.
	 */
	function prodigy_product_template_tags( $settings = array() ) {
		$data_mapper = new Prodigy_Product_Tags_Data_Mapper();
		$parameters  = $data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'single-product/product-tags.php', $parameters );
	}
}

if ( ! function_exists( 'prodigy_single_product_breadcrumbs_template' ) ) {
	/**
	 * Output the breadcrumbs.
	 */
	function prodigy_single_product_breadcrumbs_template( $settings = array() ) {
		$data_mapper = new Prodigy_Product_Breadcrumbs_Data_Mapper();
		$parameters  = $data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'single-product/breadcrumbs.php', $parameters );
	}
}

if ( ! function_exists( 'prodigy_show_product_images' ) ) {
	/**
	 * Output the product image.
	 */
	function prodigy_show_product_images( $settings = array() ) {
		$data_mapper = new Prodigy_Product_Image_Data_Mapper();
		$parameters  = $data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'single-product/product-image.php', $parameters );
	}
}

if ( ! function_exists( 'prodigy_show_product_thumbnails' ) ) {
	/**
	 * Output the product thumbnails.
	 */
	function prodigy_show_product_thumbnails( $settings = array() ) {
		$data_mapper = new Prodigy_Product_Thumbnails_Data_Mapper();
		$parameters  = $data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'single-product/product-thumbnails.php', $parameters );
	}
}

if ( ! function_exists( 'max_srcset_image_width_func' ) ) {

	/**
	 * Output the product tabs.
	 */
	function max_srcset_image_width_func() {
		return 1;
	}
}

if ( ! function_exists( 'prodigy_output_product_data_tabs' ) ) {

	/**
	 * Output the product tabs.
	 */
	function prodigy_output_product_data_tabs( $settings = array() ) {
		$product_template = new Prodigy_Product_Template_Builder( new Prodigy_Product_Template_Item() );
		$product          = empty( $GLOBALS['prodigy_product'] ) && isset( $settings['idWidget'] ) ?
			$product_template->set_product( (int) $settings['default_product_id'] ) :
			$GLOBALS['prodigy_product'];

		$settings           = is_array( $settings ) ? $settings : array();
		$settings['params'] = array(
			'product'     => $product,
			'settings'    => $settings ?? array(),
			'description' => $product->get_remote_description(),
		);

		Prodigy_Template::prodigy_get_template( 'single-product/tabs/tabs.php', $settings );
	}
}

if ( ! function_exists( 'prodigy_review_modals_template' ) ) {
	function prodigy_review_modals_template( $settings = array() ) {
		Prodigy_Template::prodigy_get_template( 'single-product/modals/review.php', $settings );
	}
}


if ( ! function_exists( 'prodigy_product_get_impersonation_template' ) ) {

	/**
	 * Get impersonation template.
	 */
	function prodigy_product_get_impersonation_template() {
		Prodigy_Template::prodigy_get_template( 'global/impersonation.php' );
	}
}


if ( ! function_exists( 'prodigy_product_description_tab' ) ) {

	/**
	 * Output the description tab description.
	 */
	function prodigy_product_description_tab() {
		Prodigy_Template::prodigy_get_template( 'single-product/tabs/description.php' );
	}
}

if ( ! function_exists( 'prodigy_success_review_modals_template' ) ) {

	/**
	 * Output the success review modal
	 */
	function prodigy_success_review_modals_template() {
		Prodigy_Template::prodigy_get_template( 'single-product/modals/success-review.php' );
	}
}

if ( ! function_exists( 'prodigy_product_additional_information_tab' ) ) {

	/**
	 * Output the attributes tab additional information.
	 */
	function prodigy_product_additional_information_tab() {
		Prodigy_Template::prodigy_get_template( 'single-product/tabs/additional-information.php' );
	}
}

if ( ! function_exists( 'prodigy_product_comments_tab' ) ) {

	/**
	 * Output the attribute tab additional information.
	 */
	function prodigy_product_comments_tab( $settings = array() ) {
		$data_mapper = new Prodigy_Product_Review_Data_Mapper();
		$parameters  = $data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'single-products-reviews.php', $parameters );
	}
}


if ( ! function_exists( 'prodigy_product_tiered_prices_tab' ) ) {
	/**
	 * Output the tiered prices information.
	 */
	function prodigy_product_tiered_prices_tab( $settings = array() ) {
		$data_mapper = new Prodigy_Product_Tiered_Prices_Data_Mapper();
		$parameters  = $data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'single-product/tabs/tiered-prices.php', $parameters );
	}
}

if ( ! function_exists( 'is_prodigy_product_taxonomy' ) ) {

	/**
	 * Is_prodigy_product_taxonomy - Returns true when viewing a product taxonomy archive.
	 *
	 * @return bool
	 */
	function is_prodigy_product_taxonomy() {
		return is_tax( get_object_taxonomies( Prodigy::get_prodigy_product_type() ) );
	}
}

if ( ! function_exists( 'prodigy_default_product_tabs' ) ) {

	/**
	 * @param $params
	 *
	 * @return array|void
	 */
	function prodigy_default_product_tabs( $params ) {
		if ( empty( $GLOBALS['prodigy_product'] ) && ! empty( $params['product'] ) ) {
			$GLOBALS['prodigy_product'] = $params['product'];
		}

		$product = $GLOBALS['prodigy_product'];

		if ( ! empty( $product ) ) {
			$description = $product->get_remote_description();
			$tabs        = array();
			// Description tab - shows product content.
			if ( $description ) {
				$tabs['description'] = array(
					'title'    => __( 'Description', 'prodigy-commerce' ),
					'priority' => 10,
					'callback' => 'prodigy_product_description_tab',
					'settings' => $params['settings'] ?? Prodigy_Tabs::TAB_VALUE_NEGATIVE_OPTION,
				);
			}

			$tabs['additional_information'] = array(
				'title'    => __( 'Additional information', 'prodigy-commerce' ),
				'priority' => 20,
				'callback' => 'prodigy_product_additional_information_tab',
				'settings' => $params['settings'] ?? Prodigy_Tabs::TAB_VALUE_NEGATIVE_OPTION,
			);

			$count           = Prodigy_Product_Comments::get_count_comments( (int) $product->get_field( 'ID' ) );
			$tabs['reviews'] = array(
				/* translators: %d: number of reviews */
				'title'    => sprintf( __( 'Reviews (%d)', 'prodigy-commerce' ), $count ?? 0 ),
				'priority' => 30,
				'callback' => 'prodigy_product_comments_tab',
				'settings' => $params['settings'] ?? Prodigy_Tabs::TAB_VALUE_NEGATIVE_OPTION,
			);

			if ( ( defined( 'ELEMENTOR_VERSION' )
					&& \Elementor\Plugin::$instance->editor->is_edit_mode() ) ||
				$product->tiered_prices
			) {
				$tabs['tiered_prices'] = array(
					'title'    => __( 'QTY and Price Breaks', 'prodigy-commerce' ),
					'priority' => 40,
					'callback' => 'prodigy_product_tiered_prices_tab',
					'settings' => $params['settings'] ?? Prodigy_Tabs::TAB_VALUE_NEGATIVE_OPTION,
				);
			}

			return $tabs;
		}
	}
}

if ( ! function_exists( 'prodigy_comments' ) ) {

	/**
	 * Output the Review comments template.
	 *
	 * @param WP_Comment $comment
	 * @param array      $args
	 * @param int        $depth
	 */
	function prodigy_comments( $comment, $args, $depth ) {
		$GLOBALS['prodigy_comment'] = $comment;
		Prodigy_Template::prodigy_get_template(
			'single-product/review.php',
			array(
				'comment' => $comment,
				'args'    => $args,
				'depth'   => $depth,
			)
		);
	}
}

if ( ! function_exists( 'prodigy_display_product_attributes' ) ) {
	/**
	 * Outputs a list of product attributes for a product.
	 */
	function prodigy_display_product_attributes( $settings = array() ) {
		$data_mapper = new Prodigy_Product_Attributes_Data_Mapper(
			new Prodigy_Product_Template_Builder( new Prodigy_Product_Template_Item() )
		);
		$parameters  = $data_mapper->get_default_parameters( (array) $settings );
		Prodigy_Template::prodigy_get_template( 'single-product/product-attributes.php', $parameters );
	}
}


if ( ! function_exists( 'prodigy_product_template_review_rating' ) ) {
	/**
	 * Display the reviewers star rating
	 *
	 * @return void
	 */
	function prodigy_product_template_review_rating() {
		if ( post_type_supports( Prodigy::get_prodigy_product_type(), 'comments' ) ) {
			Prodigy_Template::prodigy_get_template( 'single-product/review-rating.php' );
		}
	}
}

if ( ! function_exists( 'prodigy_product_template_rating' ) ) {

	/**
	 * Output the product rating.
	 */
	function prodigy_product_template_rating( $settings = array() ) {

		if ( post_type_supports( Prodigy::get_prodigy_product_type(), 'comments' ) ) {
			$data_mapper = new Prodigy_Product_Rating_Data_Mapper();
			$parameters  = $data_mapper->get_default_parameters( (array) $settings );
			Prodigy_Template::prodigy_get_template( 'single-product/rating.php', $parameters );
		}
	}
}


if ( ! function_exists( 'prodigy_review_display_meta' ) ) {
	/**
	 * Display the review authors meta (name, verified owner, review date)
	 *
	 * @return void
	 */
	function prodigy_review_display_meta() {
		Prodigy_Template::prodigy_get_template( 'single-product/review-meta.php' );
	}
}

if ( ! function_exists( 'prodigy_review_display_comment_text' ) ) {

	/**
	 * Display the review content.
	 */
	function prodigy_review_display_comment_text() {
		echo '<div class="prodigy-product-review__text">';
		comment_text();
		echo '</div>';
	}
}

/**
 * Get HTML for ratings.
 *
 * @param float $rating
 *
 * @return string
 */
function pg_get_rating_html( $rating ) {
	$html = '<div class="prodigy-star-rating">' . pg_get_star_rating_html( $rating ) . '</div>';

	return apply_filters( 'prodigy_product_get_rating_html', $html, $rating );
}

/**
 * Get HTML for star rating.
 *
 * @param float $rating
 *
 * @return string
 */
function pg_get_star_rating_html( $rating ) {
	$html = '<span class="prodigy-star-rating__item" style="width:' . ( ( roundToHalf( $rating ) / 5 ) * 100 ) . '%"></span>';

	return apply_filters( 'pg_get_star_rating_html', $html, $rating );
}

/**
 * @param int $x
 *
 * @return float
 */
function roundToHalf( $x ) {
	if ( ! $x ) {
		return 0;
	}

	return ceil( $x / 0.5 ) * 0.5;
}


if ( ! function_exists( 'prodigy_review_display_gravatar' ) ) {
	/**
	 * Display the review authors gravatar
	 *
	 * @param array $comment
	 *
	 * @return void
	 */
	function prodigy_review_display_gravatar( $comment ) {
		echo get_avatar( $comment, apply_filters( 'prodigy_review_gravatar_size', '60' ), '' );
	}
}


/**
 * @param array $attachment
 * @param int   $image_id
 *
 * @return string
 */
function prodigy_get_remote_gallery_image_html( array $attachment, int $image_id ): string {
	$width        = $attachment['cropping-params']['w'] ?? '800';
	$height       = $attachment['cropping-params']['h'] ?? '1000';
	$view_box     = 0 . ' ' . 0 . ' ' . $width . ' ' . $height;
	$image_url    = $attachment['cropped-url'] ?? '';
	$image_retina = $attachment['versions']['catalog_retina'] ?? '';

	$image = '<svg style="vertical-align: middle; height: 100%" viewBox="' . $view_box . '" xmlns="http://www.w3.org/2000/svg">
		<desc>"' . esc_html__( 'Product', 'prodigy-commerce' ) . '"</desc>
			<image class="thumb-gallery-image-js" style="height: 100%; width: 100%" href="' . $image_url . '" data-image-id="' . $image_id . '" loading="lazy" />

			<image class="thumb-gallery-logo-js" href="" data-image-id="' . $image_id . '" height="0" width="0" x="0" y="0" style="" loading="lazy"></image>
		</svg>';

	return '<div class="swiper-slide"><div class="prodigy-product__gallery-img p-0">' . $image . '</div></div>';
}

/**
 * @param array $line_item
 *
 * @return string
 */
function get_cart_item_logo_image_template( array $line_item ): string {
	if ( ! isset( $line_item['attributes']['image-url'] ) ) {
		return '<i class="icon icon-image"></i>';
	}

	$width    = $line_item['attributes']['image-cropping-params']['w'] ?? '800';
	$height   = $line_item['attributes']['image-cropping-params']['h'] ?? '1000';
	$view_box = 0 . ' ' . 0 . ' ' . $width . ' ' . $height;

	$svg = sprintf(
		'<svg style="vertical-align: middle; height: 100%%" viewBox="%s" xmlns="http://www.w3.org/2000/svg">
            <desc>%s</desc>
            <image style="width: 100%%; height: 100%%;" href="%s" loading="lazy"></image>',
		esc_attr( $view_box ),
		esc_html__( 'Product', 'prodigy-commerce' ),
		esc_url( $line_item['attributes']['image-url'] ?? '' )
	);

	if ( ! empty( $line_item['attributes']['logos'] ) ) {
		foreach ( $line_item['attributes']['logos'] as $logo ) {
			if ( $logo['logo']['visible'] ) {
				$transformX = $logo['location']['x'] + ( $logo['location']['width'] / 2 );
				$transformY = $logo['location']['y'] + ( $logo['location']['height'] / 2 );
				$svg       .= sprintf(
					'<image href="%s" height="%s" width="%s" x="%s" y="%s" style="transform-origin: %spx %spx; transform: rotate(%sdeg) rotateY(%sdeg) rotateX(%sdeg);" loading="lazy"></image>',
					esc_url( $logo['logo']['original-url'] ?? '' ),
					esc_attr( $logo['location']['height'] ?? '' ),
					esc_attr( $logo['location']['width'] ?? '' ),
					esc_attr( $logo['location']['x'] ?? '' ),
					esc_attr( $logo['location']['y'] ?? '' ),
					esc_attr( $transformX ),
					esc_attr( $transformY ),
					esc_attr( $logo['location']['angle'] ?? '' ),
					esc_attr( $logo['location']['rotation-y'] ?? '' ),
					esc_attr( $logo['location']['rotation-x'] ?? '' )
				);
			}
		}
	}
	$svg .= '</svg>';

	return $svg;
}

/**
 * @param array $product
 *
 * @return string
 */
function get_product_logo_image_template( array $product ): string {
	if ( empty( $product['versions-image-url']['cropped'] ) ) {
		return '';
	}

	$width    = $product['image-cropping-params']['w'] ?? '1000';
	$height   = $product['image-cropping-params']['h'] ?? '1000';
	$view_box = 0 . ' ' . 0 . ' ' . $width . ' ' . $height;

	$svg = sprintf(
		'<svg class="prodigy-product-list__item-preview-img" style="vertical-align: middle;height: 100%%" viewBox="%s" xmlns="http://www.w3.org/2000/svg">
            <desc>%s</desc>
            <image style="width: 100%%;height: 100%%" href="%s" loading="lazy"></image>',
		esc_attr( $view_box ),
		esc_html__( 'Product', 'prodigy-commerce' ),
		esc_url( $product['versions-image-url']['cropped'] ?? '' )
	);

	if ( isset( $product['required_logos'] ) ) {
		foreach ( $product['required_logos'] as $required_logo ) {
			if (
				! empty( $required_logo['logo-location'] ) &&
				! empty( $required_logo['logo'] ) &&
				! empty( $product['image-cropping-params'] )
			) {
				if ( isset( $required_logo['logo-location']['x'], $required_logo['logo-location']['width'] ) ) {
					$transformX = $required_logo['logo-location']['x'] + ( $required_logo['logo-location']['width'] / 2 );
				}
				if ( isset( $required_logo['logo-location']['y'], $required_logo['logo-location']['height'] ) ) {
					$transformY = $required_logo['logo-location']['y'] + ( $required_logo['logo-location']['height'] / 2 );
				}

				$svg .= sprintf(
					'<image href="%s" height="%s" width="%s" x="%s" y="%s" style="transform-origin: %spx %spx; transform: rotate(%sdeg) rotateY(%sdeg) rotateX(%sdeg);" loading="lazy"></image>',
					esc_url( $required_logo['logo']['original-url'] ?? '' ),
					esc_attr( $required_logo['logo-location']['height'] ?? '' ),
					esc_attr( $required_logo['logo-location']['width'] ?? '' ),
					esc_attr( $required_logo['logo-location']['x'] ?? '' ),
					esc_attr( $required_logo['logo-location']['y'] ?? '' ),
					esc_attr( $transformX ?? 0 ),
					esc_attr( $transformY ?? 0 ),
					esc_attr( $required_logo['logo-location']['angle'] ?? '' ),
					esc_attr( $required_logo['logo-location']['rotation-y'] ?? '' ),
					esc_attr( $required_logo['logo-location']['rotation-x'] ?? '' )
				);
			}
		}
	}

	$svg .= '</svg>';

	return $svg;
}

/**
 * @param array $attachment
 * @param int   $image_id
 *
 * @return string
 */
function prodigy_get_remote_gallery_thumb_html( array $attachment, int $image_id ): string {
	$width        = $attachment['cropping-params']['w'] ?? '800';
	$height       = $attachment['cropping-params']['h'] ?? '1000';
	$view_box     = 0 . ' ' . 0 . ' ' . $width . ' ' . $height;
	$image_url    = $attachment['cropped-url'] ?? '';
	$image_retina = $attachment['versions']['catalog_retina'] ?? '';

	$image = '<div class="prodigy-product__gallery-img p-0">
				<img class="d-none" alt="' . esc_html__( 'Product', 'prodigy-commerce' ) . '" data-large_image_width="' . $width . '" data-large_image_height="' . $height . '" src="' . $image_url . '" data-large_image="' . $image_retina . '" />
				<svg style="vertical-align: middle;" viewBox="' . $view_box . '" xmlns="http://www.w3.org/2000/svg" >
					<image class="main-gallery-image-js" style="width: 100%" data-image-id="' . $image_id . '" href="' . $image_url . '"></image>
					<image class="prodigy-product__gallery-logo-js" data-image-id="' . $image_id . '" data-id="logo-id" href="" height="200" width="200" x="363" y="235" style="transform-origin: 463px 352.5px; transform: rotate(-24deg) rotateY(45deg) rotateX(45deg);"></image>
				</svg>
			  </div>';

	return '<div class="swiper-slide">' . $image . '</div>';
}


/**
 * @param int    $category_id
 * @param object $post
 *
 * @return bool
 */
function has_post_category( int $category_id, $post ): bool {
	return has_term( $category_id, Prodigy::get_prodigy_category_type(), $post );
}

/**
 * @param int    $tag_id
 * @param object $post
 *
 * @return bool
 */
function has_post_tag( int $tag_id, $post ): bool {
	return has_term( $tag_id, Prodigy::get_prodigy_tag_type(), $post );
}

function get_format_related_products( $products ) {
	$products_data = array();

	if ( ! empty( $products ) ) {

		foreach ( $products as $key => $product ) {
			$local_product = Prodigy_Products::get_product_meta_by_remote_id( 'prodigy_remote_product_id', (int) $product['id'] );

			$products_data['data'][ $key ]['id']                    = (int) $product['id'];
			$products_data['data'][ $key ]['name']                  = $product['attributes']['name'] ?? '';
			$products_data['data'][ $key ]['price']                 = $product['attributes']['price'] ?? '';
			$products_data['data'][ $key ]['sale-price']            = $product['attributes']['sale-price'] ?? '';
			$products_data['data'][ $key ]['versions-image-url']    = $product['attributes']['versions-image-url'] ?? '';
			$products_data['data'][ $key ]['rating']                = $product['attributes']['rating'] ?? '';
			$products_data['data'][ $key ]['logo-location']         = $product['attributes']['logo-location'] ?? '';
			$products_data['data'][ $key ]['logo']                  = $product['attributes']['logo'] ?? '';
			$products_data['data'][ $key ]['image-cropping-params'] = $product['attributes']['image-cropping-params'] ?? '';
			$products_data['data'][ $key ]['tiered-price']          = $product['attributes']['tiered-price'] ?? false;
			$products_data['data'][ $key ]['display-price-range']   = $product['attributes']['display-price-range'] ?? false;
			$products_data['data'][ $key ]['price-range']           = $product['attributes']['price-range'] ?? array();
			$products_data['data'][ $key ]['required_logos']        = $product['attributes']['required_logos'] ?? array();
			if ( isset( $local_product->post_id ) ) {
				$products_data['data'][ $key ]['local_url'] = esc_url( get_permalink( $local_product->post_id ) );
			}

			$categories                                = explode( ',', $product['attributes']['categories-list'] ?? '' );
			$products_data['data'][ $key ]['category'] = $categories[0] ?? '';
		}
		$products_data['products-count'] = $product['attributes']['products-count'] ?? '';
	}

	return $products_data;
}

if ( ! function_exists( 'get_active_product_tab' ) ) {
	/**
	 * Set tabs
	 *
	 * @param array $tabs
	 * @param array $settings
	 *
	 * @return array
	 */
	function get_active_product_tab( array $tabs, array $settings = array() ): array {
		$available_tabs = array_filter(
			array(
				'tiered_price'    => isset( $tabs['tiered_prices'] ) && isset( $settings['general_tabs_controls_content_tiered_prices'] ) && $settings['general_tabs_controls_content_tiered_prices'],
				'description'     => isset( $tabs['description'] ) && ( isset( $settings['general_tabs_controls_content_description'] ) && $settings['general_tabs_controls_content_description'] ),
				'additional_info' => isset( $tabs['additional_information'] ) && ( isset( $settings['general_tabs_controls_content_additional_info'] ) && $settings['general_tabs_controls_content_additional_info'] ),
				'reviews'         => isset( $tabs['reviews'] ) && ( isset( $settings['general_tabs_controls_content_reviews'] ) && $settings['general_tabs_controls_content_reviews'] ),
			)
		);

		if ( empty( $available_tabs ) || isset( $available_tabs['description'] ) ) {
			$tabs['description']['active']                          = true;
			$tabs['description']['prodigy_product_description_tab'] = true;
		}

		if ( isset( $available_tabs['tiered_price'] ) ) {
			$tabs['tiered_prices']['prodigy_product_tiered_prices_tab'] = true;
		}

		if ( isset( $available_tabs['additional_info'] ) ) {
			$tabs['additional_information']['prodigy_product_additional_information_tab'] = true;
		}

		if ( isset( $available_tabs['reviews'] ) ) {
			$tabs['reviews']['prodigy_product_comments_tab'] = true;
		}

		return $tabs;
	}

	/**
	 * @param string $min_price
	 * @param string $max_price
	 * @param bool   $is_show_min_price
	 *
	 * @return void
	 */
	function display_price_range( string $min_price, string $max_price, bool $is_show_min_price = false ) {
		if ( $is_show_min_price ) {
			$min_display = Prodigy_Formatting::display_price( $min_price ?? '0.00' );
			$max_display = Prodigy_Formatting::display_price( $max_price ?? '0.00' );
			echo '<div class="prodigy-product-list__item-price">' . esc_html( $min_display ) . '</div>';
			echo '<span class="prodigy-product-list__item-price"> &nbsp-&nbsp </span>';
			echo '<div class="prodigy-product-list__item-price">' . esc_html( $max_display ) . '</div>';
		} else {
			$min_display = Prodigy_Formatting::display_price( $min_price ?? '0.00' );
			echo '<div class="prodigy-product-list__item-price">' . esc_html( $min_display ) . '</div>';
		}
	}

	/**
	 * @param string      $price
	 * @param bool        $is_sale
	 * @param string|null $sale_price
	 *
	 * @return void
	 */
	function display_single_price( string $price, bool $is_sale = false, string $sale_price = null ) {
		if ( $is_sale ) {
			$price_display      = Prodigy_Formatting::display_price( $price ?? '0.00' );
			$sale_price_display = Prodigy_Formatting::display_price( $sale_price ?? '0.00' );
			echo '<div class="prodigy-product-list__item-price prodigy-product-list__item-price--sale">' . esc_html( $price_display ) . '</div>&nbsp';
			echo '<span class="prodigy-product-list__item-price"> </span>';
			echo '<div class="prodigy-product-list__item-price">' . esc_html( $sale_price_display ) . '</div>';
		} else {
			$price_display = Prodigy_Formatting::display_price( $price ?? '0.00' );
			echo '<div class="prodigy-product-list__item-price">' . esc_html( $price_display ) . '</div>';
		}
	}
}
