/**
 * This file adds some LIVE to the Theme Customizer live preview. To leverage
 * this, set your custom settings to 'postMessage' and then add your handling
 * here. Your javascript should grab settings from customizer controls, and
 * then make any necessary changes to the page using jQuery.
 *
 * @package Astra
 */
/**
 * Generate font size in PX & REM
 */
function astra_font_size_rem( size, with_rem, device ) {
	var css = '';
	if( size != '' ) {
		var device = ( typeof device !== 'undefined' ) ? device : 'desktop';
		// font size with 'px'.
		css = 'font-size: ' + size + 'px;';
		// font size with 'rem'.
		if ( with_rem ) {
			var body_font_size = wp.customize( 'astra-settings[font-size-body]' ).get();
			body_font_size['desktop'] 	= ( body_font_size['desktop'] != '' ) ? body_font_size['desktop'] : 15;
			body_font_size['tablet'] 	= ( body_font_size['tablet'] != '' ) ? body_font_size['tablet'] : body_font_size['desktop'];
			body_font_size['mobile'] 	= ( body_font_size['mobile'] != '' ) ? body_font_size['mobile'] : body_font_size['tablet'];
			if ( body_font_size[ device + '-unit' ] === 'px' ) {
				css += 'font-size: ' + ( size / body_font_size[device] ) + 'rem;';
			}
		}
	}
	return css;
}
/**
 * Refresh customizer iframe.
 */
function astra_refresh_customizer( control ) {
	wp.customize( control, function( value ) {
		value.bind( function( value ) {
			wp.customize.preview.send( 'refresh' );
		} );
	} );
}
/**
 * Apply CSS for the element
 */
function astra_color_responsive_css( addon, control, css_property, selector ) {
	wp.customize( control, function( value ) {
		value.bind( function( value ) {
			if ( value.desktop || value.mobile || value.tablet ) {
				// Remove '
				);
			} else {
				jQuery( 'style#' + control + '-' + addon ).remove();
			}
		} );
	} );
}
/**
 * Responsive Font Size CSS
 */
function astra_responsive_font_size( control, selector, dispatchCustomEvent = false, styleGuideSelector = '' ) {
	wp.customize( control, function( value ) {
		value.bind( function( value ) {
			if ( value.desktop || value.mobile || value.tablet ) {
				// Remove '
				);
				if ( dispatchCustomEvent ) {
					let styleGuideUpdatedEvent = new CustomEvent( 'AstraStyleGuideElementUpdated', {
						'detail': {
							'value': {
								'desktop': value.desktop + ( undefined == value['desktop-unit'] ? 'px' : value['desktop-unit'] ),
								'tablet': value.tablet + ( undefined == value['tablet-unit'] ? 'px' : value['tablet-unit'] ),
								'mobile': value.mobile + ( undefined == value['mobile-unit'] ? 'px' : value['mobile-unit'] )
							},
							'selector': styleGuideSelector
						}
					});
					document.dispatchEvent(styleGuideUpdatedEvent);
				}
			} else {
				jQuery( 'style#' + control  + '-font-size' ).remove();
			}
		} );
	} );
}
/**
 * Font extras control dynamic CSS.
 *
 * @since x.x.x
 */
function astra_font_extras_css( control, selector ) {
	wp.customize( 'astra-settings[' + control + ']', function( value ) {
		value.bind( function( data ) {
			if ( data ) {
				// Remove '
				);
			} else {
				jQuery( 'style#' + control ).remove();
			}
		} );
	} );
}
/**
 * Responsive Spacing CSS
 */
function astra_responsive_spacing( control, selector, type, side ) {
	wp.customize( control, function( value ) {
		value.bind( function( value ) {
			var sidesString = "";
			var spacingType = "padding";
			if ( value.desktop.top || value.desktop.right || value.desktop.bottom || value.desktop.left || value.tablet.top || value.tablet.right || value.tablet.bottom || value.tablet.left || value.mobile.top || value.mobile.right || value.mobile.bottom || value.mobile.left ) {
				if ( typeof side !== 'undefined' ) {
					sidesString = side + "";
					// Replace comma character with dash, necessary to separate out spacing dimensions.
					sidesString = sidesString.replace(/,/g , "-");
				}
				if ( typeof type !== 'undefined' ) {
					spacingType = type + "";
				}
				// Remove '
				);
			} else {
				wp.customize.preview.send( 'refresh' );
				jQuery( 'style#' + control + '-' + spacingType + '-' + sidesString ).remove();
			}
		} );
	} );
}
/**
 * CSS
 */
function astra_css_font_size( control, selector ) {
	wp.customize( control, function( value ) {
		value.bind( function( size ) {
			if ( size ) {
				// Remove '
				);
			} else {
				jQuery( 'style#' + control ).remove();
			}
		} );
	} );
}
/**
 * Return get_hexdec()
 */
function get_hexdec( hex ) {
	var hexString = hex.toString( 16 );
	return parseInt( hexString, 16 );
}
/**
 * Apply CSS for the element
 */
function astra_css( control, css_property, selector, unit, important = false ) {
	wp.customize( control, function( value ) {
		value.bind( function( new_value ) {
			// Remove '
				);
				if( 'unset' === new_value ){
					jQuery( 'style#' + control + '-' + css_property ).remove();
				}
			} else {
				// Remove old.
				jQuery( 'style#' + control + '-' + css_property ).remove();
			}
		} );
	} );
}
/**
 * Dynamic Internal/Embedded Style for a Control
 */
