jQuery(".single_variation_wrap").on("show_variation", function(event, variation){
let available_date=Date.parse(variation.preorder_date_time_filter) > Date.now();
if(variation.is_pre_order==true&&available_date){
jQuery('.single_add_to_cart_button').html(DBData.preorders_add_to_cart_text);
var preorderDate,
str,
dateValue=DBData.preorder_avaiable_date;
if(dateValue.includes('{date_format}')){
jQuery('.single_variation_wrap').find('.variation-date-text').remove();
str=DBData.preorder_avaiable_date;
preorderDate=str.replace("{date_format}", variation.preorder_date_time_format);
}else if(dateValue.includes('{human_readable}')){
jQuery('.single_variation_wrap').find('.variation-date-text').remove();
str=DBData.preorder_avaiable_date;
preorderDate=str.replace("{human_readable}", variation.preorder_date_human_time);
}else{
jQuery('.single_variation_wrap').find('.variation-date-text').remove();
str=DBData.preorder_avaiable_date;
preorderDate='';
let countdown_format=DBData.wc_preorders_countdown_format;
apply_countdown_for_variable_products(variation.preorder_date_time_filter, countdown_format, str);
}
jQuery('.single_variation_wrap').find('.variation-date-text').remove();
jQuery('.single_variation_wrap').find('.preorder-price-adjustment-details').remove();
if(DBData.position_preorder_avaiable_date==='before'){
jQuery('.woocommerce-variation-add-to-cart').before('<span class="variation-date-text  preorder-avaiable-date-text">' + preorderDate +'</span>');
if(variation.preorder_price_adjustment_label!=undefined){
jQuery('.woocommerce-variation-add-to-cart').before('<span class="preorder-price-adjustment-details">' + variation.preorder_price_adjustment_label +'</span>');
}}else{
jQuery('.woocommerce-variation-add-to-cart').after('<span class="variation-date-text  preorder-avaiable-date-text  bp-mt-20">' + preorderDate +'</span>');
if(variation.preorder_price_adjustment_label!=undefined){
jQuery('.woocommerce-variation-add-to-cart').after('<span class="preorder-price-adjustment-details">' + variation.preorder_price_adjustment_label +'</span>');
}
jQuery('.woocommerce-variation-add-to-cart').css('padding-top', '0');
}}else{
jQuery('.single_add_to_cart_button').html(DBData.default_add_to_cart_text);
jQuery('.single_variation_wrap').find('.variation-date-text').remove();
jQuery('.single_variation_wrap').find('.preorder-price-adjustment-details').remove();
}
if(DBData.is_checkbox_active_spp!=undefined&&DBData.is_checkbox_active_spp!='yes'){
jQuery('span.variation-date-text').remove();
}});
function apply_countdown_for_variable_products(date, countdown_format, preorder_avaiable_date){
let date_parts=date.split('/');
let countDownDate=Date.UTC(date_parts[2], date_parts[0] - 1, date_parts[1]);
countDownDate=Date.parse(new Date(countDownDate).toISOString());
document.querySelector(".variation-date-text.preorder-avaiable-date-text");
let x=setInterval(function(){
let now=new Date().getTime();
let countdown_string='';
let distance=countDownDate - now;
let days=Math.floor(distance / (1000 * 60 * 60 * 24));
let hours=Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes=Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
let seconds=Math.floor((distance % (1000 * 60)) / 1000);
countdown_string=(countdown_format.includes("%d")) ? countdown_string + days + " days":countdown_string;
countdown_string=(countdown_format.includes("%h")) ? countdown_string + " "+ hours + " hours":countdown_string;
countdown_string=(countdown_format.includes("%i")) ? countdown_string + " "+ minutes + " minutes":countdown_string;
countdown_string=(countdown_format.includes("%s")) ? countdown_string + " "+ seconds + " seconds":countdown_string;
let preorderDate=preorder_avaiable_date.replace("{countdown}", countdown_string);
document.querySelector(".variation-date-text.preorder-avaiable-date-text").innerHTML=preorderDate;
}, 1000);
};