/**
 * Check Section Theme on Scroll — singleton (safe with Barba).
 * Cached DOM refs; single rAF; Lenis-only scroll when available.
 * Skips while underlay menu is open (transform breaks sticky rects);
 * re-syncs on underlay-nav:closed.
 */
let wired = false;
let lenisOff = null;
let currentTheme = null;
let currentBg = null;
let ticking = false;

let navBarHeightEl = null;
let themeNavElements = null;
let bgNavElements = null;
let container = null;

function cacheDom() {
	navBarHeightEl = document.querySelector('[data-nav-bar-height]');
	themeNavElements = document.querySelectorAll('[data-theme-nav]');
	bgNavElements = document.querySelectorAll('[data-bg-nav]');
	container = document.querySelector('[data-barba="container"]') || document;
}

function updateElements(elements, attribute, value) {
	for (let i = 0; i < elements.length; i++) {
		elements[i].setAttribute(attribute, value);
	}
}

function isMenuBusy() {
	const status = document.body.getAttribute('data-menu-status');
	return status === 'open' || status === 'closing';
}

function checkThemeSection() {
	ticking = false;
	if (!container) cacheDom();

	// Menu translates [data-main] — sticky section rects are unreliable.
	if (isMenuBusy()) return;

	const themeObserverOffset = navBarHeightEl ? navBarHeightEl.offsetHeight / 2 : 0;
	const themeSections = container.querySelectorAll('[data-theme-section]');

	for (let i = 0; i < themeSections.length; i++) {
		const themeSection = themeSections[i];
		const rect = themeSection.getBoundingClientRect();
		if (rect.top > themeObserverOffset || rect.bottom < themeObserverOffset) continue;

		const themeSectionActive = themeSection.getAttribute('data-theme-section');
		const bgSectionActive = themeSection.getAttribute('data-bg-section');

		if (themeSectionActive !== currentTheme && themeNavElements) {
			updateElements(themeNavElements, 'data-theme-nav', themeSectionActive);
			currentTheme = themeSectionActive;
		}

		if (bgSectionActive && bgSectionActive !== currentBg && bgNavElements) {
			updateElements(bgNavElements, 'data-bg-nav', bgSectionActive);
			currentBg = bgSectionActive;
		}

		break;
	}
}

function requestCheck() {
	if (ticking) return;
	ticking = true;
	requestAnimationFrame(checkThemeSection);
}

/** Force re-read of data-theme-section (e.g. mid-section theme flip / menu close). */
export function refreshSectionTheme() {
	currentTheme = null;
	currentBg = null;
	checkThemeSection();
}

export default function initCheckSectionThemeScroll(lenis) {
	cacheDom();

	if (!wired) {
		wired = true;
		// With Lenis, native scroll is redundant — only fall back without it.
		if (!lenis) {
			window.addEventListener('scroll', requestCheck, { passive: true });
		}
		window.addEventListener('resize', requestCheck, { passive: true });
		// Re-apply section theme after underlay menu closes (same as static dark galleries).
		window.addEventListener('underlay-nav:closed', refreshSectionTheme);
	}

	if (lenisOff) {
		lenisOff();
		lenisOff = null;
	}

	if (lenis) {
		lenis.on('scroll', requestCheck);
		lenisOff = () => {
			if (typeof lenis.off === 'function') lenis.off('scroll', requestCheck);
		};
	}

	currentTheme = null;
	currentBg = null;
	checkThemeSection();
}
