/**
 * File: customizer.js.
 *
 * Theme Customizer enhancements for a better user experience.
 *
 * Contains handlers to make Theme Customizer preview reload changes asynchronously.
 */

( function ( $ ) {
	 
	let anchor = document.querySelector( '.navbar-brand a' );
	if ( anchor && 'H1' === anchor.tagName ) {
		anchor = anchor.firstChild;
	}

	// Header text color.
	wp.customize( 'header_textcolor', function ( value ) {
		value.bind( function ( to ) {
			if ( anchor ) {
				if ( 'blank' === to ) {
					anchor.style.clip = 'rect(1px, 1px, 1px, 1px)';
					anchor.style.position = 'absolute';
				} else {
					anchor.style.clip = 'auto';
					anchor.style.position = 'relative';
					anchor.style.color = to;
				}
			}
		} );
	} );

	// Hide post date.
	wp.customize( 'wrockmetro_single_meta', function ( value ) {
		value.bind( function ( newVal ) {
			if ( newVal === 'disable' ) {
				$( '.post-date' ).hide(); // Replace '.post-date' with the CSS class or selector that represents your post date element.
			} else {
				$( '.post-date' ).show();
			}
		} );
	} );

	// Hero area customization.
	wp.customize( 'wrockmetro_heroarea', function ( value ) {
		value.bind( function ( newVal ) {
			if ( 'enable' === newVal ) {
				$( '.styletwo-header #wrapper-hero' ).css( 'margin-top', '-76px' );
			} else {
				// Reset to default or any other value.
				$( '.styletwo-header #wrapper-hero' ).css( 'margin-top', '0' );
			}

			// Trigger an instant refresh.
			wp.customize.previewer.refresh();
		} );
	} );

	// Site info.
	wp.customize( 'wrockmetro_site_info_override', function ( value ) {
		value.bind( function ( to ) {
			const siteInfo = document.querySelector( '.site-info' );
			if ( siteInfo ) {
				siteInfo.innerHTML = to;
			}
		} );
	} );

} )( jQuery );
