/**
 * Capture billing email changes on WooCommerce checkout for abandoned cart recovery.
 */
(function () {
	'use strict';

	var cfg = window.SuiteMkt3kCartCapture || {};
	var lastSent = 0;

	document.addEventListener('change', function (e) {
		if (!e.target || !e.target.matches || !e.target.matches('#billing_email')) {
			return;
		}
		var email = (e.target.value || '').trim();
		if (!email || (Date.now() - lastSent) < 3000 || !cfg.ajaxUrl) {
			return;
		}
		lastSent = Date.now();
		var data = new FormData();
		data.append('action', 'suitemkt3k_capture_cart');
		data.append('email', email);
		data.append('name', (document.getElementById('billing_first_name') || {}).value || '');
		data.append('phone', (document.getElementById('billing_phone') || {}).value || '');
		if (cfg.nonce) {
			data.append('nonce', cfg.nonce);
		}
		fetch(cfg.ajaxUrl, { method: 'POST', body: data, credentials: 'same-origin' }).catch(function () {});
	}, true);
})();
