(function () {
	'use strict';

	function initTabs(wrapper) {
		var tabs = wrapper.querySelectorAll(':scope > .upcoming-events-tabs > .upcoming-events-tab');
		var panels = wrapper.querySelectorAll(':scope > .upcoming-events-panel');

		tabs.forEach(function (tab) {
			tab.addEventListener('click', function () {
				var target = tab.getAttribute('data-tab');

				tabs.forEach(function (t) {
					var active = t === tab;
					t.classList.toggle('is-active', active);
					t.setAttribute('aria-selected', active ? 'true' : 'false');
					t.tabIndex = active ? 0 : -1;
				});

				panels.forEach(function (panel) {
					panel.classList.toggle('is-active', panel.getAttribute('data-panel') === target);
				});
			});

			tab.addEventListener('keydown', function (e) {
				if (e.key === 'Enter' || e.key === ' ') {
					e.preventDefault();
					tab.click();
				}
			});
		});
	}

	function initPastPagination(list) {
		var perPage = parseInt(list.getAttribute('data-per-page'), 10) || 25;
		var items = Array.prototype.slice.call(list.querySelectorAll(':scope > .upcoming-event-item'));
		var total = items.length;
		if (!total) {
			return;
		}

		var bar = list.parentElement.querySelector(':scope > .upcoming-events-pagination');
		if (!bar) {
			return;
		}

		var pageCount = Math.max(1, Math.ceil(total / perPage));
		var currentPage = 1;

		var firstBtn = bar.querySelector('.uep-first');
		var prevBtn = bar.querySelector('.uep-prev');
		var nextBtn = bar.querySelector('.uep-next');
		var lastBtn = bar.querySelector('.uep-last');
		var shownEl = bar.querySelector('.uep-shown');
		var totalEl = bar.querySelector('.uep-total');

		if (totalEl) {
			totalEl.textContent = total;
		}

		function render() {
			var start = (currentPage - 1) * perPage;
			var end = Math.min(start + perPage, total);

			items.forEach(function (item, index) {
				item.style.display = (index >= start && index < end) ? '' : 'none';
			});

			if (shownEl) {
				shownEl.textContent = end;
			}

			var atFirst = currentPage === 1;
			var atLast = currentPage === pageCount;

			if (firstBtn) firstBtn.disabled = atFirst;
			if (prevBtn) prevBtn.disabled = atFirst;
			if (nextBtn) nextBtn.disabled = atLast;
			if (lastBtn) lastBtn.disabled = atLast;
		}

		if (firstBtn) {
			firstBtn.addEventListener('click', function () {
				currentPage = 1;
				render();
			});
		}
		if (prevBtn) {
			prevBtn.addEventListener('click', function () {
				currentPage = Math.max(1, currentPage - 1);
				render();
			});
		}
		if (nextBtn) {
			nextBtn.addEventListener('click', function () {
				currentPage = Math.min(pageCount, currentPage + 1);
				render();
			});
		}
		if (lastBtn) {
			lastBtn.addEventListener('click', function () {
				currentPage = pageCount;
				render();
			});
		}

		render();
	}

	document.addEventListener('DOMContentLoaded', function () {
		document.querySelectorAll('.upcoming-events-wrapper').forEach(initTabs);
		document.querySelectorAll('.past-events-list').forEach(initPastPagination);
	});
})();
