(function ($) {
	"use strict";
	$( document ).ready(
		function () {
			/*enable disable toggle*/
			$( '#ecafe-edtoggleId' ).on(
				'click',
				function () {
					// $('.ecafe-settings-form-wrapper .widget-list-checkbox:checkbox:enabled').prop('checked', $(this).prop('checked'));
					// selectors to exclude
					var excluded =
					'.ecafecateacf, .ecafecatecfs, .ecafecatejet, .ecafecatemetabox, .ecafecatepods, .ecafecatetoolset, .ecafecatewck, .ecafefeatures-pro-schema';

					// target only widgets NOT inside excluded categories
					$( '.ecafe-settings-form-wrapper .ecafe-panel-column' )
					.not( excluded )
					.find( '.widget-list-checkbox:enabled' )
					.prop( 'checked', $( this ).prop( 'checked' ) );
					if (this.checked) {
						$( this ).closest( ".ecafe-edtoggle" ).addClass( "activetoggle" );
					} else {
						$( this ).closest( ".ecafe-edtoggle" ).removeClass( "activetoggle" );
					}
				}
			);

			/*filter*/
			$( '.ecafe-panel-head .ecafe-panel-h-filter .ecafe-panel-h-filter-list a' ).on(
				'click',
				function (event) {
					event.preventDefault();
					$( this ).closest( '.ecafe-panel-h-filter-list' ).find( '.ecactivecat' ).removeClass( 'ecactivecat' );
					var catlist = $( this ).data( 'catlist' );
					$( this ).closest( '.form-ecafe_widgets' ).find( '.ecafe-panel-row .ecafe-panel-column' ).css( 'display','none' );
					if (catlist == 'ecafecatall' && catlist != undefined) {
						$( this ).closest( '.form-ecafe_widgets' ).find( '.ecafe-panel-row .ecafe-panel-column' ).css( 'display','inline-flex' );
					} else {
						$( this ).closest( '.form-ecafe_widgets' ).find( '.' + catlist ).css( 'display','inline-flex' );
					}

					$( this ).addClass( 'ecactivecat' );
				}
			);

			// custom login
			if ($( '#ecafe_wp_security .ecafe-panel-column input#custom_login_path' ).length) {
				function eclptoggleFields(){
					var clpathVal    = $( '#custom_login_path' ).val().trim();
					var clbehaviorEl = $( '#custom_login_behavior' ).closest( '.ecafe-panel-column' );
					var clmessageEl  = $( '#custom_login_message' ).closest( '.ecafe-panel-column' );

					if (clpathVal !== '') {
						clbehaviorEl.show();

						if ($( '#custom_login_behavior' ).val() === 'message') {
							clmessageEl.show();
						} else {
							clmessageEl.hide();
						}
					} else {
						clbehaviorEl.hide();
						clmessageEl.hide();
					}
				}

				eclptoggleFields();
				$( '#custom_login_path, #custom_login_behavior' ).on( 'keyup change', eclptoggleFields );
			}

		}
	);

	document.addEventListener(
		"DOMContentLoaded",
		function () {
			var ecafeBuildTempType        = document.getElementById( "ecafe-build-temp-type" );
			var ecafeBuildTempDisplay     = document.getElementById( "ecafe-build-temp-display" );
			var ecafeBuildDisplaySingular = document.getElementById( "ecafe-build-display-singular" );
			var ecafeBuildDisplayArchives = document.getElementById( "ecafe-build-display-archives" );
			var ecafeBuildDisplayPrev     = document.getElementById( "ecafe-build-display-prev" );

			if ($( '#ecafe-build-display-archives' ).length > 0) {
				jQuery( '#ecafe-build-display-archives' ).select2();
			}

			if ($( '#ecafe-build-temp-display' ).length > 0) {
				var fieldCols = {
					temp: ecafeBuildTempDisplay.closest( ".ecafe-field-col" ),
					singular: ecafeBuildDisplaySingular.closest( ".ecafe-field-col" ),
					archives: ecafeBuildDisplayArchives.closest( ".ecafe-field-col" ),
					prev: ecafeBuildDisplayPrev.closest( ".ecafe-field-col" )
				};

				ecafeBuildTempType.addEventListener(
					'change',
					function () {
						const selectedValue = this.value;
						if (selectedValue == 'singular' || selectedValue == 'archives') {
							let template_data = ecafeBuildDisplaySingular.value
							if (selectedValue == 'archives') {
								template_data = ecafeBuildDisplayArchives.value;
							}
							ecafe_get_prev_ajax( selectedValue, template_data )
						} else {
							ecafeBuildDisplayPrev.innerHTML = ''; // Clear post details container if no option is selected
						}
					}
				);

				ecafeBuildDisplaySingular.addEventListener(
					'change',
					function () {
						const selectedValue = ecafeBuildTempType.value;
						if (selectedValue == 'singular') {
							let template_data = this.value
							ecafe_get_prev_ajax( selectedValue, template_data )
						} else {
							ecafeBuildDisplayPrev.innerHTML = ''; // Clear post details container if no option is selected
						}
					}
				);

				jQuery( '#ecafe-build-display-archives' ).on(
					'change',
					function () {
						const selectedValue = ecafeBuildTempType.value;
						if (selectedValue == 'archives' && jQuery( this ).val() != '') {
							let template_data = jQuery( this ).val()
							ecafe_get_prev_ajax( selectedValue, template_data )
						} else {
							ecafeBuildDisplayPrev.innerHTML = ''; // Clear post details container if no option is selected
						}
					}
				);
			}

			function ecafe_get_prev_ajax(template_type, template_data){
				const xhr = new XMLHttpRequest();
				xhr.open( 'POST', ajaxurl, true );
				xhr.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
				xhr.onreadystatechange = function () {
					if (xhr.readyState === 4 && xhr.status === 200) {
						var result_data                 = JSON.parse( xhr.responseText )
						ecafeBuildDisplayPrev.innerHTML = result_data.data;
					} else if (xhr.status !== 200) {
						console.error( 'Error fetching details:', xhr.statusText );
					}
				};

				xhr.send( 'action=ecafe_get_prev_list&ecafe_nonce=' + ecafe_data.nonce + '&template_type=' + template_type + '&template_data=' + template_data );
			}
			function handleTemplateTypeChange() {
				var templateType = ecafeBuildTempType.value;
				switch (templateType) {
					case "header":
					case "footer":
						showField( "temp" );
						hideField( "singular" );
						hideField( "archives" );
						hideField( "prev" );
						break;
					case "singular":
					case "archives":
						hideField( "temp" );
						hideField( "singular" );
						hideField( "archives" );
						showField( templateType );
						showField( "prev" );
						break;
					default:
						hideField( "temp" );
						hideField( "singular" );
						hideField( "archives" );
						hideField( "prev" );
						break;
				}
			}

			function hideField(field) {
				fieldCols[field].style.display = "none";
			}

			function showField(field) {
				fieldCols[field].style.display = "block";
			}

			if (ecafeBuildTempType) {
				ecafeBuildTempType.addEventListener( "change", handleTemplateTypeChange );
				handleTemplateTypeChange();
			}
		}
	);
})( window.jQuery );