(function() {
	// ### ENTER MODULE NAME HERE ######
	var str_moduleName = '__moduleName__'; // Modulnamen angepasst
	// #################################
	lsjs.__moduleHelpers[str_moduleName] = {
		self: null,
		popupElement: null,
		popupElementNotPayPalVerified: null,
		overlayElement: null,
		observerPayPal: null,
		observerSubmitBox: null,

		// anpassen falls id sich ändern sollte
		idPayPalCheckoutContainer: "payPalCheckout",
		idfinishingOrderSubmitBox: "finishingOrderSubmitBox",

		getLibraryToLoad: function() {
			return [];
		},

		onDomReady: function(el_toReload) {
		},

		onCajaxDomUpdate: function(el_toReload) {
			this.updateSubmitLink();
			this.checkConditionsAndShowPopup();
			this.setupObserversIfNotPresent(); // Sicherstellen, dass Observer noch/wieder aktiv sind
		},

		onLibrariesLoadedAndDomReady: function(el_toReload) {
			this.self = this;
			this.updateSubmitLink();
			this.checkConditionsAndShowPopup();
			this.setupObserversIfNotPresent();
		},

		onLibraryLoaded: function(str_libraryURL) {
		},

		updateSubmitLink: function() {
			const submitButtonElement = document.querySelector('#'+this.idfinishingOrderSubmitBox+' .submit-box a.button');
			if (submitButtonElement && submitButtonElement.href) {
				this.currentSubmitLink = submitButtonElement.href;
			} else {
				this.currentSubmitLink = '#'; // Fallback, falls kein Link gefunden wird
			}
			// Wenn das Popup bereits existiert, den Link des Buttons aktualisieren
			if (this.popupElement) {
				const popupLinkButton = this.popupElement.querySelector('.popup-submit-button');
				if (popupLinkButton) {
					popupLinkButton.href = this.currentSubmitLink;
				}
			}
		},

		checkConditionsAndShowPopup: function() {
			const payPalCheckoutElement = document.getElementById(this.idPayPalCheckoutContainer);
			const finishingOrderSubmitBox = document.getElementById(this.idfinishingOrderSubmitBox);
			let payPalSelectedAndVerified = false;

			if (payPalCheckoutElement && payPalCheckoutElement.classList.contains('selected')) {
				payPalSelectedAndVerified = true;
			}


			let submitBoxHasContent = false;
			if (finishingOrderSubmitBox) {
				const submitButtonInBox = finishingOrderSubmitBox.querySelector('.submit-box a.button');
				if (submitButtonInBox) { // Überprüfen, ob der spezifische Button da ist
					submitBoxHasContent = true;
					this.updateSubmitLink(); // Sicherstellen, dass der Link aktuell ist
				}
			}

			if (payPalSelectedAndVerified && !submitBoxHasContent && isShippingMethodeSelected() && !hasCheckoutValidationError()) {

				if (!this.popupElementNotPayPalVerified) {
					this.createPopupNotPayPalVerified();
				}
				// Link im Popup-Button aktualisieren, bevor es angezeigt wird
				const popupLinkButton = this.popupElementNotPayPalVerified.querySelector('.popup-submit-button');
				if (popupLinkButton) {
					popupLinkButton.href = this.currentSubmitLink;
				}
				this.showPopupNotPayPalVerified();
			} else {
				if (this.popupElementNotPayPalVerified) {
					this.hidePopupNotPayPalVerified();
				}
			}

			function isShippingMethodeSelected() {
				const shippingContainer = document.querySelector('.shippingSelection');

				if (!shippingContainer) {
					console.warn("Container with class 'shippingSelection' not found.");
					return false; // Or throw an error, depending on requirements.
				}

				const selectedElement = shippingContainer.querySelector('.selected');

				if (selectedElement) {
					return false;
				} else {
					return true;
				}
			}

			function hasCheckoutValidationError() {
				const paymentContainer = document.querySelector('#checkoutStepPayment');
				if (!paymentContainer) {
					console.warn("Container with id 'checkoutStepPayment' not found.")
					return false
				}
				const errorContainer = paymentContainer.querySelector('.checkout-validation-error');

				if (!errorContainer) {
					return false
				}
				return true;
			}


			if (payPalSelectedAndVerified && submitBoxHasContent) {

				if (!this.popupElement) {
					this.createPopup();
				}
				// Link im Popup-Button aktualisieren, bevor es angezeigt wird
				const popupLinkButton = this.popupElement.querySelector('.popup-submit-button');
				if (popupLinkButton) {
					popupLinkButton.href = this.currentSubmitLink;
				}
				this.showPopup();
			} else {
				if (this.popupElement) {
					this.hidePopup();
				}
			}
		},

		createPopup: function() {
			// Popup-Element erstellen, falls noch nicht vorhanden
			if (!this.popupElement) {
				this.popupElement = document.createElement('div');
				this.popupElement.id = 'paypal-verification-info-popup';
				this.popupElement.classList.add('hidden'); // CSS steuert das Ausblenden

				const closeButtonX = document.createElement('span');
				closeButtonX.className = 'popup-close-x';
				closeButtonX.textContent = '×';
				closeButtonX.onclick = () => this.hidePopup();
				closeButtonX.setAttribute('aria-label', 'Schließen');

				const heading = document.createElement('h2');
				heading.className = 'popup-heading';
				heading.textContent = 'Ihre Bestellung abschliessen';

				const message = document.createElement('p');
				message.className = 'popup-message';
				message.textContent = 'Bitte klicken Sie auf den Button, um Ihren Bestellvorgang abzuschließen.';

				const linkButton = document.createElement('a');
				linkButton.className = 'popup-submit-button';
				linkButton.href = this.currentSubmitLink;
				linkButton.textContent = 'Weiter zur Zusammenfassung Ihrer Bestellung';

				this.popupElement.appendChild(closeButtonX);
				this.popupElement.appendChild(heading);
				this.popupElement.appendChild(message);
				this.popupElement.appendChild(linkButton);

				document.body.appendChild(this.popupElement);
			}

			if (!this.overlayElement) {
				this.overlayElement = document.createElement('div');
				this.overlayElement.id = 'paypal-verification-overlay';
				this.overlayElement.classList.add('hidden');
				this.overlayElement.onclick = () => this.hidePopup();
				document.body.appendChild(this.overlayElement);
			}
		},

		showPopup: function() {
			if (this.popupElement && this.overlayElement) {
				const popupLinkButton = this.popupElement.querySelector('a.popup-submit-button');
				if (popupLinkButton) {
					popupLinkButton.href = this.currentSubmitLink;
				}
				this.overlayElement.classList.remove('hidden');
				this.overlayElement.classList.add('visible');

				this.popupElement.classList.remove('hidden');
				this.popupElement.classList.add('visible');
				document.body.style.overflow = 'hidden';
			}
		},

		hidePopup: function() {
			if (this.popupElement && this.overlayElement) {
				this.overlayElement.classList.remove('visible');
				this.overlayElement.classList.add('hidden');

				this.popupElement.classList.remove('visible');
				this.popupElement.classList.add('hidden');

				document.body.style.overflow = '';
			}
		},

		createPopupNotPayPalVerified: function() {
			// Popup-Element erstellen, falls noch nicht vorhanden
			if (!this.popupElementNotPayPalVerified) {
				this.popupElementNotPayPalVerified = document.createElement('div');
				this.popupElementNotPayPalVerified.id = 'paypal-verification-info-popup';
				this.popupElementNotPayPalVerified.classList.add('hidden'); // CSS steuert das Ausblenden

				const closeButtonX = document.createElement('span');
				closeButtonX.className = 'popup-close-x';
				closeButtonX.textContent = '×';
				closeButtonX.onclick = () => this.hidePopupNotPayPalVerified();
				closeButtonX.setAttribute('aria-label', 'Schließen');

				const heading = document.createElement('h2');
				heading.className = 'popup-heading';
				heading.textContent = 'Fehlende Lieferoption';

				const message = document.createElement('p');
				message.className = 'popup-message';
				message.textContent = 'Ihr Bestellung ist noch nicht abgeschlossen. Bitte fügen Sie eine Lieferoption hinzu.';

				this.popupElementNotPayPalVerified.appendChild(closeButtonX);
				this.popupElementNotPayPalVerified.appendChild(heading);
				this.popupElementNotPayPalVerified.appendChild(message);

				document.body.appendChild(this.popupElementNotPayPalVerified);
			}

			if (!this.overlayElement) {
				this.overlayElement = document.createElement('div');
				this.overlayElement.id = 'paypal-verification-overlay';
				this.overlayElement.classList.add('hidden');
				this.overlayElement.onclick = () => this.hidePopupNotPayPalVerified();
				document.body.appendChild(this.overlayElement);
			}
		},

		showPopupNotPayPalVerified: function() {
			if (this.popupElementNotPayPalVerified && this.overlayElement) {
				const popupLinkButton = this.popupElementNotPayPalVerified.querySelector('a.popup-submit-button');
				if (popupLinkButton) {
					popupLinkButton.href = this.currentSubmitLink;
				}
				this.overlayElement.classList.remove('hidden');
				this.overlayElement.classList.add('visible');

				this.popupElementNotPayPalVerified.classList.remove('hidden');
				this.popupElementNotPayPalVerified.classList.add('visible');
				document.body.style.overflow = 'hidden';
			}
		},

		hidePopupNotPayPalVerified: function() {
			if (this.popupElementNotPayPalVerified && this.overlayElement) {
				this.overlayElement.classList.remove('visible');
				this.overlayElement.classList.add('hidden');

				this.popupElementNotPayPalVerified.classList.remove('visible');
				this.popupElementNotPayPalVerified.classList.add('hidden');

				document.body.style.overflow = '';
			}
		},

		setupObserversIfNotPresent: function() {
			const self = this;

			// check for mutation in class
			const payPalCheckoutElement = document.getElementById(this.idPayPalCheckoutContainer);
			if (payPalCheckoutElement && !this.observerPayPal) {
				this.observerPayPal = new MutationObserver(function(mutationsList, observer) {
					for (const mutation of mutationsList) {
						if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
							self.checkConditionsAndShowPopup();
						}
					}
				});
				this.observerPayPal.observe(payPalCheckoutElement, { attributes: true });
			}


			// Beobachter für #finishingOrderSubmitBox (Änderungen der Kindelemente)
			const finishingOrderSubmitBox = document.getElementById(this.idfinishingOrderSubmitBox);
			if (finishingOrderSubmitBox && !this.observerSubmitBox) {
				this.observerSubmitBox = new MutationObserver(function(mutationsList, observer) {
					for (const mutation of mutationsList) {
						if (mutation.type === 'childList') {
							self.updateSubmitLink();
							self.checkConditionsAndShowPopup();
						}
					}
				});
				this.observerSubmitBox.observe(finishingOrderSubmitBox, { childList: true, subtree: true }); // subtree: true, um auch Änderungen an .submit-box zu erfassen
			}
		}
	};
})();