function astra_add_dynamic_css( control, style ) {
	control = control.replace( '[', '-' );
	control = control.replace( ']', '' );
	jQuery( 'style#' + control ).remove();
	jQuery( 'head' ).append(
		''
	);
}
/**
 * Generate background_obj CSS
 */
function astra_background_obj_css( wp_customize, bg_obj, ctrl_name, style ) {
	var gen_bg_css 	= '';
	var bg_img		= bg_obj['background-image'];
	var bg_color	= bg_obj['background-color'];
	if( '' === bg_color && '' === bg_img ) {
		jQuery( 'style#' + ctrl_name ).remove();
	}else{
		if( undefined !== bg_obj['background-type'] && '' !== bg_obj['background-type'] ) {
			if ( ( 'color' === bg_obj['background-type'] ) ) {
				if ( '' !== bg_img && '' !== bg_color && undefined !== bg_color && 'unset' !== bg_color ) {
					gen_bg_css = 'background-image: linear-gradient(to right, ' + bg_color + ', ' + bg_color + '), url(' + bg_img + ');';
				}  else if ( undefined === bg_img || '' === bg_img || 'unset' === bg_img ) {
					gen_bg_css = 'background-color: ' + bg_color + ';';
				}
			} else if ( 'image' === bg_obj['background-type'] ) {
				if ( '' !== bg_img ) {
					if ( 'overlay-type' in bg_obj && 'none' !== bg_obj['overlay-type'] ) {
						let overlay_color	= 'overlay-color' in bg_obj ? bg_obj['overlay-color'] : '';
						let overlay_opacity	= 'overlay-opacity' in bg_obj ? bg_obj['overlay-opacity'] : '';
						let overlay_gradient	= 'overlay-gradient' in bg_obj ? bg_obj['overlay-gradient'] : '';
						if ( 'classic' === bg_obj['overlay-type'] && '' !== overlay_color ) {
							if ( '' !== overlay_opacity ) {
								wp.customize.preview.send( 'refresh' );
								return;
							}
							gen_bg_css = 'background-image: linear-gradient(to right, ' + bg_obj['overlay-color'] + ', ' + bg_obj['overlay-color'] + '), url(' + bg_img + ');';
						} else if ( 'gradient' === bg_obj['overlay-type'] && '' !== overlay_gradient ) {
							gen_bg_css = 'background-image: ' + overlay_gradient + ', url(' + bg_img + ');';
						} else {
							gen_bg_css = 'background-image: url(' + bg_img + ');';
						}
					} else {
						gen_bg_css = 'background-image: url(' + bg_img + ');';
					}
				}
			} else if ( 'gradient' === bg_obj['background-type'] ) {
				if ( '' !== bg_color && 'unset' !== bg_color ) {
					gen_bg_css = 'background-image: ' + bg_color + ';';
				}
			}
		}
		if ( '' !== bg_img ) {
			gen_bg_css += 'background-repeat: ' + bg_obj['background-repeat'] + ';';
			gen_bg_css += 'background-position: ' + bg_obj['background-position'] + ';';
			gen_bg_css += 'background-size: ' + bg_obj['background-size'] + ';';
			gen_bg_css += 'background-attachment: ' + bg_obj['background-attachment'] + ';';
		}
		var dynamicStyle = style.replace( "{{css}}", gen_bg_css );
		astra_add_dynamic_css( ctrl_name, dynamicStyle );
	}
}
/*
* Generate Font Family CSS
*/
function astra_generate_outside_font_family_css( control, selector, dispatchCustomEvent = false, styleGuideSelector = '') {
	wp.customize( control, function (value) {
		value.bind( function ( value, oldValue ) {
			var cssProperty = 'font-family';
			var link = '';
			var fontName = value.split(",")[0];
			// Replace ' character with space, necessary to separate out font prop value.
			fontName = fontName.replace(/'/g, '');
			// Remove '
				+ link
			);
		});
	});
}
/**
 * Apply Advanced CSS for the element
 *
 * @param string section Section ID.
 * @param string selector Base Selector.
 */
