const cursor = document.querySelector('.cursor');
const follower = document.querySelector('.cursor-follower');

const initCursor = function () {
	document.addEventListener('mousemove', function(event) {
		this.xPos = event.clientX;
		this.yPos = event.clientY;
	});

	const renderCursor = function () {
		cursor.style.transform = 'translate(' + document.xPos + 'px, ' + document.yPos + 'px)';
		requestAnimationFrame(renderCursor);
	}

	const renderFollower = function () {
		if (follower) {
			follower.style.transform = 'translate(' + document.xPos + 'px, ' + document.yPos + 'px)';
		}
		requestAnimationFrame(renderFollower);
	}

	const handleMouseEnter = function() {
		const isWcGallery = !!this.closest('.woocommerce-product-gallery__image');
		const isLightbox = this.classList.contains('lightbox') || this.classList.contains('woocommerce-product-gallery__trigger');

		if (isLightbox || isWcGallery) {
			cursor.classList.add('image-link');
		} else {
			cursor.classList.add('link');
		}
	}

	const handleMouseLeave = function() {
		cursor.classList.remove('image-link', 'link');
	}

	const links = document.querySelectorAll('a, .dlink, .social-link, button, input[type="button"], input[type="submit"], input[type="reset"], .logo-holder');
	links.forEach(function(el) {
		el.addEventListener('mouseenter', handleMouseEnter);
		el.addEventListener('mouseleave', handleMouseLeave);
	});

	requestAnimationFrame(renderCursor);

	if (follower) {
		requestAnimationFrame(renderFollower);
	}
}

document.addEventListener('DOMContentLoaded', initCursor);