/**
 * Klarna Checkout Debugging Tool
 * Paste this into the browser console to run diagnostics
 */

(function(window, document){
	window.wpgvKlarnaDebug = {
		// Check modal visibility and structure
		checkModal: function() {
			console.log('=== KLARNA MODAL DEBUG ===');
			var modal = document.getElementById('wpgv-klarna-modal');
			var container = document.getElementById('wpgv-klarna-container');
			var inner = document.getElementById('wpgv-klarna-modal-inner');

			console.log('Modal exists:', !!modal);
			if (modal) {
				console.log('Modal display:', window.getComputedStyle(modal).display);
				console.log('Modal visibility:', window.getComputedStyle(modal).visibility);
				console.log('Modal zIndex:', window.getComputedStyle(modal).zIndex);
				console.log('Modal pointerEvents:', window.getComputedStyle(modal).pointerEvents);
				console.log('Modal opacity:', window.getComputedStyle(modal).opacity);
			}

			console.log('\nInner div exists:', !!inner);
			if (inner) {
				console.log('Inner display:', window.getComputedStyle(inner).display);
				console.log('Inner pointerEvents:', window.getComputedStyle(inner).pointerEvents);
				console.log('Inner zIndex:', window.getComputedStyle(inner).zIndex);
			}

			console.log('\nContainer exists:', !!container);
			if (container) {
				console.log('Container display:', window.getComputedStyle(container).display);
				console.log('Container pointerEvents:', window.getComputedStyle(container).pointerEvents);
				console.log('Container overflow:', window.getComputedStyle(container).overflow);
				console.log('Container visibility:', window.getComputedStyle(container).visibility);
				console.log('Container zIndex:', window.getComputedStyle(container).zIndex);
				console.log('Container HTML length:', container.innerHTML.length);
				console.log('Container children count:', container.children.length);
			}
		},

		// Check for Pay button
		checkPayButton: function() {
			console.log('=== PAY BUTTON DEBUG ===');
			var container = document.getElementById('wpgv-klarna-container');
			if (!container) {
				console.log('Container not found');
				return;
			}

			var buttons = Array.prototype.slice.call(container.querySelectorAll('button'));
			console.log('Total buttons found:', buttons.length);

			buttons.forEach(function(btn, idx) {
				var text = (btn.innerText || btn.textContent || '').trim();
				console.log('Button ' + idx + ':', {
					text: text,
					disabled: btn.disabled,
					ariaDisabled: btn.getAttribute('aria-disabled'),
					className: btn.className,
					display: window.getComputedStyle(btn).display,
					pointerEvents: window.getComputedStyle(btn).pointerEvents,
					opacity: window.getComputedStyle(btn).opacity,
					visibility: window.getComputedStyle(btn).visibility
				});
			});

			// Try to find Pay button specifically
			var payBtn = null;
			buttons.forEach(function(b) {
				var txt = (b.innerText || b.textContent || '').trim();
				if (/pay order/i.test(txt) || /pay now/i.test(txt) || /place order/i.test(txt) || /pay\s|köp|betala/i.test(txt)) {
					payBtn = b;
					console.log('\n[PAY BUTTON FOUND]: ' + txt);
					console.log('Disabled:', payBtn.disabled);
					console.log('Computed styles:', {
						display: window.getComputedStyle(payBtn).display,
						pointerEvents: window.getComputedStyle(payBtn).pointerEvents,
						cursor: window.getComputedStyle(payBtn).cursor,
						opacity: window.getComputedStyle(payBtn).opacity,
						background: window.getComputedStyle(payBtn).background
					});
				}
			});
		},

		// Check iframes
		checkIframes: function() {
			console.log('=== IFRAME DEBUG ===');
			var container = document.getElementById('wpgv-klarna-container');
			if (!container) {
				console.log('Container not found');
				return;
			}

			var iframes = container.querySelectorAll('iframe');
			console.log('Total iframes found:', iframes.length);

			iframes.forEach(function(iframe, idx) {
				console.log('Iframe ' + idx + ':', {
					src: iframe.src,
					id: iframe.id,
					className: iframe.className,
					display: window.getComputedStyle(iframe).display,
					pointerEvents: window.getComputedStyle(iframe).pointerEvents,
					visibility: window.getComputedStyle(iframe).visibility,
					opacity: window.getComputedStyle(iframe).opacity,
					width: iframe.getAttribute('width'),
					height: iframe.getAttribute('height')
				});
			});
		},

		// Check for blocking elements
		checkBlockingElements: function() {
			console.log('=== BLOCKING ELEMENTS DEBUG ===');
			var container = document.getElementById('wpgv-klarna-container');
			if (!container) {
				console.log('Container not found');
				return;
			}

			// Get mouse position from last event (or use center)
			var rect = container.getBoundingClientRect();
			var x = rect.left + rect.width / 2;
			var y = rect.top + rect.height / 2;

			console.log('Checking elements at point:', x, y);

			var elementAt = document.elementFromPoint(x, y);
			console.log('Element at point:', elementAt);

			var parent = elementAt;
			var depth = 0;
			console.log('\nElement hierarchy:');
			while (parent && depth < 10) {
				console.log('  '.repeat(depth) + parent.tagName + ' [' + (parent.className || '') + '] - pointerEvents: ' + window.getComputedStyle(parent).pointerEvents);
				parent = parent.parentElement;
				depth++;
			}
		},

		// Try to enable Pay button manually
		enablePayButton: function() {
			console.log('=== ATTEMPTING TO ENABLE PAY BUTTON ===');
			var container = document.getElementById('wpgv-klarna-container');
			if (!container) {
				console.log('Container not found');
				return;
			}

			var buttons = Array.prototype.slice.call(container.querySelectorAll('button'));
			var payBtn = null;
			buttons.forEach(function(b) {
				var txt = (b.innerText || b.textContent || '').trim();
				if (/pay order/i.test(txt) || /pay now/i.test(txt) || /place order/i.test(txt) || /pay\s|köp|betala/i.test(txt)) {
					payBtn = b;
				}
			});

			if (payBtn) {
				console.log('Pay button found, attempting to enable...');
				payBtn.disabled = false;
				payBtn.removeAttribute('aria-disabled');
				payBtn.classList.remove('disabled');
				payBtn.style.pointerEvents = 'auto';
				payBtn.style.opacity = '1';
				console.log('Pay button enabled. Try clicking it now.');
			} else {
				console.log('No pay button found');
			}
		},

		// Run all diagnostics
		runAll: function() {
			this.checkModal();
			console.log('\n');
			this.checkPayButton();
			console.log('\n');
			this.checkIframes();
			console.log('\n');
			this.checkBlockingElements();
		}
	};

})(window, document);
