(function($){
'use strict';
jQuery(document).ready(function(){
var timer;
var save_custom_fields=cartbounty_co.save_custom_fields;
var custom_checkout_fields=cartbounty_co.checkout_fields;
var custom_email_selectors=cartbounty_co.custom_email_selectors;
var custom_phone_selectors=cartbounty_co.custom_phone_selectors;
var custom_button_selectors=cartbounty_co.custom_button_selectors;
var contact_saved=localStorage.getItem('cartbounty_contact_saved');
function getCheckoutData(){
let email=jQuery('#email').val()||jQuery('#billing_email').val()||'';
let phone=jQuery('#billing-phone').val()||jQuery('#shipping-phone').val()||jQuery('#billing_phone').val()||'';
if(email.length > 0||phone.length > 0){
let checkoutFields={};
jQuery(custom_checkout_fields).each(function (){
let field=jQuery(this);
let fieldId=field.attr('id');
let fieldValue=field.val();
if(field.is('textarea')&&field.closest('#order-notes').length > 0){
fieldId='order_notes';
}else if(field.is(':checkbox')){
fieldValue=field.is(':checked') ? 1:0;
}
checkoutFields[fieldId]=fieldValue;
});
clearTimeout(timer);
var emailValidation=cartbounty_co.email_validation;
var phoneValidation=cartbounty_co.phone_validation;
if(email.match(emailValidation)||phone.match(phoneValidation)){
timer=setTimeout(function(){
let data={
action:				"cartbounty_save",
nonce:				cartbounty_co.nonce,
customer:			checkoutFields,
}
jQuery.post(cartbounty_co.ajaxurl, data,
function(response){
if(response.success){
localStorage.setItem('cartbounty_contact_saved', true);
removeExitIntentForm();
}});
}, 600);
}}
}
function saveCustomField(){
var custom_field_selector=jQuery(this);
var cartbounty_contact_saved=localStorage.getItem('cartbounty_contact_saved');
if(cartbounty_contact_saved){
return;
}
if(jQuery(custom_field_selector).length > 0&&!contact_saved){
var cartbounty_custom_field=jQuery(custom_field_selector).val()||'';
var emailValidation=cartbounty_co.email_validation;
var phoneValidation=cartbounty_co.phone_validation;
if(cartbounty_custom_field!=''){
if(cartbounty_custom_field.match(emailValidation)){
localStorage.setItem('cartbounty_custom_email', cartbounty_custom_field);
}else if(cartbounty_custom_field.match(phoneValidation)){
localStorage.setItem('cartbounty_custom_phone', cartbounty_custom_field);
}}
}}
function passCustomFieldToCartBounty(){
var cartbounty_custom_email_stored=localStorage.getItem('cartbounty_custom_email');
var cartbounty_custom_phone_stored=localStorage.getItem('cartbounty_custom_phone');
var cartbounty_contact_saved=localStorage.getItem('cartbounty_contact_saved');
if(( cartbounty_custom_email_stored==null&&cartbounty_custom_phone_stored==null)||cartbounty_contact_saved){
return;
}
var fields={
email: 			cartbounty_custom_email_stored,
phone: 			cartbounty_custom_phone_stored,
};
var data={
action:			"cartbounty_save",
source:			"cartbounty_custom_field",
nonce:			cartbounty_co.nonce,
customer:		fields,
}
jQuery.post(cartbounty_co.ajaxurl, data,
function(response){
if(response.success){
localStorage.setItem('cartbounty_contact_saved', true);
removeCustomFields();
removeExitIntentForm();
jQuery(document).off('added_to_cart', passCustomFieldToCartBounty);
}});
}
function removeCustomFields(){
localStorage.removeItem('cartbounty_custom_email');
localStorage.removeItem('cartbounty_custom_phone');
}
function removeExitIntentForm(){
if(jQuery('#cartbounty-exit-intent-form').length > 0){
jQuery('#cartbounty-exit-intent-form').remove();
jQuery('#cartbounty-exit-intent-form-backdrop').remove();
}}
function appendHiddenInputField(){
var $target=jQuery(this);
var customInputField=jQuery('<input>',{
type: 'hidden',
name: 'cartbounty_bot_test',
value: '1'
});
var $form=$target.closest('form.cart');
if($form.length){
$form.append(customInputField);
}else{
$target.data('cartbounty_bot_test', '1');
}}
function addCartBountyInputDataToAjax(e, xhr, settings){
if(settings.url.indexOf('wc-ajax=add_to_cart') > -1){
settings.data +='&cartbounty_bot_test=1';
}};
function saveBotTestResult(){
localStorage.setItem('cartbounty_bot_test_passed', true);
}
function saveAnonymousCart(){
let bot_test_passed=localStorage.getItem('cartbounty_bot_test_passed');
if(bot_test_passed){
var data={
action:			"cartbounty_save",
source:			"cartbounty_anonymous_bot_test",
nonce:			cartbounty_co.nonce
}
setTimeout(function(){
jQuery.post(cartbounty_co.ajaxurl, data,
function(response){
if(response.success){
localStorage.removeItem('cartbounty_bot_test_passed');
}});
}, 1000);
}}
jQuery('.wc-block-checkout, .woocommerce-checkout').on('keyup keypress change', 'input, textarea, select', getCheckoutData);
jQuery(window).on('load', getCheckoutData);
jQuery(document).ajaxSend(addCartBountyInputDataToAjax);
jQuery(document).on('click', custom_button_selectors, appendHiddenInputField);
jQuery(document).on('click', custom_button_selectors, saveBotTestResult);
if(( save_custom_fields&&!contact_saved) ){
passCustomFieldToCartBounty();
jQuery(document).on('keyup keypress change', custom_email_selectors + ', ' + custom_phone_selectors, saveCustomField);
jQuery(document).on('added_to_cart', passCustomFieldToCartBounty);
}
saveAnonymousCart();
});
})(jQuery);