import { gsap } from 'gsap';

let $els = null;

const backToTop = (str) => {
	$els = document.querySelectorAll(str);
	addListeners($els);
};

const addListeners = ($els) => {
	$els.forEach((el) => {
		el.addEventListener('click', () => {
			gsap.to(window, { duration: 1, scrollTo: 0 });
		});
	});

	window.addEventListener('scroll', () => {
		let b = window.scrollY > window.innerHeight ? true : false;

		$els.forEach((el) => {
			if (b) {
				el.dataset.open = true;
				el.classList.add('active');
			} else {
				delete el.dataset.open;
				el.classList.remove('active');
			}
		});
	});
};

export { backToTop };
