/**
 * Per-page front-end inits (re-run after each Barba enter, scoped to container).
 * Cheap selector gates + dynamic imports for heavy / rare modules.
 */
import { ScrollTrigger } from 'gsap/ScrollTrigger';
import { has } from './runtime.js';
import { registerPageCleanup, runPageCleanups } from './pageCleanup.js';
import initTextReveal from './textReveal.js';
import initCheckSectionThemeScroll from './checkSectionThemeScroll.js';
import initAccordionCSS from './accordionCss.js';
import initBannerMask from './bannerMask.js';
import initMediaReveal from './mediaReveal.js';
import initGlobalParallax, { destroyGlobalParallax } from './globalParallax.js';
import initPlayPauseVideoScroll from './playPauseVideo.js';
import initScrollToAnchorLenis from './scrollToAnchorLenis.js';
import { destroyDraggableMarquee } from './draggableMarquee.js';
import { destroyScrollGallery } from './scrollGallery.js';

function trackCleanup(scope, cleanup) {
	if (typeof cleanup === 'function') registerPageCleanup(scope, cleanup);
}

async function loadModule(scope, loader, ...args) {
	const mod = await loader();
	const cleanup = mod.default(scope, ...args);
	trackCleanup(scope, cleanup);
}

export async function initPageModules(lenis, root) {
	const scope = root || document;
	const tasks = [];

	// Core path — present on most pages, keep sync for first paint / ST refresh.
	if (has('[js-reveal]', scope)) initTextReveal(scope);
	if (has('[data-media-reveal]', scope)) initMediaReveal(scope);
	if (has('[data-section-mask], [data-banner-mask]', scope)) {
		trackCleanup(scope, initBannerMask(scope));
	}
	if (has('[data-parallax="trigger"]', scope)) initGlobalParallax(scope);
	if (has('[data-video="playpause"]', scope)) initPlayPauseVideoScroll(scope);
	if (has('[data-accordion-css-init]', scope)) initAccordionCSS(scope);

	if (has('[data-filter-group]', scope)) {
		tasks.push(loadModule(scope, () => import('./filterBasic.js')));
	}
	if (has('[data-toggle-init]', scope)) {
		tasks.push(loadModule(scope, () => import('./toggleSwitch.js')));
	}

	initCheckSectionThemeScroll(lenis);
	initScrollToAnchorLenis(lenis, scope);

	// Heavy / optional — code-split so unused pages skip parse + deps.
	if (has('[data-mini-showreel-open]', scope)) {
		tasks.push(loadModule(scope, () => import('./miniShowreel.js')));
	}
	if (has('[data-bunny-player-init]', scope)) {
		tasks.push(loadModule(scope, () => import('./bunnyPlayer.js')));
	}
	if (has('[data-orbit-tiles-init]', scope)) {
		tasks.push(loadModule(scope, () => import('./orbitTiles.js')));
	}
	if (has('[data-scroll-gallery-init]', scope)) {
		tasks.push(loadModule(scope, () => import('./scrollGallery.js'), lenis));
	}
	if (has('[data-tabs="wrapper"]', scope)) {
		tasks.push(loadModule(scope, () => import('./tabsScroll.js')));
	}
	if (has('[data-draggable-marquee-init]', scope)) {
		tasks.push(loadModule(scope, () => import('./draggableMarquee.js')));
	}

	if (tasks.length) await Promise.all(tasks);

	ScrollTrigger.refresh();
}

export function destroyPageModules(root) {
	runPageCleanups(root);
	destroyDraggableMarquee(root);
	destroyScrollGallery(root);
	destroyGlobalParallax();
}
