/* global wp, jQuery */
(function () {
	function ns() {
		return typeof wpPsSimpleCaptchaWoo !== 'undefined' && wpPsSimpleCaptchaWoo.ns
			? wpPsSimpleCaptchaWoo.ns
			: 'wp-powersuite-simple-captcha';
	}

	/**
	 * Prefer the SSR host printed before Place Order (checkout-actions-block).
	 * Never insert into `.wc-block-checkout__actions_row` — that throws React #60.
	 * Fallback: create a host as a sibling before `.wc-block-checkout__actions`.
	 */
	function ensureHost() {
		var existing = document.getElementById('wpps-simple-captcha-woo-checkout');
		if (existing) {
			return existing;
		}
		var wrap = document.querySelector('.wpps-simple-captcha-wrap.wpps-captcha-woo-checkout');
		if (wrap) {
			return wrap.closest('#wpps-simple-captcha-woo-checkout') || wrap;
		}

		var actions =
			document.querySelector('.wc-block-checkout__actions') ||
			document.querySelector('.wp-block-woocommerce-checkout-actions-block');
		if (!actions || !actions.parentNode) {
			return null;
		}

		var host = document.createElement('div');
		host.id = 'wpps-simple-captcha-woo-checkout';
		host.className = 'wpps-captcha-woo-checkout wpps-captcha-cushion';
		host.style.clear = 'both';
		host.style.margin = '0 0 16px';
		actions.parentNode.insertBefore(host, actions);
		return host;
	}

	function readFields() {
		var root =
			document.getElementById('wpps-simple-captcha-woo-checkout') ||
			document.querySelector('.wpps-simple-captcha-wrap.wpps-captcha-woo-checkout');
		if (!root) {
			return { key: '', answer: '', hp: '', hp_value: '' };
		}
		var keyEl = root.querySelector('input[name="wpps_simple_captcha_key"]');
		var ansEl = root.querySelector('input[name="wpps_simple_captcha_answer"]');
		var hpEl = root.querySelector('.wpps-sc-honeypot input[type="text"]');
		return {
			key: keyEl && keyEl.value ? keyEl.value : '',
			answer: ansEl && ansEl.value ? ansEl.value : '',
			hp: hpEl && hpEl.name ? hpEl.name : '',
			hp_value: hpEl && hpEl.value ? hpEl.value : '',
		};
	}

	function setExtensionData(fields) {
		if (typeof wp === 'undefined' || !wp.data) {
			return;
		}
		var dispatch = wp.data.dispatch('wc/store/checkout');
		var payload = {
			key: fields.key || '',
			answer: fields.answer || '',
			hp: fields.hp || '',
			hp_value: fields.hp_value || '',
		};
		if (typeof dispatch.setExtensionData === 'function') {
			dispatch.setExtensionData(ns(), payload);
		} else if (typeof dispatch.__internalSetExtensionData === 'function') {
			dispatch.__internalSetExtensionData(ns(), payload);
		}
	}

	function sync() {
		ensureHost();
		setExtensionData(readFields());
	}

	document.addEventListener('DOMContentLoaded', function () {
		sync();
		document.body.addEventListener('input', function (e) {
			if (
				e.target &&
				(e.target.name === 'wpps_simple_captcha_answer' ||
					e.target.name === 'wpps_simple_captcha_key' ||
					(e.target.name && String(e.target.name).indexOf('wpps_sc_hp_') === 0))
			) {
				sync();
			}
		});
		if (typeof jQuery !== 'undefined' && wp && wp.data && wp.data.subscribe) {
			wp.data.subscribe(sync);
		}
	});
})();