function astra_builder_advanced_css( section, selector ) {
    var tablet_break_point    = 921,
		mobile_break_point    = 544;
    // Padding.
    wp.customize( 'astra-settings[' + section + '-padding]', function( value ) {
        value.bind( function( padding ) {
			if( ! padding.hasOwnProperty('desktop') ) {
				return
			}
            if(
                padding.desktop.bottom != '' || padding.desktop.top != '' || padding.desktop.left != '' || padding.desktop.right != '' ||
                padding.tablet.bottom != '' || padding.tablet.top != '' || padding.tablet.left != '' || padding.tablet.right != '' ||
                padding.mobile.bottom != '' || padding.mobile.top != '' || padding.mobile.left != '' || padding.mobile.right != ''
            ) {
                var dynamicStyle = '';
                dynamicStyle += selector + ' {';
                dynamicStyle += 'padding-left: ' + padding['desktop']['left'] + padding['desktop-unit'] + ';';
                dynamicStyle += 'padding-right: ' + padding['desktop']['right'] + padding['desktop-unit'] + ';';
                dynamicStyle += 'padding-top: ' + padding['desktop']['top'] + padding['desktop-unit'] + ';';
                dynamicStyle += 'padding-bottom: ' + padding['desktop']['bottom'] + padding['desktop-unit'] + ';';
                dynamicStyle += '} ';
                dynamicStyle +=  '@media (max-width: ' + tablet_break_point + 'px) {';
                dynamicStyle += selector + ' {';
                dynamicStyle += 'padding-left: ' + padding['tablet']['left'] + padding['tablet-unit'] + ';';
                dynamicStyle += 'padding-right: ' + padding['tablet']['right'] + padding['tablet-unit'] + ';';
                dynamicStyle += 'padding-top: ' + padding['tablet']['top'] + padding['tablet-unit'] + ';';
                dynamicStyle += 'padding-bottom: ' + padding['tablet']['bottom'] + padding['tablet-unit'] + ';';
                dynamicStyle += '} ';
                dynamicStyle += '} ';
                dynamicStyle +=  '@media (max-width: ' + mobile_break_point + 'px) {';
                dynamicStyle += selector + ' {';
                dynamicStyle += 'padding-left: ' + padding['mobile']['left'] + padding['mobile-unit'] + ';';
                dynamicStyle += 'padding-right: ' + padding['mobile']['right'] + padding['mobile-unit'] + ';';
                dynamicStyle += 'padding-top: ' + padding['mobile']['top'] + padding['mobile-unit'] + ';';
                dynamicStyle += 'padding-bottom: ' + padding['mobile']['bottom'] + padding['mobile-unit'] + ';';
                dynamicStyle += '} ';
                dynamicStyle += '} ';
                astra_add_dynamic_css( section + '-padding-toggle-button', dynamicStyle );
            } else {
                astra_add_dynamic_css( section + '-padding-toggle-button', '' );
            }
        } );
    } );
    // Margin.
    wp.customize( 'astra-settings[' + section + '-margin]', function( value ) {
        value.bind( function( margin ) {
        	if( ! margin.hasOwnProperty('desktop') ) {
        		return
			}
            if(
                margin.desktop.bottom != '' || margin.desktop.top != '' || margin.desktop.left != '' || margin.desktop.right != '' ||
                margin.tablet.bottom != '' || margin.tablet.top != '' || margin.tablet.left != '' || margin.tablet.right != '' ||
                margin.mobile.bottom != '' || margin.mobile.top != '' || margin.mobile.left != '' || margin.mobile.right != ''
            ) {
                var dynamicStyle = '';
                dynamicStyle += selector + ' {';
                dynamicStyle += 'margin-left: ' + margin['desktop']['left'] + margin['desktop-unit'] + ';';
                dynamicStyle += 'margin-right: ' + margin['desktop']['right'] + margin['desktop-unit'] + ';';
                dynamicStyle += 'margin-top: ' + margin['desktop']['top'] + margin['desktop-unit'] + ';';
                dynamicStyle += 'margin-bottom: ' + margin['desktop']['bottom'] + margin['desktop-unit'] + ';';
                dynamicStyle += '} ';
                dynamicStyle +=  '@media (max-width: ' + tablet_break_point + 'px) {';
                dynamicStyle += selector + ' {';
                dynamicStyle += 'margin-left: ' + margin['tablet']['left'] + margin['tablet-unit'] + ';';
                dynamicStyle += 'margin-right: ' + margin['tablet']['right'] + margin['tablet-unit'] + ';';
                dynamicStyle += 'margin-top: ' + margin['tablet']['top'] + margin['tablet-unit'] + ';';
                dynamicStyle += 'margin-bottom: ' + margin['tablet']['bottom'] + margin['tablet-unit'] + ';';
                dynamicStyle += '} ';
                dynamicStyle += '} ';
                dynamicStyle +=  '@media (max-width: ' + mobile_break_point + 'px) {';
                dynamicStyle += selector + ' {';
                dynamicStyle += 'margin-left: ' + margin['mobile']['left'] + margin['mobile-unit'] + ';';
                dynamicStyle += 'margin-right: ' + margin['mobile']['right'] + margin['mobile-unit'] + ';';
                dynamicStyle += 'margin-top: ' + margin['mobile']['top'] + margin['mobile-unit'] + ';';
                dynamicStyle += 'margin-bottom: ' + margin['mobile']['bottom'] + margin['mobile-unit'] + ';';
                dynamicStyle += '} ';
                dynamicStyle += '} ';
                astra_add_dynamic_css( section + '-margin-toggle-button', dynamicStyle );
            } else {
                astra_add_dynamic_css( section + '-margin-toggle-button', '' );
            }
        } );
    } );
}
/**
 * Apply Advanced CSS for the element
 *
 * @param string section Section ID.
 * @param string selector Base Selector.
 */
