// document.addEventListener('DOMContentLoaded', function () {
// 	const forms = document.querySelectorAll('.cfturnstile-disable-submit');
// 	forms.forEach(function (form) {
// 		const submit = form.querySelector('[type="submit"]');
// 		if (submit) {
// 			submit.disabled = true;
// 			submit.style.opacity = '0.5';
// 			submit.style.cursor = 'not-allowed';
// 		}
// 	});
// });

// window.ultimate_securityTurnstileCallback = function (token) {
// 	const forms = document.querySelectorAll('.cfturnstile-disable-submit');
// 	forms.forEach(function (form) {
// 		const submit = form.querySelector('[type="submit"]');
// 		if (submit) {
// 			submit.disabled = false;
// 			submit.style.opacity = '1';
// 			submit.style.cursor = 'pointer';
// 		}
// 	});
// };

// disable-submit.js
( function () {
	'use strict';

	function disableSubmitForForm( form ) {
		if ( ! form ) {
			return;
		}
		if ( form.dataset.cfTurnstileDisabled === '1' ) {
			return;
		}
		form.dataset.cfTurnstileDisabled = '1';

		// target both input[type=submit] and <button type=submit>
		const submits = form.querySelectorAll( 'input[type="submit"], button[type="submit"]' );
		submits.forEach( function ( el ) {
			el.dataset._prevDisabled = el.disabled ? '1' : '0';
			el.disabled = true;
			el.style.opacity = '0.5';
			el.style.cursor = 'not-allowed';
		} );
		console.log( '[cfturnstile] disabled submit for form', form );
	}

	function enableSubmitForForm( form ) {
		if ( ! form ) {
			return;
		}
		const submits = form.querySelectorAll( 'input[type="submit"], button[type="submit"]' );
		submits.forEach( function ( el ) {
			// restore prior disabled state if stored
			if ( el.dataset._prevDisabled === '1' ) {
				el.disabled = true;
			} else {
				el.disabled = false;
			}
			el.style.opacity = '';
			el.style.cursor = '';
			delete el.dataset._prevDisabled;
		} );
		console.log( '[cfturnstile] enabled submit for form', form );
	}

	function scanAndMark() {
		const widgets = document.querySelectorAll( '.cf-turnstile, .turnstile-login, .turnstile' ); // be permissive
		widgets.forEach( function ( widget ) {
			const form = widget.closest( 'form' );
			if ( form ) {
				if ( ! form.classList.contains( 'cfturnstile-disable-submit' ) ) {
					form.classList.add( 'cfturnstile-disable-submit' );
				}
				disableSubmitForForm( form );
			}
		} );
	}

	// DOM ready initial scan
	document.addEventListener( 'DOMContentLoaded', function () {
		console.log( '[cfturnstile] DOMContentLoaded - scanning for widgets' );
		scanAndMark();
	} );

	// Also run a quick scan in case this script loads after DOMContentLoaded
	try {
		if ( document.readyState === 'interactive' || document.readyState === 'complete' ) {
			console.log(
				'[cfturnstile] document.readyState',
				document.readyState,
				'- immediate scan'
			);
			scanAndMark();
		}
	} catch ( e ) {
		// ignore
	}

	// Observe DOM for widgets that are added later (Turnstile sometimes injects)
	const observer = new MutationObserver( function ( mutations ) {
		let found = false;
		for ( const m of mutations ) {
			if ( m.addedNodes && m.addedNodes.length ) {
				for ( const n of m.addedNodes ) {
					if ( n.nodeType === 1 ) {
						if (
							n.matches &&
							n.matches( '.cf-turnstile, .turnstile-login, .turnstile' )
						) {
							found = true;
							break;
						} else if (
							n.querySelector &&
							( n.querySelector( '.cf-turnstile' ) ||
								n.querySelector( '.turnstile-login' ) ||
								n.querySelector( '.turnstile' ) )
						) {
							found = true;
							break;
						}
					}
				}
			}
			if ( found ) {
				break;
			}
		}
		if ( found ) {
			console.log( '[cfturnstile] MutationObserver detected widget(s) - scanning' );
			scanAndMark();
		}
	} );

	observer.observe( document.documentElement || document.body, {
		childList: true,
		subtree: true,
	} );

	// global callback — Turnstile will call this name (must match data-callback attr)
	window.ultimate_securityTurnstileCallback = function ( token ) {
		console.log(
			'[cfturnstile] callback called, token length:',
			token ? token.length : 'no-token'
		);
		// Enable any forms that we previously disabled
		const forms = document.querySelectorAll( '.cfturnstile-disable-submit' );
		forms.forEach( function ( form ) {
			enableSubmitForForm( form );
		} );
	};
} )();
