/**
 * Custom JS for theme elements
 */

// Wocommerce active class for category list.
let url     = window.location.href;
let catLink = document.querySelectorAll(
	".wc-block-product-categories-list li a"
);
catLink.forEach((item) => {
	if (item.href === url) {
		item.classList.add("active");
	}
});


// wp-block-main-header Product Search.
let toggleButton = document.querySelector(".wp-block-main-header .wp-search-toggle .icon-search");
let toggleWrap = document.querySelector(".wp-block-main-header .wp-search-toggle");
if (toggleButton !== null) {
	toggleButton.addEventListener("click", function (e) {
		e.stopPropagation();
		document.body.classList.add("open-search");
		toggleWrap.classList.add("toggled-on");
		let toogleContainer = document.querySelector(".wp-block-main-header .wp-custom-product-search-container");
		toogleContainer.addEventListener("click", function (e) {
			e.stopPropagation();
		});
		let toggleInput = document.querySelector(".wp-block-main-header .wp-block-search__input");
		window.setTimeout(() => toggleInput.focus(), 0);
	});
}
let toggleButtonClose = document.querySelector( ".wp-block-main-header .wp-search-toggle .icon-close");
if (toggleButtonClose !== null) {
	toggleButtonClose.addEventListener("click", function (e) {
		document.body.classList.remove("open-search");
		toggleWrap.classList.remove("toggled-on");
	});
}

document.addEventListener('click', function (event) {
	document.body.classList.remove("open-search");
	toggleWrap.classList.remove("toggled-on");
});

// Function to Add/remove class for mobile Woocommerce sidebar Widgets/filters
const toggleFunc = (targetElem) => {
	let filterButtons = document.querySelectorAll(targetElem);

	filterButtons.forEach((filterButton) => {
		if (filterButton !== null) {
			filterButton.addEventListener("click", () => {
				if (filterButton.classList.contains("toggled-on")) {
					// if has 'toggled-on' class remove class
					filterButton.classList.remove("toggled-on");
				} else {
					// otherwise add 'toggled-on' class
					filterButton.classList.add("toggled-on");
				}
			});
		}
	});
};

toggleFunc(".btn-mobile-filters");

// 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();