function astra_border_spacing_advanced_css( section, selector ) {
	wp.customize( 'astra-settings[' + section + '-border-width]', function( setting ) {
		setting.bind( function( border ) {
			var dynamicStyle = selector + ' {';
			if ( border.top ) {
				dynamicStyle += 'border-top-style: solid;';
				dynamicStyle += 'border-top-width: ' + border.top + 'px;';
			}
			if ( border.right ) {
				dynamicStyle += 'border-right-style: solid;';
				dynamicStyle += 'border-right-width: ' + border.right + 'px;';
			}
			if ( border.bottom ) {
				dynamicStyle += 'border-bottom-style: solid;';
				dynamicStyle += 'border-bottom-width: ' + border.bottom + 'px;';
			}
			if ( border.left ) {
				dynamicStyle += 'border-left-style: solid;';
				dynamicStyle += 'border-left-width: ' + border.left + 'px;';
			}
			dynamicStyle += '} ';
			astra_add_dynamic_css( 'astra-settings[' + section + '-border-width]', dynamicStyle );
		} );
	} );
	wp.customize( 'astra-settings[' + section + '-border-radius]', function( setting ) {
		setting.bind( function( border ) {
			if ( !border.top && !border.right && !border.bottom && !border.left ) {
				wp.customize.preview.send( 'refresh' );
				return;
			}
			var dynamicStyle = selector + ' {';
			if ( border.top !== '' ) {
				dynamicStyle += 'border-top-left-radius: ' + border.top + 'px;';
			}
			if ( border.right !== '' ) {
				dynamicStyle += 'border-top-right-radius: ' + border.right + 'px;';
			}
			if ( border.left !== '' ) {
				dynamicStyle += 'border-bottom-left-radius: ' + border.left + 'px;';
			}
			if ( border.bottom !== '' ) {
				dynamicStyle += 'border-bottom-right-radius: ' + border.bottom + 'px;';
			}
			dynamicStyle += '} ';
			astra_add_dynamic_css( 'astra-settings[' + section + '-border-radius]', dynamicStyle );
		} );
	} );
	astra_css( 'astra-settings[' + section + '-border-color]', 'border-color', selector );
	astra_builder_advanced_css( section, selector );
}
/*
* Generate Font Weight CSS
*/
function astra_generate_font_weight_css( font_control, control, css_property, selector ) {
	wp.customize( control, function( value ) {
		value.bind( function( new_value ) {
			control = control.replace( '[', '-' );
			control = control.replace( ']', '' );
			var link = '';
			if ( new_value ) {
				/**
				 *	If ( unit == 'url' ) then = url('{VALUE}')
				 *	If ( unit == 'px' ) then = {VALUE}px
				 *	If ( unit == 'em' ) then = {VALUE}em
				 *	If ( unit == 'rem' ) then = {VALUE}rem.
				 */
				if ( 'undefined' != typeof unit) {
					if ( 'url' === unit ) {
						new_value = 'url(' + new_value + ')';
					} else {
						new_value = new_value + unit;
					}
				}
				var fontName = wp.customize._value[font_control]._value;
				fontName = fontName.split(',');
				fontName = fontName[0].replace( /'/g, '' );
				// Remove old.
				jQuery( 'style#' + control + '-' + css_property ).remove();
				if ( fontName in astraCustomizer.googleFonts ) {
					// Remove old.
					jQuery('#' + font_control).remove();
					if( new_value === "inherit" ) {
						link = '';
					} else {
						link = '';
					}
				}
				// Concat and append new '
					+ link
				);
			} else {
				// Remove old.
				jQuery( 'style#' + control ).remove();
			}
		} );
	});
}
/**
 * Apply CSS for the element
 */
