/**
 * Custom JS for theme elements
 */
(function () {
	'use strict';

	const warbleTheme = {

		init() {
			this.wrapProductInfo();
			this.sectionAnimation();
			this.cartUpdate();
			this.cartButtonText();
			this.mobileMenuPadding();
			this.resetPasswordTitle();
			this.headerSearch();
			this.bannerAnimation();
		},

		// WRAP product title and price in new div for latest product section
		wrapProductInfo() {
			const warbleproductDivs = document.querySelectorAll('.wp-block-latest-products .wc-block-grid__product');

			if (warbleproductDivs.length) {
				warbleproductDivs.forEach((productDiv) => {

					const warbleproductInfoWrap = document.createElement('div');
					warbleproductInfoWrap.classList.add('wp-product-info-wrap');

					const warbleproductLink = productDiv.querySelector('.wc-block-grid__product-link');
					const warbleproductPrice = productDiv.querySelector('.wc-block-grid__product-price');
					const warbleproductTitle = productDiv.querySelector('.wc-block-grid__product-title');
					const warbleproductReview = productDiv.querySelector('.wc-block-grid__product-rating');

					if (warbleproductTitle) {
						warbleproductInfoWrap.appendChild(warbleproductTitle);
					}

					if (warbleproductPrice) {
						warbleproductInfoWrap.appendChild(warbleproductPrice);
					}

					if (warbleproductReview) {
						warbleproductInfoWrap.appendChild(warbleproductReview);
					}

					if (warbleproductLink) {
						warbleproductLink.after(warbleproductInfoWrap);
					}

				});
			}
		},

		// ADD CLASS WHILE SECTION IS IN VIEWPORT
		isInViewport(elem) {
			const warbledistance = elem.getBoundingClientRect();
			const warblewindowHeight = window.innerHeight || document.documentElement.clientHeight;

			const warbleelementMiddle = warbledistance.top + warbledistance.height / 2;

			return (
				warbleelementMiddle >= 0 &&
				warbleelementMiddle <= warblewindowHeight
			);
		},

		sectionAnimation() {
			const warblesections = document.querySelectorAll('.wp-block-section');

			if (warblesections.length) {
				window.addEventListener('scroll', () => {

					warblesections.forEach((element) => {

						if (this.isInViewport(element)) {
							element.classList.add('transition-element');
						}

					});

				});
			}
		},

		// CART UPDATE
		cartUpdate() {
			const warblebtnTrigger = document.querySelector('button[name="update_cart"]');

			if (warblebtnTrigger) {
				warblebtnTrigger.addEventListener('click', () => {

					setTimeout(() => {

						if (typeof warbleQtyChange === 'function') {
							warbleQtyChange();
						}

						this.cartUpdate();

					}, 5000);

				});
			}
		},

		// Added to cart text change
		cartButtonText() {
			const warblecartBtn = document.querySelectorAll('.add_to_cart_button');

			if (warblecartBtn.length) {
				warblecartBtn.forEach((item) => {

					item.addEventListener('click', () => {
						item.innerHTML = 'added to cart';
					});

				});
			}
		},

		// Sticky Mobile Icon Menu body padding bottom
		mobileMenuPadding() {
			const warblefixedMenu = document.querySelector('.wp-mobile-icon-menu');

			if (warblefixedMenu) {
				document.body.style.paddingBottom = `${warblefixedMenu.clientHeight}px`;
			}
		},

		// Reset password title
		resetPasswordTitle() {
			const warbleelement = document.querySelector('.woocommerce-lost-password .woocommerce');
			const warblechild = document.querySelector('.lost_reset_password');

			if (warbleelement && warblechild) {

				const warblepassTitle = document.createElement('h3');
				warblepassTitle.textContent = 'Reset Your Password';

				warbleelement.insertBefore(warblepassTitle, warblechild);
			}
		},

		// HEADER PRODUCT SEARCH
		headerSearch() {

			const warblemobileIcon = document.querySelector('.wp-mobile-icon-menu');
			const warblesearchCont = document.querySelectorAll('.wp-header-right');
			const warblesearchDummy = document.querySelector('.dummy-icon .wp-block-search__button');
			const warblesearchWrap = document.querySelector('.wp-header-right .search-product');

			const warbleShowHandler = (e) => {

				e.preventDefault();
				document.body.classList.toggle('search-toggle');

			};

			if (warblesearchCont.length) {

				const warblesearchBtn = document.querySelectorAll('.dummy-icon .wp-block-search__button');

				warblesearchBtn.forEach((btn) => {
					btn.addEventListener('click', warbleShowHandler);
				});

				if (warblemobileIcon && warblesearchWrap && warblesearchDummy) {

					document.addEventListener('click', (e) => {

						if (
							!warblesearchWrap.contains(e.target) &&
							!warblesearchDummy.contains(e.target) &&
							!warblemobileIcon.contains(e.target)
						) {
							document.body.classList.remove('search-toggle');
						}

					});
				}
			}
		},

		// Header media load animation
		bannerAnimation() {
			const warblebannerWrap = document.querySelector('.wp-block-custom-header-media');

			if (warblebannerWrap) {
				setTimeout(() => {
					warblebannerWrap.classList.add('show');
				}, 200);
			}
		},


	};

	document.addEventListener('DOMContentLoaded', () => {
		warbleTheme.init();
	});

})();