const wcLightbox = function () {
	'use strict';

	const wcGalleryTrigger = document.querySelector('.woocommerce-product-gallery__trigger');
	if (wcGalleryTrigger) {
		const wcGallery = [];
		const productImages = document.querySelectorAll('.woocommerce-product-gallery__image a');
		productImages.forEach(function (imageLink) {
			wcGallery.push({
				href: imageLink.getAttribute('href')
			});
		});
		const wcOptions = {
			elements: wcGallery,
			autoplayVideos: false,
		}
		const wcLightbox = GLightbox(wcOptions);

		wcGalleryTrigger.addEventListener('click', function (event) {
			wcLightbox.open();
		});
	}
}
window.addEventListener('load', wcLightbox);
function initProductQtyControl() {
	const controls = document.querySelectorAll('.product-qty-control');
	if (!controls) {
		return;
	}

	controls.forEach((control) => {
		const input = control.querySelector('input.qty');
		const btnInc = control.querySelector('.product-qty-inc');
		const btnDec = control.querySelector('.product-qty-dec');
		const label = control.querySelector('.product-qty');
		const minValue = parseInt(input.getAttribute('min')) || 0;

		if (label) {
			label.textContent = input.value;
		}

		btnDec.addEventListener('click', (event) => {
			event.preventDefault();
			let value = parseInt(input.value);
			input.value = value > minValue ? value - 1 : minValue;
			input.dispatchEvent(new Event('input', { bubbles: true }));
			if (label) {
				label.textContent = input.value;
			}
		});
		btnInc.addEventListener('click', (event) => {
			event.preventDefault();
			let value = parseInt(input.value);
			input.value = value + 1;
			input.dispatchEvent(new Event('input', { bubbles: true }));
			if (label) {
				label.textContent = input.value;
			}
		});

	})
}

document.addEventListener('DOMContentLoaded', () => {
	initProductQtyControl();
	document.body.addEventListener('updated_cart_totals', () => {
		initProductQtyControl();
	});

	jQuery(document.body).on('item_removed_from_classic_cart', function () {
		initProductQtyControl();
	});
});