document.addEventListener("DOMContentLoaded", function () {

	// Dismiss notices.
	document.body.addEventListener('click', function (event) {
		if (event.target.matches('.notice-dismiss')) {
			document.querySelector('.cf-notice').style.display = 'none';
		}
	});

	document.body.addEventListener('click', function (event) {
		if (event.target.matches('.tab-pane .btn-primary')) {
			var currentButton = event.target.closest('.tab-pane').id;
			var nextButton = 'step' + (parseInt(currentButton.slice(4, 5)) + 1);

			if (currentButton === 'step3') {
				freeWizardData = new FormData();
				freeWizardData.append('action', 'multicollab_free_plugin_wizard_submit');
				freeWizardData.append('subscribe_email', document.querySelector('.last_step_email_subscription')?.value);
				freeWizardData.append('opt_in', document.querySelector('.count_me_in_free')?.checked);
				freeWizardData.append('country', document.querySelector('.cf-country-name')?.value);
				freeWizardData.append('survey_list', document.querySelector('.pro_last_step_select')?.value);
				freeWizardData.append('nonce', multicollabSetupWizardL10n.nonce);


				fetch(ajaxurl, {
					method: 'POST',
					body: freeWizardData
				})
				.then(response => response.text())
				.then(success => {
					var url = success.replace('&amp;', '&');
					window.location.href = url;
				})
				.finally(() => {
					document.body.classList.remove('cf-settings-loader');
				});

				document.body.classList.add('cf-settings-loader');

			} else {
				document.getElementById(currentButton).style.transition = "opacity 0.4s";
				document.getElementById(currentButton).style.opacity = 0;

				setTimeout(function () {
					document.getElementById(currentButton).style.display = "none";
					const nextBtn = document.getElementById(nextButton);
					nextBtn.style.display = "block";
					nextBtn.style.transition = "opacity 0.4s";
					nextBtn.style.opacity = 1;
				}, 400);

			}
		}
	});

});

// Added style to admin HTML
function javascriptLoad() {
	const bodyHasSetupWizard = document.body.classList.contains('admin_page_multicollab_setup_wizard');
	const htmlElement = document.querySelector('html');

	if (bodyHasSetupWizard) {
		htmlElement.style.paddingTop = '0px';
	} else if (window.innerWidth <= 600) {
		htmlElement.style.paddingTop = '0px';
	} else {
		htmlElement.style.paddingTop = '32px';
	}
}

document.addEventListener("DOMContentLoaded", javascriptLoad);
