// source --> https://www.stonehengerecords.com/wp-content/plugins/elex-woo-flexible-pricing/assets/js/ajax-test.js?ver=10.7.0 
jQuery(document).ready(function ($) {

	// This event triggers only AFTER the default WooCommerce scripts have 
	// finished updating the variation UI (price, images, and availability).
	$(document).on('show_variation', '.variations_form', function (event, variation) {

		const var_id = variation.variation_id;
		if (!var_id) {
			return;
		}

		const data = {
			action: 'test_response',
			_ajax_nonce: the_ajax_script.elex_wfp_variation_nonce_token,
			var_id: var_id,
		};
		// the_ajax_script.ajaxurl is a variable that will contain the url to the ajax processing file
		$.post(the_ajax_script.ajaxurl, data, function (res) {
			const response = JSON.parse(res)
			if (response.general_flag !== 'yes') {
				return;
			}
			const min_price = response.min_value || 0;
			const add_to_cart_button_label = response.add_to_cart_label || "Add to cart";

			jQuery('.woocommerce-variation-price').find('.elex-set-min-price').remove();
			jQuery('.woocommerce-variation-price').append('<p></p>' + '<div class="wrap-validation elex-set-min-price" >' +
				'<label class="custom-min-price-validation"  for="custom_price_field_variation"> ' + response.label + ' (' + response.currency_symbol + ')' + '</label>' +
				'<input type="number" step="any" min="0" class="custom_price_field_variation_' + response.var_id + '" value="' + min_price + '" id="custom_price_field_variation_' + response.var_id + '" name="custom_price_field_variation_' + response.var_id + '"  />' +
				'<small class="description_product" >*' + response.desc + '</small></div>')
			jQuery(".single_add_to_cart_button").text(add_to_cart_button_label)
			return false;
		});
	});
});