/**
 * The JavaScript code you place here will be processed by esbuild, and the
 * output file will be created at `../theme/js/script.min.js` and enqueued by
 * default in `../theme/functions.php`.
 *
 * For esbuild documentation, please see:
 * https://esbuild.github.io/
 */
import { gsap } from 'gsap';
import { ScrollToPlugin } from 'gsap/ScrollToPlugin.js';
import Lenis from 'lenis';
import Rellax from 'rellax';

gsap.registerPlugin(ScrollToPlugin);

import Swiper from 'swiper/bundle';
import { clickEvents } from './dom';
import { screen } from './screen';
import { backToTop } from './backToTop';
import { flexGap } from './flex-gap';
import { videoBackground } from './video-background';

// Initialize Lenis smooth scroll
const lenis = new Lenis({
  duration: 1.2,
  easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
  direction: 'vertical',
  gestureDirection: 'vertical',
  smooth: true,
  mouseMultiplier: 1,
  smoothTouch: false,
  touchMultiplier: 2,
  infinite: false,
});

// Connect Lenis to GSAP's ScrollTrigger
function raf(time) {
  lenis.raf(time);
  requestAnimationFrame(raf);
}
requestAnimationFrame(raf);

// Update screen parallax on Lenis scroll
lenis.on('scroll', () => {
  window.dispatchEvent(new Event('ljn:scroll'));
});

document.addEventListener('DOMContentLoaded', () => {
	let rellax = null;
	const initRellax = () => {
		if (window.innerWidth < 1024) {
			if (rellax) {
				rellax.destroy();
				rellax = null;
			}
			return;
		}
		if (rellax) {
			return;
		}
		rellax = new Rellax('.rellax', {
			// center: true,
			// wrapper:'.rellax-container'
		});
	};

	initRellax();
	window.addEventListener('resize', initRellax, { passive: true });

	flexGap();
	clickEvents();
	screen();
	backToTop('.js-back-to-top');
	videoBackground();
	new Swiper('.swiper-galerie', {
		slidesPerView: 1,
		loop: true,
		navigation: {
			nextEl: '.swiper-button-next',
			prevEl: '.swiper-button-prev',
		},
	});
});
