/**
 * Signocore Language Switcher - Frontend JavaScript
 *
 * Handles dropdown toggle, keyboard navigation, ARIA attributes,
 * and sets the language cookie before navigation.
 */
(function () {
	'use strict';

	var COOKIE_NAME = 'sml_language';
	var COOKIE_DAYS = 365;

	/**
	 * Set a cookie with the selected language code.
	 *
	 * @param {string} langCode
	 */
	function setLanguageCookie(langCode) {
		var date = new Date();
		date.setTime(date.getTime() + COOKIE_DAYS * 24 * 60 * 60 * 1000);
		document.cookie =
			COOKIE_NAME +
			'=' +
			encodeURIComponent(langCode) +
			';expires=' +
			date.toUTCString() +
			';path=/;SameSite=Lax';
	}

	/**
	 * Initialize dropdown switchers.
	 */
	function initDropdowns() {
		var switchers = document.querySelectorAll('.sml-switcher-dropdown');

		for (var i = 0; i < switchers.length; i++) {
			initDropdown(switchers[i]);
		}
	}

	/**
	 * Initialize a single dropdown switcher.
	 *
	 * @param {HTMLElement} switcher
	 */
	function initDropdown(switcher) {
		var button = switcher.querySelector('.sml-switcher-current');
		var list = switcher.querySelector('.sml-switcher-list');

		if (!button || !list) {
			return;
		}

		// Toggle on button click
		button.addEventListener('click', function (e) {
			e.preventDefault();
			e.stopPropagation();
			toggleDropdown(button, list);
		});

		// Keyboard navigation on the button
		button.addEventListener('keydown', function (e) {
			handleButtonKeydown(e, button, list);
		});

		// Keyboard navigation within the list
		list.addEventListener('keydown', function (e) {
			handleListKeydown(e, button, list);
		});

		// Close when clicking outside
		document.addEventListener('click', function (e) {
			if (!switcher.contains(e.target)) {
				closeDropdown(button, list);
			}
		});
	}

	/**
	 * Toggle a dropdown open/closed.
	 *
	 * @param {HTMLElement} button
	 * @param {HTMLElement} list
	 */
	function toggleDropdown(button, list) {
		var isOpen = button.getAttribute('aria-expanded') === 'true';

		if (isOpen) {
			closeDropdown(button, list);
		} else {
			openDropdown(button, list);
		}
	}

	/**
	 * Open a dropdown.
	 *
	 * @param {HTMLElement} button
	 * @param {HTMLElement} list
	 */
	function openDropdown(button, list) {
		button.setAttribute('aria-expanded', 'true');
		list.setAttribute('data-open', 'true');

		// Focus the first link
		var firstLink = list.querySelector('.sml-switcher-link');
		if (firstLink) {
			firstLink.focus();
		}
	}

	/**
	 * Close a dropdown.
	 *
	 * @param {HTMLElement} button
	 * @param {HTMLElement} list
	 */
	function closeDropdown(button, list) {
		button.setAttribute('aria-expanded', 'false');
		list.setAttribute('data-open', 'false');
	}

	/**
	 * Handle keydown events on the dropdown button.
	 *
	 * @param {KeyboardEvent} e
	 * @param {HTMLElement} button
	 * @param {HTMLElement} list
	 */
	function handleButtonKeydown(e, button, list) {
		switch (e.key) {
			case 'ArrowDown':
			case 'Enter':
			case ' ':
				e.preventDefault();
				openDropdown(button, list);
				break;
			case 'Escape':
				e.preventDefault();
				closeDropdown(button, list);
				button.focus();
				break;
		}
	}

	/**
	 * Handle keydown events within the dropdown list.
	 *
	 * @param {KeyboardEvent} e
	 * @param {HTMLElement} button
	 * @param {HTMLElement} list
	 */
	function handleListKeydown(e, button, list) {
		var links = list.querySelectorAll('.sml-switcher-link');
		var currentIndex = -1;

		for (var i = 0; i < links.length; i++) {
			if (links[i] === document.activeElement) {
				currentIndex = i;
				break;
			}
		}

		switch (e.key) {
			case 'ArrowDown':
				e.preventDefault();
				if (currentIndex < links.length - 1) {
					links[currentIndex + 1].focus();
				} else {
					links[0].focus();
				}
				break;

			case 'ArrowUp':
				e.preventDefault();
				if (currentIndex > 0) {
					links[currentIndex - 1].focus();
				} else {
					links[links.length - 1].focus();
				}
				break;

			case 'Escape':
				e.preventDefault();
				closeDropdown(button, list);
				button.focus();
				break;

			case 'Tab':
				closeDropdown(button, list);
				break;

			case 'Home':
				e.preventDefault();
				links[0].focus();
				break;

			case 'End':
				e.preventDefault();
				links[links.length - 1].focus();
				break;
		}
	}

	/**
	 * Initialize language link click handlers to set the cookie before navigation.
	 */
	function initLanguageLinks() {
		var links = document.querySelectorAll('.sml-switcher-link[data-lang]');

		for (var i = 0; i < links.length; i++) {
			links[i].addEventListener('click', function () {
				var langCode = this.getAttribute('data-lang');
				if (langCode) {
					setLanguageCookie(langCode);
				}
			});
		}
	}

	/**
	 * Initialize all switcher functionality when DOM is ready.
	 */
	function init() {
		initDropdowns();
		initLanguageLinks();
	}

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