/* Settings page — Test connection button. Backend URL is injected
   from PHP via wp_localize_script as window.PrintPriceSettings.backend. */
( function () {
	function init() {
		var btn = document.getElementById( 'pp-connect-test' );
		if ( ! btn ) {
			return;
		}
		btn.addEventListener( 'click', async function ( e ) {
			e.preventDefault();
			var status = document.getElementById( 'pp-connect-status' );
			var backend = ( window.PrintPriceSettings && window.PrintPriceSettings.backend ) || '';
			if ( ! backend ) {
				status.textContent = '✗ No backend URL configured';
				status.style.color = 'red';
				return;
			}
			status.textContent = 'Checking…';
			try {
				var res = await fetch( backend + '/api/woo/health' );
				if ( res.ok ) {
					status.textContent = '✓ Backend reachable';
					status.style.color = 'green';
				} else {
					status.textContent = '✗ HTTP ' + res.status;
					status.style.color = 'red';
				}
			} catch ( err ) {
				status.textContent = '✗ ' + err.message;
				status.style.color = 'red';
			}
		} );
	}

	if ( document.readyState === 'loading' ) {
		document.addEventListener( 'DOMContentLoaded', init );
	} else {
		init();
	}
} )();
