<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://wpconfiguratorbuilder.com/
 * @since      1.0.0
 *
 * @package    wcb-configurator-builder
 * @subpackage wcb-configurator-builder/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    wcb-configurator-builder
 * @subpackage wcb-configurator-builder/public
 * @author     WPCB <support@wpconfiguratorbuilder.com>
 */
class WPCB_Public {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * The scripts has been output.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      boolean    $scripts    The status of scripts display of this plugin.
	 */
	private $scripts;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since      1.0.0
	 * @param      string $plugin_name       The name of the plugin.
	 * @param      string $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version     = $version;
		$this->scripts     = false;

		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 99 );
		add_action( 'wpcb_public_output_scripts', array( $this, 'output_scripts' ) );
		add_action( 'after_setup_theme', array( $this, 'register_image_sizes' ) );
		add_action( 'wp_footer', array( $this, 'enqueue_threejs_with_importmap' ) );

		add_filter( 'script_loader_tag', array( $this, 'set_module_script_loader_tag' ), 10, 3 );
		add_filter( 'body_class', array( $this, 'set_body_configurator_class' ) );
	}

	/**
	 * Register the configurator stylesheets for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {
		wp_register_style( $this->plugin_name . '_slick', plugin_dir_url( __FILE__ ) . 'assets/css/wpcb-slick.css', array(), $this->version, 'all' );
		wp_register_style( $this->plugin_name . '_lightbox', plugin_dir_url( __FILE__ ) . 'assets/css/wpcb-lightbox.min.css', array(), $this->version, 'all' );
		wp_register_style( $this->plugin_name . '_public', plugin_dir_url( __FILE__ ) . 'assets/css/wpcb-public.css', array(), $this->version, 'all' );

		if ( isset( $_GET['page'] ) && 'wpcb_configuration_preview' === esc_attr( $_GET['page'] ) || wp_is_block_theme() ) {
			wp_add_inline_style( $this->plugin_name . '_public', $this->enqueue_inline_styles() );
		}

		wp_register_script( $this->plugin_name . '_slick', plugin_dir_url( __FILE__ ) . 'assets/js/wpcb-slick.min.js', array( 'jquery' ), $this->version, true );
		wp_register_script( $this->plugin_name . '_modelviewer', plugin_dir_url( __FILE__ ) . 'assets/js/wpcb-modelviewer.min.js', array( 'jquery' ), $this->version, true );
		wp_register_script( $this->plugin_name . '_qr_code', plugin_dir_url( __FILE__ ) . 'assets/js/wpcb-qr-code.min.js', array( 'jquery' ), $this->version, true );

		$min = defined('WCB_DEV_MODE') ? '' : '.min';

		wp_register_script( $this->plugin_name . '_fabric', plugin_dir_url( __FILE__ ) . 'assets/js/wpcb-fabric' . $min . '.js', array( 'jquery' ), $this->version, true );
		wp_register_script( $this->plugin_name . '_lightbox', plugin_dir_url( __FILE__ ) . 'assets/js/wpcb-lightbox' . $min . '.js', array( 'jquery' ), $this->version, true );
		wp_register_script( $this->plugin_name . '_public', plugin_dir_url( __FILE__ ) . 'assets/js/wpcb-public' . $min . '.js', array( 'jquery' ), $this->version, true );

		if ( isset( $_GET['page'] ) && 'wpcb_configuration_preview' === esc_attr( $_GET['page'] ) || wp_is_block_theme() ) {
			wp_localize_script( $this->plugin_name . '_public', 'wpcb_globals', $this->enqueue_scripts_vars() );
			wp_localize_script( $this->plugin_name . '_public', 'wpcb_vars', $this->enqueue_script_product_vars() );
		}

		do_action( 'wpcb_register_scripts' );

		if ( ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) || ( function_exists('woodmart_get_opt') && function_exists('is_product') && is_product() ) ) {
			$this->output_scripts();
		}
	}

	/**
	 * Actually output registered scripts to frontend.
	 *
	 * @since    1.0.0.
	 */
	public function output_scripts() {
		global $wpcb_configuration_id;
		if ( ! $wpcb_configuration_id ) {
			return; // Loading in TOO EARLY. Abort.
		}

		// Needed for each script instance.
		wp_localize_script( $this->plugin_name . '_public', 'wpcb_vars', $this->enqueue_script_product_vars() );

		if ( $this->scripts ) {
			return;
		}

		/**
		 * Styles
		 */
		$type = wpcb_get_option( 'wpcb_configuration_visual_type' );
		if ( get_post_meta( $wpcb_configuration_id, '_visual_type', true ) && 'inherit' !== get_post_meta( $wpcb_configuration_id, '_visual_type', true ) ) {
			$type = get_post_meta( $wpcb_configuration_id, '_visual_type', true );
		}

		if ( 'image' === $type ) {
			wp_enqueue_style( $this->plugin_name . '_slick' );
		}
		wp_enqueue_style( $this->plugin_name . '_lightbox' );

		do_action( 'wpcb_enqueue_styles' );

		wp_add_inline_style( $this->plugin_name . '_public', $this->enqueue_inline_styles() );
		wp_enqueue_style( $this->plugin_name . '_public' );

		/**
		 * Scripts
		 */
		wp_enqueue_script( $this->plugin_name . '_lightbox' );
		wp_enqueue_script( $this->plugin_name . '_fabric' );
		if ( 'image' === $type ) {
			wp_enqueue_script( $this->plugin_name . '_slick' );
		} else {
			wp_enqueue_script( $this->plugin_name . '_qr_code' );
			wp_enqueue_script( $this->plugin_name . '_modelviewer' );
		}

		do_action( 'wpcb_enqueue_scripts' );

		wp_localize_script( $this->plugin_name . '_public', 'wpcb_globals', $this->enqueue_scripts_vars() );

		wp_enqueue_script( $this->plugin_name . '_public' );

		$this->scripts = true;
	}

	/**
	 * Add module type to public script handles.
	 *
	 * @since    1.0.0
	 */
	public function set_module_script_loader_tag($tag, $handle, $src) {
		if ( 'wpcb_modelviewer' === $handle) {
			$tag = '<script type="module" src="' . esc_url($src) . '"></script>';
		}
		return $tag;
	}

	/**
	 * Add importmap for ThreeJS modules.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_threejs_with_importmap() {
		global $wpcb_configuration_id;
		if ( ! $wpcb_configuration_id ) {
			return; // Loading in TOO EARLY. Abort.
		}

		if ( 'model' !== wpcb_get_option( 'wpcb_configuration_visual_type' ) && 'model' !== get_post_meta( $wpcb_configuration_id, '_visual_type', true ) ) {
			return;
		}

		// Import map + module script
		$safe_url = esc_url( plugin_dir_url( __FILE__ ) . 'assets/js/wpcb-three.min.js' );
		?>
		<script type="importmap">
			{
				"imports": {
					"three": "<?php echo $safe_url; ?>"
				}
			}
		</script>
		<script type="module">
			import * as THREE from 'three';
			window.THREE = THREE;
		</script>
		<?php
	}

	/**
	 * Add body class for configurator page.
	 *
	 * @since    1.0.0
	 */
	public function set_body_configurator_class( $classes ) {
		global $wpcb_configuration_id;
		if ( ! $wpcb_configuration_id ) {
			return $classes;
		}

		if ( function_exists('is_product') && is_product() ) {
			$classes[] = 'wpcb-template-woocommerce';

			if ( 'fullwidth' === get_post_meta( $wpcb_configuration_id, '_template_width', true  ) ) {
				$classes[] = 'wpcb-template-width-full';
			}
		}
		if ( $wpcb_template = get_post_meta( $wpcb_configuration_id, '_template', true  ) ) {
			$classes[] = 'wpcb-template-' . $wpcb_template;
		}
		if ( $wpcb_height = get_post_meta( $wpcb_configuration_id, '_template_height', true ) ) {
			$classes[] = 'wpcb-template-height-' . $wpcb_height;
		}
		if ( $wpcb_sticky = get_post_meta( $wpcb_configuration_id, '_gallery_sticky', true ) ) {
			$classes[] = 'wpcb-template-gallery-' . $wpcb_sticky;
		}
		if ( $wpcb_border = wpcb_get_option( 'wpcb_border_style' ) ) {
			$classes[] = 'wpcb-border-' . $wpcb_border;
		}
		if ( is_rtl() ) {
			$classes[] = 'wpcb-is-rtl';
		}

		return $classes;
	}

	/**
	 * Register the configurator inline JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_inline_styles() {
		global $wpcb_configuration_id;
		if ( ! $wpcb_configuration_id ) {
			return ''; // Loading in TOO EARLY. Abort.
		}

		$main_background = wpcb_get_option( 'wpcb_primary_color' ) ?: '#fff';
		$main_accent = wpcb_get_option( 'wpcb_accent_color' ) ?: '#F4F5F7';
		$main_gallery = wpcb_get_option( 'wpcb_gallery_color' ) ?: '#fff';
		$main_border_color = wpcb_get_option( 'wpcb_template_border_color' ) ?: '#ccc';
		$main_title_color = wpcb_get_option( 'wpcb_template_title_color' ) ?: '#000';
		$main_color = wpcb_get_option( 'wpcb_template_color' ) ?: '#333';
		$main_link_color = wpcb_get_option( 'wpcb_template_link_color' ) ?: '#1456A5';
		$main_link_hover_color = wpcb_get_option( 'wpcb_template_link_hover_color' ) ?: '#063E81';

		$main_success = wpcb_get_option( 'wpcb_template_success_color' ) ?: '#43B830';
		$main_danger = wpcb_get_option( 'wpcb_template_warning_color' ) ?: '#E29933';
		$main_error = wpcb_get_option( 'wpcb_template_error_color' ) ?: '#E23333';

		$field_background = wpcb_get_option( 'wpcb_option_background_color' ) ?: '#fff';
		$field_color = wpcb_get_option( 'wpcb_option_text_color' ) ?: '#000';
		$field_border = wpcb_get_option( 'wpcb_option_border_color' ) ?: '#ccc';
		$field_hover_background = wpcb_get_option( 'wpcb_option_hover_background_color' ) ?: '#fff';
		$field_hover_color = wpcb_get_option( 'wpcb_option_hover_text_color' ) ?: '#000';
		$field_hover_border = wpcb_get_option( 'wpcb_option_hover_border_color' ) ?: '#1456A5';
		$field_active_background = wpcb_get_option( 'wpcb_option_active_background_color' ) ?: '#F4F5F7';
		$field_active_color = wpcb_get_option( 'wpcb_option_active_text_color' ) ?: '#000';
		$field_active_border = wpcb_get_option( 'wpcb_option_active_border_color' ) ?: '#1456A5';

		$main_button_background = wpcb_get_option( 'wpcb_button_background_color' ) ?: '#1456A5';
		$main_button_border = wpcb_get_option( 'wpcb_button_border_color' ) ?: '#1456A5';
		$main_button_color = wpcb_get_option( 'wpcb_button_text_color' ) ?: '#fff';
		$main_button_hover_background = wpcb_get_option( 'wpcb_button_hover_background_color' ) ?: '#063E81';
		$main_button_hover_border = wpcb_get_option( 'wpcb_button_hover_border_color' ) ?: '#063E81';
		$main_button_hover_color = wpcb_get_option( 'wpcb_button_hover_text_color' ) ?: '#fff';

		$button_background = wpcb_get_option( 'wpcb_button_alt_background_color' ) ?: '#F4F5F7';
		$button_border = wpcb_get_option( 'wpcb_button_alt_border_color' ) ?: '#F4F5F7';
		$button_color = wpcb_get_option( 'wpcb_button_alt_text_color' ) ?: '#1456A5';
		$button_hover_background = wpcb_get_option( 'wpcb_button_alt_hover_background_color' ) ?: '#E3E9F5';
		$button_hover_border = wpcb_get_option( 'wpcb_button_alt_hover_border_color' ) ?: '#E3E9F5';
		$button_hover_color = wpcb_get_option( 'wpcb_button_alt_hover_text_color' ) ?: '#1456A5';

		$stock_badge_background = wpcb_get_option( 'wpcb_stock_badge_background' ) ?: '#E29933';
		$stock_badge_border = wpcb_get_option( 'wpcb_stock_badge_border' ) ?: '#E29933';
		$stock_badge_color = wpcb_get_option( 'wpcb_stock_badge_color' ) ?: '#000';
		$no_stock_badge_background = wpcb_get_option( 'wpcb_no_stock_badge_background' ) ?: '#E23333';
		$no_stock_badge_border = wpcb_get_option( 'wpcb_no_stock_badge_border' ) ?: '#E23333';
		$no_stock_badge_color = wpcb_get_option( 'wpcb_no_stock_badge_color' ) ?: '#FFF';

		$notice_check_url = wpcb_get_icon_url('notice-check');
		$notice_error_url = wpcb_get_icon_url('notice-error');
		$spinner_url = wpcb_get_icon_url('spinner');
		$iconset = wpcb_get_option('wpcb_icon_set') == 'dark' ? 'light' : 'dark';
		$checkbox_url = wpcb_get_icon_url('checkbox', $iconset);

		$style = '
		:root {
			--wpcb-template-background: ' . esc_attr( $main_background ) . ';
			--wpcb-template-accent: ' . esc_attr( $main_accent ) . ';
			--wpcb-template-gallery: ' . esc_attr( $main_gallery ) . ';
			--wpcb-template-border-color: ' . esc_attr( $main_border_color ) . ';
			--wpcb-template-title-color: ' . esc_attr( $main_title_color ) . ';
			--wpcb-template-color: ' . esc_attr( $main_color ) . ';
			--wpcb-template-link-color: ' . esc_attr( $main_link_color ) . ';
			--wpcb-template-link-hover-color: ' . esc_attr( $main_link_hover_color ) . ';
			--wpcb-success: ' . esc_attr( $main_success ) . ';
			--wpcb-danger: ' . esc_attr( $main_danger ) . ';
			--wpcb-error: ' . esc_attr( $main_error ) . ';

			--wpcb-checkbox-url: url(' . esc_url( $checkbox_url ) . ');
			--wpcb-notice-check-url: url(' . esc_url( $notice_check_url ) . ');
			--wpcb-notice-error-url: url(' . esc_url( $notice_error_url ) . ');
			--wpcb-spinner-url: url(' . esc_url( $spinner_url ) . ');
	
			--wpcb-option-background: ' . esc_attr( $field_background ) . ';
			--wpcb-option-border: ' . esc_attr( $field_border ) . ';
			--wpcb-option-color: ' . esc_attr( $field_color ) . ';
			--wpcb-option-hover-background: ' . esc_attr( $field_hover_background ) . ';
			--wpcb-option-hover-border: ' . esc_attr( $field_hover_border ) . ';
			--wpcb-option-hover-color: ' . esc_attr( $field_hover_color ) . ';
			--wpcb-option-active-background: ' . esc_attr( $field_active_background ) . ';
			--wpcb-option-active-border: ' . esc_attr( $field_active_border ) . ';
			--wpcb-option-active-color: ' . esc_attr( $field_active_color ) . ';

			--wpcb-button-background: ' . esc_attr( $main_button_background ) . ';
			--wpcb-button-border: ' . esc_attr( $main_button_border ) . ';
			--wpcb-button-color: ' . esc_attr( $main_button_color ) . ';
			--wpcb-button-hover-background: ' . esc_attr( $main_button_hover_background ) . ';
			--wpcb-button-hover-border: ' . esc_attr( $main_button_hover_border ) . ';
			--wpcb-button-hover-color: ' . esc_attr( $main_button_hover_color ) . ';
			
			--wpcb-button-alt-background: ' . esc_attr( $button_background ) . ';
			--wpcb-button-alt-border: ' . esc_attr( $button_border ) . ';
			--wpcb-button-alt-color: ' . esc_attr( $button_color ) . ';
			--wpcb-button-alt-hover-background: ' . esc_attr( $button_hover_background ) . ';
			--wpcb-button-alt-hover-border: ' . esc_attr( $button_hover_border ) . ';
			--wpcb-button-alt-hover-color: ' . esc_attr( $button_hover_color ) . ';

			--wpcb-stock-badge-background: ' . esc_attr( $stock_badge_background ) . ';
			--wpcb-stock-badge-border: ' . esc_attr( $stock_badge_border ) . ';
			--wpcb-stock-badge-color: ' . esc_attr( $stock_badge_color ) . ';
			--wpcb-no-stock-badge-background: ' . esc_attr( $no_stock_badge_background ) . ';
			--wpcb-no-stock-badge-border: ' . esc_attr( $no_stock_badge_border ) . ';
			--wpcb-no-stock-badge-color: ' . esc_attr( $no_stock_badge_color ) . ';
		}';

		/**
		 * Custom Font CSS.
		 */
		
		$title_font = wpcb_get_option( 'wpcb_title_font_family' );
		if ( $title_font ) {
			$title_font_weight = wpcb_get_option( 'wpcb_title_font_weight' );
			$title_font_style = wpcb_get_option( 'wpcb_title_font_style' );
			$title_font_size = wpcb_get_option( 'wpcb_title_font_size' );
			$title_font_size_mb = wpcb_get_option( 'wpcb_title_font_size_mobile' );

			$style .= '
			.wpcb-configurator-product-title,
			.wpcb-configurator-form .wpcb-configurator-step-title {
				font-family: ' . $title_font . ';'
				. ( $title_font_weight ? ' font-weight: ' . $title_font_weight . ';' : '' )
				. ( $title_font_style ? ' font-style: ' . $title_font_style . ';' : '' )
				. '
			}

			.wpcb-configurator-form .wpcb-configurator-field-label,
			.wpcb-configurator-panel .wpcb-configurator-panel-label,
			.wpcb-configurator-popup .wpcb-configurator-popup-label {
				font-family: ' . $title_font . ';'
				. ( $title_font_weight ? ' font-weight: ' . $title_font_weight . ';' : '' )
				. ( $title_font_style ? ' font-style: ' . $title_font_style . ';' : '' )
				. ( $title_font_size ? ' font-size: ' . $title_font_size . ';' : '' )
				. '
			}';

			if ( $title_font_size_mb ) {
				$style .= '
				@media (max-width: 767px) {
					.wpcb-configurator-form .wpcb-configurator-field-label,
					.wpcb-configurator-panel .wpcb-configurator-panel-label,
					.wpcb-configurator-popup .wpcb-configurator-popup-label { font-size: ' . $title_font_size_mb . '; }
				}';
			}
		}

		$text_font = wpcb_get_option( 'wpcb_text_font_family' );
		if ( $text_font ) {
			$text_font_weight = wpcb_get_option( 'wpcb_text_font_weight' );
			$text_font_style = wpcb_get_option( 'wpcb_text_font_style' );
			$text_font_size = wpcb_get_option( 'wpcb_text_font_size' );
			$text_font_size_mb = wpcb_get_option( 'wpcb_text_font_size_mobile' );

			$style .= '
			.wpcb-configurator-product-price,
			.wpcb-configurator-panel-price,
			.wpcb-configurator-field-value,
			.wpcb-configurator-panel-content,
			.wpcb-configurator-popup-content,
			.wpcb-option-item p,
			.wpcb-option-summary p,
			.wpcb-dropdown select,
			.wpcb-input-content input,
			.wpcb-input-file-name,
			.wpcb-configurator-total-price,
			.wpcb-configurator-total-tax-label,
			.wpcb-configurator-total-stock {
				font-family: ' . $text_font . ';'
				. ( $text_font_weight ? ' font-weight: ' . $text_font_weight . ';' : '' )
				. ( $text_font_style ? ' font-style: ' . $text_font_style . ';' : '' )
				. ( $text_font_size ? ' font-size: ' . $text_font_size . ';' : '' )
				. '
			}
			.wpcb-option-stock {
				font-family: ' . $text_font . ';'
				. ( $text_font_weight ? ' font-weight: ' . $text_font_weight . ';' : '' )
				. ( $text_font_style ? ' font-style: ' . $text_font_style . ';' : '' )
				. '
			}';

			if ( $text_font_size_mb ) {
				$style .= '
				@media (max-width: 767px) {
					.wpcb-configurator-field-value,
					.wpcb-configurator-panel-content,
					.wpcb-configurator-popup-content,
					.wpcb-option-item p,
					.wpcb-option-summary p,
					.wpcb-dropdown select,
					.wpcb-input-content input,
					.wpcb-input-file-name,
					.wpcb-configurator-total-price,
					.wpcb-configurator-total-tax-label,
					.wpcb-configurator-total-stock { font-size: ' . $text_font_size_mb . '; }
				}';
			}
		}

		$note_font = wpcb_get_option( 'wpcb_note_font_family' );
		if ( $note_font ) {
			$note_font_weight = wpcb_get_option( 'wpcb_note_font_weight' );
			$note_font_style = wpcb_get_option( 'wpcb_note_font_style' );
			$note_font_size = wpcb_get_option( 'wpcb_note_font_size' );
			$note_font_size_mb = wpcb_get_option( 'wpcb_note_font_size_mobile' );

			$style .= '
			.wpcb-option-note {
				font-family: ' . $note_font . ';'
				. ( $note_font_weight ? ' font-weight: ' . $note_font_weight . ';' : '' )
				. ( $note_font_style ? ' font-style: ' . $note_font_style . ';' : '' )
				. ( $note_font_size ? ' font-size: ' . $note_font_size . ';' : '' )
				. '
			}';

			if ( $note_font_size_mb ) {
				$style .= '
				@media (max-width: 767px) {
					.wpcb-option-note { font-size: ' . $note_font_size_mb . '; }
				}';
			}
		}

		$button_font = wpcb_get_option( 'wpcb_button_font_family' );
		if ( $button_font ) {
			$button_font_weight = wpcb_get_option( 'wpcb_button_font_weight' );
			$button_font_style = wpcb_get_option( 'wpcb_button_font_style' );
			$button_font_size = wpcb_get_option( 'wpcb_button_font_size' );
			$button_font_size_mb = wpcb_get_option( 'wpcb_button_font_size_mobile' );

			$style .= '
			.wpcb-button,
			.wpcb-configurator-step-control-button,
			.wpcb-configurator-form-totals .wpcb-button,
			.wpcb-configurator-form-totals .wpcb-configurator-form-action-label,
			.wpcb-configurator-form-totals button.single_add_to_cart_button,
			.wpcb-configurator-form-control-actions .wpcb-configurator-form-action-label,
			.wpcb-configurator-form-actions-list .wpcb-configurator-form-action-label {
				font-family: ' . $button_font . ';'
				. ( $button_font_weight ? ' font-weight: ' . $button_font_weight . ';' : '' )
				. ( $button_font_style ? ' font-style: ' . $button_font_style . ';' : '' )
				. ( $button_font_size ? ' font-size: ' . $button_font_size . ';' : '' )
				. '
			}';

			if ( $button_font_size_mb ) {
				$style .= '
				@media (max-width: 767px) {
					.wpcb-button,
					.wpcb-configurator-step-control-button,
					.wpcb-configurator-form-totals-actions .single_add_to_cart_button,
					.wpcb-configurator-form-control-actions .wpcb-configurator-form-action-label,
					.wpcb-configurator-form-actions-list .wpcb-configurator-form-action-label { font-size: ' . $button_font_size_mb . '; }
				}';
			}
		}

		/**
		 * Custom CSS.
		 */
		$style .= wpcb_get_option( 'wpcb_custom_css' );

		return $style;
	}

	/**
	 * Register the configurator inline JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts_vars() {
		global $wpcb_configuration_id, $wpcb_price_rates;
		if ( ! $wpcb_configuration_id ) {
			return array(); // Loading in TOO EARLY. Abort.
		}

		if ( ! $wpcb_price_rates ) {
			wpcb_define_price_rates();
		}

		$suffix = wpcb_get_i18n_suffix();
		$inc_price_label = '';
		if ( wpcb_get_option( 'wpcb_step_set_included_option_text', $suffix ) ) {
			$inc_price_label = sanitize_text_field( wpcb_get_option( 'wpcb_step_included_option_text', $suffix ) );
		}

		$price_prefix = '';
		if (  wpcb_get_option( 'wpcb_extra_price_plus_prefix' ) ) {
			$price_prefix = '<span class="wpcb-plus-sign">+</span>';
		}

		$price_visible = ( 'no' === get_post_meta( $wpcb_configuration_id, '_show_price', true ) ? false : true );
		if ( ! is_user_logged_in() && wpcb_get_option( 'wpcb_show_prices_logged_in_users' ) ) {
			$price_visible = false;
		}

		$currency_symbol = wpcb_get_option( 'wpcb_price_currency_symbol', $suffix ) ?: '';
		$currency_pos    = wpcb_get_option( 'wpcb_price_currency_pos', $suffix ) ?: 'left_space';
		$thousand_sep    = wpcb_get_option( 'wpcb_price_thousand_sep', $suffix ) ?: ',';
		$decimal_sep     = wpcb_get_option( 'wpcb_price_decimal_sep', $suffix ) ?: '.';
		$decimal_num     = wpcb_get_option( 'wpcb_price_decimal_number', $suffix ) ?: 2;
		$tax_suffix       = wpcb_get_option( 'wpcb_product_tax_label', $suffix ) ?: '';
		$tax_label       = wpcb_get_option( 'wpcb_price_tax_total_text', $suffix ) ?: __( 'VAT', 'wcb-configurator-builder' );
		$subtotal_label  = wpcb_get_option( 'wpcb_price_subtotal_text', $suffix ) ?: __( 'Subtotal price', 'wcb-configurator-builder' );
		$weight_label    = wpcb_get_option( 'wpcb_weight_totals_label', $suffix ) ?: __( 'Weight', 'wcb-configurator-builder' );
		$weight_unit     = get_option( 'woocommerce_weight_unit' ) ?: 'kg';
		$weight_display  = 'none';
		if ( wpcb_get_option( 'wpcb_manage_weight' ) ) {
			$weight_display  = wpcb_get_option( 'wpcb_weight_totals_display' ) ?: 'none';
		}

		$email_prefix  = get_post_meta( $wpcb_configuration_id, '_email_prefix', true  ) ?: '';
		$email_suffix  = get_post_meta( $wpcb_configuration_id, '_email_postfix', true  ) ?: '';

		$image_width = wpcb_get_option( 'wpcb_image_export_width' ) ?: '';
		$image_type  = wpcb_get_option( 'wpcb_image_export_type' ) ?: 'png';

		$empty_collapsed_label = wpcb_get_option( 'wpcb_configuration_collapsed_empty_message', $suffix ) ?: __( 'Select a value', 'wcb-configurator-builder' );
		$empty_summary_label = wpcb_get_option( 'wpcb_configuration_summary_empty_message', $suffix ) ?:  __( 'Your selected options will show up here', 'wcb-configurator-builder' );
		$empty_wishlist_label = wpcb_get_option( 'wpcb_wishlist_empty_message', $suffix ) ?:  __( 'Products added to your wishlist will show up here', 'wcb-configurator-builder' );
		$analytics_type = wpcb_get_option( 'wpcb_analytics_type' ) ?: '';

		$table_product_label = wpcb_get_option( 'wpcb_price_product_total_text', $suffix ) ?: __( 'Product price', 'wcb-configurator-builder' );
		$table_option_label = wpcb_get_option( 'wpcb_price_option_total_text', $suffix ) ?: __( 'Option price', 'wcb-configurator-builder' ) ;
		$table_total_label = wpcb_get_option( 'wpcb_price_total_text', $suffix ) ?: __( 'Total price', 'wcb-configurator-builder' );

		$wpcb_load_icon   = wpcb_get_icon_url( 'load' );
		$wpcb_remove_icon = wpcb_get_icon_url( 'bin' );
		$wpcb_pdf_icon    = wpcb_get_icon_url( 'pdf' );
		$wpcb_link_icon   = wpcb_get_icon_url( 'link' );
		$wpcb_arrow_icon  = wpcb_get_icon_url( 'chevron-right' );
		$wpcb_plus_icon   = wpcb_get_icon_url( 'plus' );
		$wpcb_minus_icon  = wpcb_get_icon_url( 'minus' );

		$main_background  = wpcb_get_option( 'wpcb_primary_color' ) ?: '#fff';
		$main_button_background = wpcb_get_option( 'wpcb_button_background_color' ) ?: '#1456A5';

		$wpcb_globals = array(
			'ajax_url' => admin_url('admin-ajax.php'),
			'nonce' => wp_create_nonce('wpcb-ajax-nonce'),
			'settings' => array(
				'use_product_price' => ( wpcb_get_option( 'wpcb_exclude_product_base_prices' ) ? false : true ),
				'price_visible' => $price_visible,
				'price_tax_display' => esc_attr( $wpcb_price_rates['tax_display'] ),
				'price_tax_included' => ( 'no' !== $wpcb_price_rates['include_tax'] ? true : false ),
				'price_tax_display_both' => ( wpcb_get_option( 'wpcb_show_totals_incl_excl_tax' ) ? true : false ),
				'price_breakdown' => ( wpcb_get_option( 'wpcb_show_price_totals_breakdown' ) ? true : false ),
				'price_totals_single' => ( wpcb_get_option( 'wpcb_show_single_price_total' ) ? true : false ),
				'price_totals_inline' => ( wpcb_get_option( 'wpcb_show_price_totals_inline' ) ? true : false ),
				'price_option_difference' => ( wpcb_get_option( 'wpcb_show_price_option_difference' ) ? true : false ),
				'price_unit_min_base' => ( wpcb_get_option( 'wpcb_base_unit_price_on_min_value' ) ? true : false ),
				'summary_sku_visible' => ( wpcb_get_option( 'wpcb_summary_show_option_sku' ) ? true : false ),
				'enable_defaults' => ( wpcb_get_option( 'wpcb_load_default_options' ) ? true : false ),
				'uncheck_options' => ( wpcb_get_option( 'wpcb_allow_uncheck_options' ) ? true : false ),
				'enable_loader' => ( wpcb_get_option( 'wpcb_show_loader_indicator' ) ? true : false ),
				'track_options' => ( wpcb_get_option( 'wpcb_remember_conditional_options' ) ? true : false ),
				'collapse_attributes' => ( wpcb_get_option( 'wpcb_collapse_attribute_on_value' ) ? true : false ),
				'disable_urls' => ( wpcb_get_option( 'wpcb_url_option_disable_click' ) ? true : false ),
				'new_tab_urls' => ( wpcb_get_option( 'wpcb_url_option_new_tab' ) ? true : false ),
				'tax_row_display' => ( wpcb_get_option( 'wpcb_show_price_totals_tax_row' ) ? true : false ),
				'allow_backorders' => ( wpcb_get_option( 'wpcb_allow_backorders' ) ? true : false ),
				'bundle_quantity' => ( wpcb_get_option( 'wpcb_checkout_bundle_product_qty' ) ? true : false ),
				'show_leave_popup' => ( wpcb_get_option( 'wpcb_show_leave_popup' ) ? true : false ),
				'show_low_stock' => ( wpcb_get_option( 'wpcb_display_low_stock' ) ? true : false ),
				'low_stock_qty' => ( wpcb_get_option( 'wpcb_low_stock_qty' ) ? intval(wpcb_get_option( 'wpcb_low_stock_qty' )) : 999999 ),
				'icon_theme' => wpcb_get_option('wpcb_icon_set'),
				'currency_symbol' => esc_attr( $currency_symbol ),
				'currency_position' => esc_attr( $currency_pos ),
				'thousand_separator' => esc_attr( $thousand_sep ),
				'decimal_separator' => esc_attr( $decimal_sep ),
				'number_of_decimals' => intval( $decimal_num ),
				'image_width' => intval( $image_width ),
				'image_type' => intval( $image_type ),
			),
			'price_prefix' => esc_attr( $price_prefix ),
			'inc_price_label' => esc_attr( $inc_price_label ),
			'collapse_empty_label' => esc_attr( $empty_collapsed_label ),
			'summary_empty_label' => esc_attr( $empty_summary_label ),
			'wishlist_empty_label' => esc_attr( $empty_wishlist_label ),
			'tax_price_suffix' => esc_attr( $tax_suffix ),
			'email_body_prefix' => esc_attr( $email_prefix ),
			'email_body_suffix' => esc_attr( $email_suffix ),
			'analytics_type' => esc_attr( $analytics_type ),
			'table_product_label' => esc_attr( $table_product_label ),
			'table_option_label' => esc_attr( $table_option_label ),
			'table_total_label' => esc_attr( $table_total_label ),
			'table_subtotal_label' => esc_attr( $subtotal_label ),
			'table_tax_label' => esc_attr( $tax_label ),
			'table_weight_label' => esc_attr( $weight_label ),
			'weight_display' => esc_attr( $weight_display ),
			'weight_unit' => esc_attr( $weight_unit ),
			'icon_load' => esc_url( $wpcb_load_icon ),
			'icon_remove' => esc_url( $wpcb_remove_icon ),
			'icon_pdf' => esc_url( $wpcb_pdf_icon ),
			'icon_link' => esc_url( $wpcb_link_icon ),
			'icon_arrow' => esc_url( $wpcb_arrow_icon ),
			'icon_plus' => '<img src="' . esc_url( $wpcb_plus_icon ) . '" class="hotspot-plus">',
			'icon_minus' => '<img src="' . esc_url( $wpcb_minus_icon ) . '" class="hotspot-minus">',
			'base_color' => esc_url( $main_button_background ),
			'base_background' => esc_url( $main_background ),
			'border_circle' => ( 'square' !== wpcb_get_option('wpcb_border_style' ) ? true : false ),
			'error_required' => ( wpcb_get_option('wpcb_form_field_required_error' ) ?: __( 'This field is required', 'wcb-configurator-builder' ) ),
			'error_length' => ( wpcb_get_option('wpcb_form_field_length_error' ) ?: __( 'This field is required to have character length of min {min} and max {max}', 'wcb-configurator-builder' ) ),
			'error_value' => ( wpcb_get_option('wpcb_form_field_value_error' ) ?: __( 'This field is required to have value of min {min} and max {max}', 'wcb-configurator-builder' ) ),
			'error_selection' => '',
			'error_product' => '',
			'error_file' => '',
			'error_file_dims' => '',
		);

		if ( 'model' === wpcb_get_option( 'wpcb_configuration_visual_type' ) || 'model' === get_post_meta( $wpcb_configuration_id, '_visual_type', true ) ) {
			$base_nodes = get_post_meta( $wpcb_configuration_id, '_wpcb_model_base_nodes', true ) ?: false;
			$base_hotspots = get_post_meta( $wpcb_configuration_id, '_wpcb_model_base_hotspots', true ) ?: false;
			$all_hotspots = get_post_meta( $wpcb_configuration_id, '_wpcb_model_hotspots', true ) ?: array();

			$hotspots = array();
			if ( $all_hotspots ) {
				$label = wpcb_sanitize_title( get_the_title( $wpcb_configuration_id ) );
				foreach ( $all_hotspots as $index => $hotspot ) {
					$context = $label . '_model_hotspot_' . $index;
					$hotspots[] = [
						'id' => wpcb_get_value( $hotspot['hotspot_id'] ),
						'content' => wpcb__( trim( wpcb_get_value( $hotspot['hotspot_text'] ) ), $context ),
						'position' => wpcb_get_value( $hotspot['hotspot_target'] ),
						'normal' => wpcb_get_value( $hotspot['hotspot_normal'] ),
					];
				}
			}
			$wpcb_globals['hotspots'] = $hotspots;

			if ( $base_nodes ) {
				$base_nodes = array_map( 'trim', explode( ',', $base_nodes ) );
			} else {
				$base_nodes = array();
			}
			$wpcb_globals['base_nodes'] = $base_nodes;

			if ( $base_hotspots ) {
				$base_hotspots = array_map( 'trim', explode( ',', $base_hotspots ) );
			} else {
				$base_hotspots = array();
			}
			$wpcb_globals['base_hotspots'] = $base_hotspots;
			
			$wpcb_plus_icon = wpcb_get_icon_url( 'plus' );
			$wpcb_minus_icon = wpcb_get_icon_url( 'minus' );

			$wpcb_globals['icon_plus'] = '<img src="' . esc_url( $wpcb_plus_icon ) . '" class="hotspot-plus">';
			$wpcb_globals['icon_minus'] = '<img src="' . esc_url( $wpcb_minus_icon ) . '" class="hotspot-minus">';
		}

		$wpcb_globals = apply_filters( 'wpcb_configuration_public_vars', $wpcb_globals );

		return $wpcb_globals;
	}

	/**
	 * Register individual configurator product vars
	 *
	 * @since    1.0.0
	 */
	public function enqueue_script_product_vars() {
		global $wpcb_configuration_id, $wpcb_product_vars, $wpcb_price_rates;

		if ( ! $wpcb_product_vars || ! is_array( $wpcb_product_vars ) ) {
			$wpcb_product_vars = array();
		}

		if ( ! $wpcb_configuration_id ) {
			return $wpcb_product_vars; // Loading in TOO EARLY. Abort.
		}

		if ( ! $wpcb_price_rates ) {
			wpcb_define_price_rates();
		}

		$wpcb_details = wpcb_get_product_vars( $wpcb_configuration_id );

		$wpcb_product_vars[ $wpcb_configuration_id ] = [
			'configurator_id' => $wpcb_configuration_id,
			'product_id' => (int) $wpcb_details['id'],
			'product_name' => $wpcb_details['name'],
			'product_price' => floatval( $wpcb_details['price'] ),
			'product_sale_price' => floatval( $wpcb_details['sale_price'] ),
			'product_base_price' => floatval( $wpcb_details['base_price'] ),
			'product_tax_percent' => floatval( $wpcb_details['tax_percent'] ),
			'product_weight' => floatval( $wpcb_details['weight'] ),
			'product_stock' => floatval( $wpcb_details['stock'] ),
			'product_backorder' => $wpcb_details['backorder'],
			'product_quantity_name' => $wpcb_details['quantity_name'],
			'image_stack' => ( 'stacked' === esc_attr( get_post_meta( $wpcb_configuration_id, '_gallery_type', true ) ) ? true : false ),
			'show_final_step' => ( 'final' === get_post_meta( $wpcb_configuration_id, '_step_action', true ) ? true : false ),
		];

		$wpcb_product_vars = apply_filters( 'wpcb_configuration_public_product_vars', $wpcb_product_vars );

		return $wpcb_product_vars;
	}

	/**
	 * Register image sizes.
	 *
	 * @since    1.0.0
	 */
	public function register_image_sizes() {
		add_image_size( 'wpcb-icon', 24, 24, true );
		add_image_size( 'wpcb-thumbnail', 96, 96, true );
	}
}
