/**
 * FoodOrd Theme-like sidebar spacing for third-party themes.
 *
 * Replicates the FoodOrd Theme behavior by dynamically setting:
 * - --foodord-header-h (top offset)
 * - --foodord-footer-h (informational)
 * - --foodord-bottom-gap (keeps the fixed sidebar above the footer)
 *
 * @package FoodOrd
 */
(function () {
	'use strict';

	function toPx(value) {
		return typeof value === 'number' ? value + 'px' : String(value);
	}

	function getHeight(el) {
		if (!el) {
			return 0;
		}
		const rect = el.getBoundingClientRect();
		return Math.max(0, Math.round(rect.height));
	}

	function firstMatch(selectors) {
		for (const selector of selectors) {
			const el = document.querySelector(selector);
			if (el) {
				return el;
			}
		}
		return null;
	}

	function applyVars(headerPx, footerPx, bottomGapPx) {
		let styleEl = document.getElementById('foodord-dyn-vars');
		if (!styleEl) {
			styleEl = document.createElement('style');
			styleEl.id = 'foodord-dyn-vars';
			styleEl.type = 'text/css';
			document.head.appendChild(styleEl);
		}
		const css = `:root{--foodord-header-h:${headerPx};--foodord-footer-h:${footerPx};--foodord-bottom-gap:${bottomGapPx};}`;
		if (styleEl.textContent !== css) {
			styleEl.textContent = css;
		}
	}

	function setVars() {
		const sidebar = document.querySelector('.foodord-sidebar');
		if (!sidebar) {
			return;
		}

		const header = document.getElementById('masthead') || firstMatch([
			'header#masthead',
			'header.site-header',
			'[role="banner"]',
			'header'
		]);
		const footer = document.getElementById('colophon') || firstMatch([
			'footer#colophon',
			'footer.site-footer',
			'[role="contentinfo"]',
			'footer'
		]);
		const footerBottom = document.getElementById('footer-bottom');
		const adminbar = document.getElementById('wpadminbar');

		let headerBottom = 0;
		if (header) {
			const hb = header.getBoundingClientRect().bottom;
			headerBottom = Math.max(0, Math.round(hb));
		}

		let adminH = 0;
		if (adminbar && window.getComputedStyle(adminbar).position === 'fixed') {
			adminH = getHeight(adminbar);
		}

		const footerH = getHeight(footer);
		const topOffset = Math.max(headerBottom, adminH);

		const vh = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
		const scrollY = window.scrollY || window.pageYOffset || 0;
		let nearestAbsTop = Infinity;
		if (footer) {
			const r1 = footer.getBoundingClientRect();
			nearestAbsTop = Math.min(nearestAbsTop, r1.top + scrollY);
		}
		if (footerBottom) {
			const r2 = footerBottom.getBoundingClientRect();
			nearestAbsTop = Math.min(nearestAbsTop, r2.top + scrollY);
		}
		let bottomGap = 0;
		if (nearestAbsTop !== Infinity) {
			bottomGap = Math.max(0, Math.round((scrollY + vh) - nearestAbsTop));
		}

		applyVars(toPx(topOffset), toPx(footerH || 0), toPx(bottomGap));
	}

	function debounce(fn, wait) {
		let t;
		return function () {
			window.clearTimeout(t);
			const args = arguments;
			t = window.setTimeout(function () {
				fn.apply(null, args);
			}, wait);
		};
	}

	document.addEventListener('DOMContentLoaded', function () {
		setVars();
	});

	window.addEventListener('load', function () {
		setVars();
	});

	window.addEventListener('resize', debounce(setVars, 150));

	let ticking = false;
	window.addEventListener(
		'scroll',
		function () {
			if (ticking) {
				return;
			}
			ticking = true;
			window.requestAnimationFrame(function () {
				setVars();
				const sidebar = document.querySelector('.foodord-sidebar');
				if (sidebar) {
					const scrolled = window.pageYOffset || document.documentElement.scrollTop || 0;
					if (scrolled > 0) {
						sidebar.classList.add('is-stuck');
					} else {
						sidebar.classList.remove('is-stuck');
					}
				}
				ticking = false;
			});
		},
		{ passive: true }
	);
})();
