/**
 * Custom JS for theme elements
 */

/**
 * Scroll to JS for scroll button on header
 */
const rhythmicEventSection = document.querySelector(".events-content-wrapper");

if (rhythmicEventSection !== null) {
	let isDown = false;
	let startX;
	let scrollLeft;

	rhythmicEventSection.addEventListener("mousedown", (e) => {
		isDown = true;
		rhythmicEventSection.classList.add("active");
		startX = e.pageX - rhythmicEventSection.offsetLeft;
		scrollLeft = rhythmicEventSection.scrollLeft;
	});
	rhythmicEventSection.addEventListener("mouseleave", () => {
		isDown = false;
		rhythmicEventSection.classList.remove("active");
	});
	rhythmicEventSection.addEventListener("mouseup", () => {
		isDown = false;
		rhythmicEventSection.classList.remove("active");
	});
	rhythmicEventSection.addEventListener("mousemove", (e) => {
		if (!isDown) return;

		e.preventDefault();

		let x = e.pageX - rhythmicEventSection.offsetLeft;
		let walk = (x - startX) * 3; //scroll-fast

		rhythmicEventSection.scrollLeft = scrollLeft - walk;
	});
}

let elem = document.querySelector(".sticky-playlist.wp-block-section");
let body = document.body;
if (elem) {
	body.classList.add("has-sticky-playlist");
}

// RESIZE FUNCTION
window.addEventListener("resize", onResizeFunction);

// LOAD FUNCTION
window.addEventListener("load", onResizeFunction);

function onResizeFunction(e) {
	//do whatever you want to do on resize event
	let serviceImage = document.querySelector(".service-image");
	let serviceContent = document.querySelectorAll(".service-content");

	if (serviceImage !== null && serviceContent !== null) {
		let serviceImageClientHeight = serviceImage.clientHeight;

		serviceContent.forEach((item) => {
			item.style.minHeight = `${serviceImageClientHeight}px`;
			item.style.minHeight <= `300px` && (item.style.overflowY = "scroll");
			item.style.minHeight <= `300px` && (item.style.paddingTop = `50px`);
		});
	}
}

// Plus minus function added on product input counter
function wooQtyChange() {
	let qtyWrap = document.querySelectorAll('.quantity');
	qtyWrap.forEach((wrap) => {
		let qNav = document.createElement('div');
		let qUp = document.createElement('div');
		let qDown = document.createElement('div');
		let input = wrap.querySelectorAll('.qty');
		qUp.innerHTML = '+';
		qDown.innerHTML = '-';
		qNav.appendChild(qUp);
		qNav.appendChild(qDown);
		wrap.appendChild(qNav)
		qUp.setAttribute('class', 'quantity-button quantity-up');
		qDown.setAttribute('class', 'quantity-button quantity-down');
		max = 99999;
		let min = '';
		input.forEach((inputItem) => {
			min = inputItem.getAttribute('min');
			if (qtyWrap.length > 1) {
				// Set default value to 0
				inputItem.value = 0;
			}
			qNav.setAttribute('class', 'quantity-nav');
			let btnUp = wrap.querySelectorAll('.quantity-up');
			btnUp.forEach((btnItem) => {
				btnItem.addEventListener('click', function () {
					let oldValue = parseFloat(inputItem.value);
					if (oldValue >= max) {
						var newVal = oldValue;
					} else {
						var newVal = oldValue + 1;
					}
					inputItem.value = newVal
					var element = document.createEvent('HTMLEvents');
					var event = new Event('change', {
						bubbles: true
					});
					inputItem.dispatchEvent(event);
					return event;
				})
			})
			let btnDown = wrap.querySelectorAll('.quantity-down');
			btnDown.forEach((btnItem) => {
				btnItem.addEventListener('click', function () {
					let oldValue = parseFloat(inputItem.value);
					if (oldValue >= max) {
						var newVal = oldValue;
					} else if (oldValue <= 0) {
						var newVal = 0;
					} else {
						var newVal = oldValue - 1;
					}
					inputItem.value = newVal
					var element = document.createEvent('HTMLEvents');
					var event = new Event('change', {
						bubbles: true
					});
					inputItem.dispatchEvent(event);
					return event;
				})
			})
		})
	})
}
wooQtyChange();

function wooCartUpdate() {
	let btnTrigger = document.querySelector('button[name="update_cart"]');
	if (btnTrigger !== null) {
		btnTrigger.addEventListener('click', function () {
			setTimeout(function () {
				wooQtyChange();
			}, 5000);
			setTimeout(function () {
				wooCartUpdate();
			}, 5000);
		});
	}
}
wooCartUpdate();
