jQuery(document).ready(function($) {
	// Helper to set cookie
	function setCookie(name, value, days) {
		var expires = "";
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			expires = "; expires=" + date.toUTCString();
		}
		document.cookie = name + "=" + (value || "")  + expires + "; path=/";
	}

	// Helper to get cookie
	function getCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	// Language switcher dropdown toggle
	$(document).on('click', '.teknotok-ait-selected-lang', function(e) {
		e.stopPropagation();
		$(this).closest('.teknotok-ait-switcher-container').toggleClass('open');
	});

	// Close dropdown when clicking elsewhere
	$(document).on('click', function() {
		$('.teknotok-ait-switcher-container').removeClass('open');
	});

	// Language selection action
	$(document).on('click', '.teknotok-ait-languages-dropdown li[data-lang]', function() {
		var selectedLang = $(this).data('lang');
		
		// Save choice in cookie (1 year expiration)
		setCookie('teknotok_ait_language', selectedLang, 365);
		
		// Redirect to localized URL
		if (window.teknotokAitSwitcher && window.teknotokAitSwitcher.urls && window.teknotokAitSwitcher.urls[selectedLang]) {
			window.location.href = window.teknotokAitSwitcher.urls[selectedLang];
		} else {
			var url = new URL(window.location.href);
			url.searchParams.set('lang', selectedLang);
			window.location.href = url.toString();
		}
	});

	// Browser language redirection prompt logic
	var $prompt = $('#teknotok-ait-redirect-prompt');
	if ($prompt.length > 0) {
		var dismissCookie = getCookie('teknotok_ait_redirect_prompt');
		if (dismissCookie !== 'dismissed') {
			var targets = $prompt.data('targets');
			var prompts = $prompt.data('prompts');
			var browserLang = (navigator.language || navigator.userLanguage).substring(0, 2);
			
			if (targets && targets[browserLang]) {
				var langName = targets[browserLang];
				
				// Apply localized prompt text and button labels if available
				var promptData = (prompts && prompts[browserLang]) ? prompts[browserLang] : null;
				if (promptData) {
					$prompt.find('.teknotok-ait-banner-text').html(promptData.text);
					$('#teknotok-ait-btn-switch').text(promptData.switch);
					$('#teknotok-ait-btn-dismiss').text(promptData.dismiss);
				} else {
					// Fallback to replacing placeholder in default HTML string
					var htmlText = $prompt.find('.teknotok-ait-banner-text').html();
					var updatedText = htmlText.replace('%lang_name%', '<strong>' + langName + '</strong>');
					$prompt.find('.teknotok-ait-banner-text').html(updatedText);
				}

				// Show the banner
				$prompt.fadeIn(300);

				// Switch Action
				$('#teknotok-ait-btn-switch').on('click', function() {
					setCookie('teknotok_ait_language', browserLang, 365);
					setCookie('teknotok_ait_redirect_prompt', 'dismissed', 30);
					if (window.teknotokAitSwitcher && window.teknotokAitSwitcher.urls && window.teknotokAitSwitcher.urls[browserLang]) {
						window.location.href = window.teknotokAitSwitcher.urls[browserLang];
					} else {
						var url = new URL(window.location.href);
						url.searchParams.set('lang', browserLang);
						window.location.href = url.toString();
					}
				});

				// Dismiss Action
				$('#teknotok-ait-btn-dismiss').on('click', function() {
					setCookie('teknotok_ait_redirect_prompt', 'dismissed', 30);
					$prompt.fadeOut(200);
				});
			}
		}
	}
});