function astra_apply_responsive_background_css( control, selector, device, singleColorSelector, addon ) {
	wp.customize( control, function( value ) {
		value.bind( function( bg_obj ) {
			addon = addon || '';
			singleColorSelector = singleColorSelector || '';
			addon = ( addon ) ? addon : 'header';
			control = control.replace( '[', '-' );
			control = control.replace( ']', '' );
			if( '' === bg_obj[device] || undefined === bg_obj[device] ){
				return;
			}
			var gen_bg_css 	= '';
			var bg_img		= bg_obj[device]['background-image'];
			var bg_tab_img	= bg_obj['tablet']['background-image'];
			var bg_desk_img	= bg_obj['desktop']['background-image'];
			var bg_color	= bg_obj[device]['background-color'];
			var tablet_css  = ( bg_obj['tablet']['background-image'] ) ? true : false;
			var desktop_css = ( bg_obj['desktop']['background-image'] ) ? true : false;
			if( undefined !== bg_obj[device]['background-type'] && '' !== bg_obj[device]['background-type'] ) {
				if ( ( 'color' === bg_obj[device]['background-type'] ) ) {
					if ( '' !== bg_img && '' !== bg_color && undefined !== bg_color && 'unset' !== bg_color ) {
						gen_bg_css = 'background-image: linear-gradient(to right, ' + bg_color + ', ' + bg_color + '), url(' + bg_img + ');';
					} else if ( 'mobile' === device ) {
						if ( desktop_css ) {
							gen_bg_css = 'background-image: linear-gradient(to right, ' + bg_color + ', ' + bg_color + '), url(' + bg_desk_img + ');';
						} else if ( tablet_css ) {
							gen_bg_css = 'background-image: linear-gradient(to right, ' + bg_color + ', ' + bg_color + '), url(' + bg_tab_img + ');';
						} else {
							gen_bg_css = 'background-color: ' + bg_color + ';';
							gen_bg_css += 'background-image: none;';
						}
					} else if ( 'tablet' === device ) {
						if ( desktop_css ) {
							gen_bg_css = 'background-image: linear-gradient(to right, ' + bg_color + ', ' + bg_color + '), url(' + bg_desk_img + ');';
						} else {
							gen_bg_css = 'background-color: ' + bg_color + ';';
							gen_bg_css += 'background-image: none;';
						}
					} else if ( undefined === bg_img || '' === bg_img ) {
						gen_bg_css = 'background-color: ' + bg_color + ';';
						gen_bg_css += 'background-image: none;';
					}
				} else if ( 'image' === bg_obj[device]['background-type'] ) {
					if ( '' !== bg_img ) {
						if ( undefined !== bg_obj[device]['overlay-type'] && 'none' !== bg_obj[device]['overlay-type'] ) {
							let overlay_color	= undefined !== bg_obj[device]['overlay-color'] ? bg_obj[device]['overlay-color'] : '';
							let overlay_opacity	= undefined !== bg_obj[device]['overlay-opacity'] ? bg_obj[device]['overlay-opacity'] : '';
							let overlay_gradient	= undefined !== bg_obj[device]['overlay-gradient'] ? bg_obj[device]['overlay-gradient'] : '';
							if ( 'classic' === bg_obj[device]['overlay-type'] && '' !== overlay_color ) {
								if ( '' !== overlay_opacity ) {
									wp.customize.preview.send( 'refresh' );
									return;
								}
								gen_bg_css = 'background-image: linear-gradient(to right, ' + overlay_color + ', ' + overlay_color + '), url(' + bg_img + ');';
							} else if ( 'gradient' === bg_obj[device]['overlay-type'] && '' !== overlay_gradient ) {
								gen_bg_css = 'background-image: ' + overlay_gradient + ', url(' + bg_img + ');';
							} else {
								gen_bg_css = 'background-image: url(' + bg_img + ');';
							}
						} else {
							gen_bg_css = 'background-image: url(' + bg_img + ');';
						}
					}
				} else if ( 'gradient' === bg_obj[device]['background-type'] ) {
					if ( '' !== bg_color && 'unset' !== bg_color ) {
						gen_bg_css = 'background-image: ' + bg_color + ';';
					}
				}
			}
			if ( '' !== bg_img ) {
				gen_bg_css += 'background-repeat: ' + bg_obj[device]['background-repeat'] + ';';
				gen_bg_css += 'background-position: ' + bg_obj[device]['background-position'] + ';';
				gen_bg_css += 'background-size: ' + bg_obj[device]['background-size'] + ';';
				gen_bg_css += 'background-attachment: ' + bg_obj[device]['background-attachment'] + ';';
			}
			// Remove old.
			jQuery( 'style#' + control + '-' + device + '-' + addon ).remove();
			if ( 'desktop' == device ) {
				var dynamicStyle = ''
			}
			if ( 'tablet' == device ) {
				var dynamicStyle = ''
			}
			if ( 'mobile' == device ) {
				var dynamicStyle = ''
			}
			// Concat and append new '
				);
			} else {
				jQuery( 'style#' + control  + '-font-size' ).remove();
			}
		});
	});
	astra_refresh_customizer( 'astra-settings[related-metadata-separator]' );
	astra_refresh_customizer( 'astra-settings[related-posts-image-ratio-type]' );
	astra_refresh_customizer( 'astra-settings[related-posts-image-ratio-pre-scale]' );
	astra_refresh_customizer( 'astra-settings[related-posts-image-custom-scale-width]' );
	astra_refresh_customizer( 'astra-settings[related-posts-image-custom-scale-height]' );
	astra_refresh_customizer( 'astra-settings[related-posts-image-size]' );
	astra_refresh_customizer( 'astra-settings[related-posts-author-prefix-label]' );
	astra_refresh_customizer( 'astra-settings[related-posts-author-avatar]' );
	astra_refresh_customizer( 'astra-settings[related-posts-meta-date-type]' );
	astra_refresh_customizer( 'astra-settings[related-posts-date-format]' );
	astra_refresh_customizer( 'astra-settings[related-posts-category-style]' );
	astra_refresh_customizer( 'astra-settings[related-posts-tag-style]' );
	function astraHandleButtonPresetPreview( btnSelector ) {
		wp.customize( 'astra-settings[button-preset-style]', function( setting ) {
			setting.bind( function( value ) {
				var buttonBGColor   = wp.customize( 'astra-settings[button-bg-color]' ).get();
				var buttonTextColor = wp.customize( 'astra-settings[button-color]' ).get();
				var themeColor = wp.customize( 'astra-settings[theme-color]' ).get();
				if( 'button_04' === value || 'button_05' === value || 'button_06' === value ) {
					var buttonBorderColor = wp.customize( 'astra-settings[theme-button-border-group-border-color]' ).get();
					if( '' === buttonBorderColor ) {
						jQuery( 'style#astra-settings-theme-button-border-group-border-color' ).remove();
						// Theme Button - Background Color
						jQuery( 'head' ).append(
							''
						);
					}
					if( '' === buttonTextColor && '' !== buttonBGColor ) {
						jQuery( 'style#astra-settings-button-outline-preset-color' ).remove();
						jQuery( 'head' ).append(
							''
						);
					}
					if( '' === buttonTextColor && '' === buttonBGColor ) {
						jQuery( 'style#astra-settings-button-outline-preset-color' ).remove();
						jQuery( 'head' ).append(
							''
						);
					}
					// Remove old.
					jQuery( 'style#astra-settings-button-preset-style-background-color' ).remove();
					// Concat and append new '
					);
				}
				else {
					jQuery( 'style#astra-settings-button-bg-color-background-color' ).remove();
					jQuery( 'style#astra-settings-button-outline-preset-color' ).remove();
					if( '' === buttonTextColor && '' === buttonBGColor ) {
						jQuery( 'head' ).append(
							''
						);
					} else {
						// Set background color for button to theme color when value is empty.
						buttonBGColor = ( '' != buttonBGColor ) ? buttonBGColor : themeColor;
						// Theme Button - Background Color
						jQuery( 'head' ).append(
							''
						);
					}
					if( '' === buttonTextColor ) {
						// Set button text color to white when value is empty.
						jQuery( 'head' ).append(
							''
						);
						jQuery( 'style#astra-settings-button-color-color' ).remove();
					}
				}
			} );
		} );
		wp.customize( 'astra-settings[secondary-button-preset-style]', function( setting ) {
			var btnBgColorSelector = '.wp-block-buttons .wp-block-button .wp-block-button__link.is-style-outline:not(.has-background), .wp-block-buttons .wp-block-button.is-style-outline>.wp-block-button__link:not(.has-background), .ast-outline-button';
			setting.bind( function( value ) {
				var buttonBGColor   = wp.customize( 'astra-settings[secondary-button-bg-color]' ).get();
				var buttonTextColor = wp.customize( 'astra-settings[secondary-button-color]' ).get();
				var themeColor = wp.customize( 'astra-settings[theme-color]' ).get();
				if( 'button_04' === value || 'button_05' === value || 'button_06' === value ) {
					var buttonBorderColor = wp.customize( 'astra-settings[secondary-theme-button-border-group-border-color]' ).get();
					if( '' === buttonBorderColor ) {
						jQuery( 'style#astra-settings-secondary-theme-button-border-group-border-color' ).remove();
						// Theme Button - Background Color
						jQuery( 'head' ).append(
							''
						);
					}
					if( '' === buttonTextColor && '' !== buttonBGColor ) {
						jQuery( 'style#astra-settings-secondary-button-outline-preset-color' ).remove();
						jQuery( 'head' ).append(
							''
						);
					}
					if( '' === buttonTextColor && '' === buttonBGColor ) {
						jQuery( 'style#astra-settings-secondary-button-outline-preset-color' ).remove();
						jQuery( 'head' ).append(
							''
						);
					}
					// Remove old.
					jQuery( 'style#astra-settings-secondary-button-preset-style-background-color' ).remove();
					// Concat and append new '
					);
				} else {
					jQuery( 'style#astra-settings-secondary-button-bg-color-background-color' ).remove();
					jQuery( 'style#astra-settings-secondary-button-outline-preset-color' ).remove();
					if( '' === buttonTextColor && '' === buttonBGColor ) {
						jQuery( 'head' ).append(
							''
						);
					} else {
						// Set background color for button to theme color when value is empty.
						buttonBGColor = ( '' != buttonBGColor ) ? buttonBGColor : themeColor;
						// Theme Button - Background Color
						jQuery( 'head' ).append(
							''
						);
					}
					if( '' === buttonTextColor ) {
						// Set button text color to white when value is empty.
						jQuery( 'head' ).append(
							''
						);
						jQuery( 'style#astra-settings-secondary-button-color-color' ).remove();
					}
					// Clear preset.
					if ( '' === value ) {
						wp.customize.preview.send( 'refresh' );
					}
				}
			} );
		} );
		wp.customize( 'astra-settings[button-color]', function( setting ) {
			setting.bind( function( value ) {
				if( '' === value ) {
					var buttonPreset = wp.customize( 'astra-settings[button-preset-style]' ).get();
					// If button has outline preset.
					if( 'button_04' === buttonPreset || 'button_05' === buttonPreset || 'button_06' === buttonPreset ) {
						var buttonBGColor   = wp.customize( 'astra-settings[button-bg-color]' ).get();
						jQuery( 'style#astra-settings-button-outline-color' ).remove();
						// Theme Button - Background Color
						jQuery( 'head' ).append(
							''
						);
					}
					 else {
						jQuery( 'style#astra-settings-button-color-color' ).remove();
						// Theme Button - Background Color
						jQuery( 'head' ).append(
							''
						);
					}
				} else {
					jQuery( 'style#astra-settings-button-color-color' ).remove();
					// Theme Button - Background Color
					jQuery( 'head' ).append(
						''
					);
				}
			} );
		} );
		wp.customize( 'astra-settings[secondary-button-color]', function( setting ) {
			setting.bind( function( value ) {
				if( '' === value ) {
					var buttonPreset = wp.customize( 'astra-settings[secondary-button-preset-style]' ).get();
					var themeColor = wp.customize( 'astra-settings[theme-color]' ).get();
					// If button has outline preset.
					if( 'button_04' === buttonPreset || 'button_05' === buttonPreset || 'button_06' === buttonPreset ) {
						var buttonBGColor   = wp.customize( 'astra-settings[secondary-button-bg-color]' ).get();
						jQuery( 'style#astra-settings-secondary-button-color-color' ).remove();
						// Theme Button - Background Color
						jQuery( 'head' ).append(
							''
						);
					} else {
						jQuery( 'style#astra-settings-button-color-color' ).remove();
						// Theme Button - Background Color
						jQuery( 'head' ).append(
							''
						);
					}
				} else {
					jQuery( 'style#astra-settings-secondary-button-color-color' ).remove();
					// Theme Button - Background Color
					jQuery( 'head' ).append(
						''
					);
				}
			} );
		} );
		wp.customize( 'astra-settings[button-bg-color]', function( setting ) {
			setting.bind( function( value ) {
				var buttonPreset = wp.customize( 'astra-settings[button-preset-style]' ).get();
				var themeColor = wp.customize( 'astra-settings[theme-color]' ).get();
				var buttonTextColor = wp.customize( 'astra-settings[button-color]' ).get();
				// If button has outline preset.
				if( 'button_04' === buttonPreset || 'button_05' === buttonPreset || 'button_06' === buttonPreset ) {
					var buttonTextColor = wp.customize( 'astra-settings[button-color]' ).get();
					var buttonBorderColor = wp.customize( 'astra-settings[theme-button-border-group-border-color]' ).get();
					if( '' === buttonBorderColor ) {
						// Theme Button - Background Color
						jQuery( 'style#astra-settings-theme-button-border-group-border-color' ).remove();
						// Theme Button - Background Color
						jQuery( 'head' ).append(
							''
						);
					}
					if( '' === buttonTextColor ) {
						jQuery( 'style#astra-settings-button-outline-preset-color' ).remove();
						jQuery( 'head' ).append(
							''
						);
					}
				}
				 else {
					jQuery( 'style#astra-settings-button-bg-color-background-color' ).remove();
					jQuery( 'style#astra-settings-button-outline-preset-color' ).remove();
					// Set background color for button to theme color when value is empty.
					value = ( '' != value ) ? value : themeColor;
					if( '' === buttonTextColor ) {
						jQuery( 'head' ).append(
							''
						);
					}
					// Theme Button - Background Color
					jQuery( 'head' ).append(
						''
					);
				}
			} );
		} );
		wp.customize( 'astra-settings[secondary-button-bg-color]', function( setting ) {
			var btnBgColorSelector = '.wp-block-buttons .wp-block-button .wp-block-button__link.is-style-outline:not(.has-background), .wp-block-buttons .wp-block-button.is-style-outline>.wp-block-button__link:not(.has-background), .ast-outline-button';
			setting.bind( function( value ) {
				var buttonPreset = wp.customize( 'astra-settings[secondary-button-preset-style]' ).get();
				var themeColor = wp.customize( 'astra-settings[theme-color]' ).get();
				var buttonTextColor = wp.customize( 'astra-settings[secondary-button-color]' ).get();
				var buttonBorderColor = wp.customize( 'astra-settings[secondary-theme-button-border-group-border-color]' ).get();
				// If button has outline preset.
				if( 'button_04' === buttonPreset || 'button_05' === buttonPreset || 'button_06' === buttonPreset ) {
					if( '' === buttonBorderColor ) {
						// Theme Button - Background Color
						jQuery( 'style#astra-settings-secondary-theme-button-border-group-border-color' ).remove();
						// Theme Button - Background Color
						jQuery( 'head' ).append(
							''
						);
					}
					if( '' === buttonTextColor ) {
						jQuery( 'style#astra-settings-secondary-button-outline-preset-color' ).remove();
						jQuery( 'head' ).append(
							''
						);
					}
					jQuery( 'style#astra-settings-secondary-button-bg-color-background-color' ).remove();
					jQuery( 'head' ).append(
						''
					);
				} else if ( 'button_01' === buttonPreset || 'button_02' === buttonPreset || 'button_03' === buttonPreset ) {
					jQuery( 'style#astra-settings-secondary-button-outline-preset-color' ).remove();
					jQuery( 'style#astra-settings-secondary-button-bg-color-background-color' ).remove();
					jQuery( 'head' ).append(
						''
					);
					// Set background color for button to theme color when value is empty.
					value = ( '' != value ) ? value : themeColor;
					if( '' === buttonTextColor ) {
						jQuery( 'head' ).append(
							''
						);
					}
				} else {
					if ( '' === buttonBorderColor ) {
						jQuery( 'head' ).append(
							''
						);
					}
					jQuery( 'style#astra-settings-secondary-button-bg-color-background-color' ).remove();
					jQuery( 'head' ).append(
						''
					);
				}
			} );
		} );
		/**
		 * Cart Count Color.
		 */
		wp.customize( 'astra-settings[woo-header-cart-product-count-color]', function( setting ) {
			setting.bind( function( color ) {
				if( color ) {
					var dynamicStyle = '.ast-menu-cart-outline .ast-cart-menu-wrap .count, .ast-site-header-cart .ast-addon-cart-wrap i.astra-icon:after { color: ' + color + '; } ';
					astra_add_dynamic_css( 'woo-header-cart-product-count-color', dynamicStyle );
				} else {
					wp.customize.preview.send( 'refresh' );
				}
			} );
		} );
		/**
		 * Cart Count Color Hover.
		 */
		wp.customize( 'astra-settings[woo-header-cart-product-count-h-color]', function( setting ) {
			setting.bind( function( color ) {
				if( color ) {
					var dynamicStyle = '.ast-site-header-cart .ast-site-header-cart-li:hover .ast-cart-menu-wrap .count .ast-count-text, .ast-site-header-cart .ast-site-header-cart-li:hover .ast-addon-cart-wrap i.astra-icon:after { color: ' + color + '; } ';
					astra_add_dynamic_css( 'woo-header-cart-product-count-h-color', dynamicStyle );
				} else {
					wp.customize.preview.send( 'refresh' );
				}
			} );
		} );
		wp.customize('astra-settings[single-product-cart-button-width]', function (value) {
			value.bind(function (size) {
				var tablet_break_point = astraBuilderPreview.tablet_break_point || 768,
				mobile_break_point = astraBuilderPreview.mobile_break_point || 544;
				if (size.desktop != '' || size.tablet != '' || size.mobile != '') {
					var dynamicStyle = '';
					dynamicStyle += '.woocommerce div.product form.cart .button.single_add_to_cart_button {';
					dynamicStyle += 'width: ' + size.desktop + '%' + ';';
					dynamicStyle += '} ';
					dynamicStyle += '@media (max-width: ' + tablet_break_point + 'px) {';
					dynamicStyle += '.woocommerce div.product form.cart .button.single_add_to_cart_button {';
					dynamicStyle += 'width: ' + size.tablet + '%' + ';';
					dynamicStyle += '} ';
					dynamicStyle += '} ';
					dynamicStyle += '@media (max-width: ' + mobile_break_point + 'px) {';
					dynamicStyle += '.woocommerce div.product form.cart .button.single_add_to_cart_button {';
					dynamicStyle += 'width: ' + size.mobile + '%' + ';';
					dynamicStyle += '} ';
					dynamicStyle += '} ';
					astra_add_dynamic_css('header-woo-cart-icon-size', dynamicStyle);
				} else {
					wp.customize.preview.send( 'refresh' );
				}
			});
		});
		// Single product Sticky add to cart.
		const astraStickyAddToCartBtnColor = '.woocommerce .ast-sticky-add-to-cart .button.alt';
		const astraStickyAddToCartBtnHover = '.woocommerce .ast-sticky-add-to-cart .button.alt:hover';
		astra_css( 'astra-settings[single-product-sticky-add-to-cart-btn-n-color]', 'color', astraStickyAddToCartBtnColor );
		astra_css( 'astra-settings[single-product-sticky-add-to-cart-btn-h-color]', 'color', astraStickyAddToCartBtnHover );
		astra_css( 'astra-settings[single-product-sticky-add-to-cart-btn-bg-n-color]', 'background', astraStickyAddToCartBtnColor );
		astra_css( 'astra-settings[single-product-sticky-add-to-cart-btn-bg-h-color]', 'background', astraStickyAddToCartBtnHover );
		astra_css( 'astra-settings[single-product-sticky-add-to-cart-btn-bg-n-color]', 'border-color',astraStickyAddToCartBtnColor );
		astra_css( 'astra-settings[single-product-sticky-add-to-cart-btn-bg-h-color]', 'border-color', astraStickyAddToCartBtnHover );
		astra_css( 'astra-settings[single-product-sticky-add-to-cart-text-color]', 'color', '.ast-sticky-add-to-cart .ast-container .ast-sticky-add-to-cart-content' );
		astra_css( 'astra-settings[single-product-sticky-add-to-cart-bg-color]', 'background-color', '.ast-sticky-add-to-cart');
		wp.customize( 'astra-settings[single-product-sticky-add-to-cart-position]', function( setting ) {
			setting.bind( function( position  ) {
				var dynamicStyle = '';
				if( 'top' === position ) {
					dynamicStyle += 'div.ast-sticky-add-to-cart{';
					dynamicStyle += 'top: 0;';
					dynamicStyle += 'bottom: initial;';
					dynamicStyle += 'transform: translate(0, -100%);';
					dynamicStyle += 'box-shadow: 0px 1px 10px rgba(0, 0, 0, 0.1), 0px 1px 9px rgba(0, 0, 0, 0.06);';
					dynamicStyle += 'opacity: 0';
					dynamicStyle += '}';
				} else {
					dynamicStyle += 'div.ast-sticky-add-to-cart{';
					dynamicStyle += 'bottom: 0;';
					dynamicStyle += 'top: initial;';
					dynamicStyle += 'transform: translate(0, 100%);';
					dynamicStyle += 'box-shadow: 0px -1px 10px rgba(0, 0, 0, 0.1), 0px -1px 9px rgba(0, 0, 0, 0.06);';
					dynamicStyle += 'opacity: 0';
					dynamicStyle += '}';
				}
				astra_add_dynamic_css( 'sticky-add-to-cart-position', dynamicStyle );
			} );
		} );
		wp.customize( 'astra-settings[single-product-payment-icon-color]', function( setting ) {
			setting.bind( function( value ) {
				wp.customize.preview.send( 'refresh' );
			} );
		} );
		wp.customize( 'astra-settings[shop-ratings-product-archive]', function( setting ) {
			setting.bind( function( value ) {
				wp.customize.preview.send( 'refresh' );
			} );
		} );
		wp.customize( 'astra-settings[single-product-payment-text]', function( setting ) {
			setting.bind( function( text ) {
				const paymentText = document.querySelector('.ast-single-product-payments legend');
				if( paymentText ) {
					paymentText.textContent = text;
				}
			} );
		} );
		wp.customize( 'astra-settings[single-product-payment-list]', function( setting ) {
			setting.bind( function( value ) {
				wp.customize.preview.send( 'refresh' );
			} );
		} );
	}
} )( jQuery );