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

	function cfg() {
		return typeof wpPsTurnstileWoo !== 'undefined' ? wpPsTurnstileWoo : {};
	}

	function setTurnstileExtensionData(token) {
		if (typeof wp === 'undefined' || !wp.data || typeof turnstile === 'undefined') {
			return;
		}
		var dispatch = wp.data.dispatch('wc/store/checkout');
		var payload = { token: token || '' };
		if (typeof dispatch.setExtensionData === 'function') {
			dispatch.setExtensionData(ns(), payload);
		} else if (typeof dispatch.__internalSetExtensionData === 'function') {
			dispatch.__internalSetExtensionData(ns(), payload);
		}
	}

	function renderOptsFor(el) {
		var c = cfg();
		var sitekey = el.getAttribute('data-sitekey') || c.siteKey || '';
		if (!sitekey) {
			return null;
		}
		return {
			sitekey: sitekey,
			theme: el.getAttribute('data-theme') || c.theme || 'auto',
			size: el.getAttribute('data-size') || c.apiSize || 'normal',
			appearance: el.getAttribute('data-appearance') || c.appearance || 'interaction-only',
			callback: function (token) {
				setTurnstileExtensionData(token);
			},
			'expired-callback': function () {
				setTurnstileExtensionData('');
			},
		};
	}

	function turnstileWooCheckoutReset() {
		if (typeof turnstile === 'undefined') {
			return;
		}
		var el = document.getElementById('cf-turnstile-woo-checkout');
		if (!el) {
			return;
		}
		var opts = renderOptsFor(el);
		if (!opts) {
			return;
		}
		if (!el.innerHTML || el.innerHTML.trim() === '') {
			try {
				turnstile.render(el, opts);
			} catch (e) {
				try {
					turnstile.render('#cf-turnstile-woo-checkout', opts);
				} catch (e2) {}
			}
			return;
		}
		try {
			if (typeof turnstile.reset === 'function') {
				turnstile.reset(el);
				return;
			}
		} catch (e) {}
		try {
			turnstile.remove(el);
		} catch (e) {}
		try {
			turnstile.render(el, opts);
		} catch (e) {
			try {
				turnstile.render('#cf-turnstile-woo-checkout', opts);
			} catch (e2) {}
		}
	}

	jQuery(document.body).on('checkout_error', function () {
		setTimeout(turnstileWooCheckoutReset, 500);
	});

	jQuery(document.body).on('updated_checkout', function () {
		var $w = jQuery('#cf-turnstile-woo-checkout');
		if (!$w.length || !$w.contents().length) {
			setTimeout(turnstileWooCheckoutReset, 300);
		}
	});

	document.addEventListener('DOMContentLoaded', function () {
		if (typeof wp === 'undefined' || !wp.data || typeof turnstile === 'undefined') {
			return;
		}

		function cfturnstileWooBlockCheckoutRender() {
			var turnstileItem = document.getElementById('cf-turnstile-woo-checkout');
			if (!turnstileItem) {
				return;
			}
			var opts = renderOptsFor(turnstileItem);
			if (!opts) {
				return;
			}
			try {
				turnstile.remove(turnstileItem);
			} catch (e) {}
			try {
				turnstile.render(turnstileItem, opts);
				turnstileItem.setAttribute('data-wpps-init', 'true');
			} catch (e) {}
		}

		wp.data.subscribe(function () {
			var turnstileItem = document.getElementById('cf-turnstile-woo-checkout');
			if (turnstileItem && (!turnstileItem.innerHTML.trim() || turnstileItem.getAttribute('data-wpps-init') !== 'true')) {
				cfturnstileWooBlockCheckoutRender();
			}
		}, 'wc/store/cart');
	});
})();
