/**
 * IGIT Related Posts – Carousel controller
 *
 * Pure vanilla JS. Finds every .igit-rpwt--carousel on the page and wires up
 * the prev/next buttons with smooth horizontal scroll.
 *
 * Exposes window.igitRpwtCarousel.init() so the async loader (frontend.js)
 * can re-initialise after AJAX-injected HTML is inserted into the DOM.
 */
( function () {
	'use strict';

	function initCarousel( wrap ) {
		// Guard: skip if already wired up (safe to call init() multiple times).
		if ( wrap.getAttribute( 'data-igit-carousel-init' ) ) {
			console.log( '[igit-carousel] already initialised, skipping', wrap );
			return;
		}
		wrap.setAttribute( 'data-igit-carousel-init', '1' );

		var track   = wrap.querySelector( '.igit-rpwt__items' );
		var btnPrev = wrap.querySelector( '.igit-rpwt__carousel-prev' );
		var btnNext = wrap.querySelector( '.igit-rpwt__carousel-next' );

		console.log( '[igit-carousel] initCarousel —', { wrap: wrap, track: track, btnPrev: btnPrev, btnNext: btnNext } );

		if ( ! track ) { console.warn( '[igit-carousel] .igit-rpwt__items not found inside wrap' ); return; }
		if ( ! btnPrev ) { console.warn( '[igit-carousel] .igit-rpwt__carousel-prev not found inside wrap' ); return; }
		if ( ! btnNext ) { console.warn( '[igit-carousel] .igit-rpwt__carousel-next not found inside wrap' ); return; }

		function getScrollAmount() {
			var firstItem = track.querySelector( '.igit-rpwt__item' );
			var gap = parseInt( getComputedStyle( track ).gap || '0', 10 ) || 16;
			var amount = firstItem ? firstItem.offsetWidth + gap : 240;
			console.log( '[igit-carousel] scrollAmount', amount, '(itemW:', firstItem ? firstItem.offsetWidth : 'n/a', 'gap:', gap, ')' );
			return amount;
		}

		function updateButtons() {
			var sl   = track.scrollLeft;
			var cw   = track.clientWidth;
			var sw   = track.scrollWidth;
			console.log( '[igit-carousel] updateButtons — scrollLeft:', sl, 'clientWidth:', cw, 'scrollWidth:', sw );
			btnPrev.disabled = sl <= 1;
			// Only disable Next when there is genuinely no room to scroll further.
			btnNext.disabled = sw > 0 && ( sl + cw >= sw - 1 );
		}

		btnPrev.addEventListener( 'click', function () {
			var amt = getScrollAmount();
			console.log( '[igit-carousel] PREV clicked, scrollBy', -amt );
			track.scrollBy( { left: -amt, behavior: 'smooth' } );
		} );

		btnNext.addEventListener( 'click', function () {
			var amt = getScrollAmount();
			console.log( '[igit-carousel] NEXT clicked, scrollBy', amt );
			track.scrollBy( { left: amt, behavior: 'smooth' } );
		} );

		track.addEventListener( 'scroll', updateButtons, { passive: true } );

		// Defer initial button-state evaluation until the browser has painted
		// so that clientWidth / scrollWidth are fully computed.
		requestAnimationFrame( function () {
			requestAnimationFrame( updateButtons );
		} );
	}

	function init() {
		var found = document.querySelectorAll( '.igit-rpwt--carousel .igit-rpwt__carousel-wrap' );
		console.log( '[igit-carousel] init() — found', found.length, 'carousel wrap(s)' );
		found.forEach( initCarousel );
	}

	// Expose globally so frontend.js (async loader) can re-run after AJAX.
	window.igitRpwtCarousel = { init: init };

	if ( document.readyState === 'loading' ) {
		document.addEventListener( 'DOMContentLoaded', init );
	} else {
		init();
	}
} )();
