(function($){
	"use strict";

	function openItem($li,duration){
		if($li.hasClass('is-open')||$li.hasClass('is-opening')){return;}
		$li.removeClass('is-closing').addClass('is-opening is-open');
		setTimeout(function(){
			$li.removeClass('is-opening');
		},duration);
		$li.children('a').attr('aria-expanded','true');
	}

	function closeItem($li,duration){
		if(!$li.hasClass('is-open')){return;}
		$li.removeClass('is-opening').addClass('is-closing');
		$li.children('a').attr('aria-expanded','false');
		setTimeout(function(){
			$li.removeClass('is-open is-closing');
		},duration);
	}

	function closeSiblings($li,duration){
		$li.siblings('.is-open').each(function(){
			closeItem($(this),duration);
		});
	}

	function clearCloseTimer($li){
		var t=$li.data('unlockafeMegaCloseTimer');
		if(t){
			clearTimeout(t);
			$li.removeData('unlockafeMegaCloseTimer');
		}
	}

	function scheduleClose($li,duration,delay){
		clearCloseTimer($li);
		var t=setTimeout(function(){
			closeItem($li,duration);
		},delay);
		$li.data('unlockafeMegaCloseTimer',t);
	}

	function isMobile(){
		return window.matchMedia && window.matchMedia('(max-width: 767px)').matches;
	}

	function isTablet(){
		return window.matchMedia && window.matchMedia('(min-width: 768px) and (max-width: 1024px)').matches;
	}

	function initCategoryFilter($root){
		var $panel=$root.find('.unlockafe-mega-menu__grid-panel').first();
		if(!$panel.length){return;}
		var $tabs=$panel.find('.unlockafe-mm-tabs');
		if(!$tabs.length){return;}
		var $items=$panel.find('.unlockafe-mm-item[data-category]');
		if(!$items.length){return;}
		$tabs.off('click.unlockafeMmTab').on('click.unlockafeMmTab','.unlockafe-mm-tab',function(e){
			e.preventDefault();
			var cat=$(this).data('category')||'';
			$tabs.find('.unlockafe-mm-tab').removeClass('is-active').attr('aria-selected','false');
			$(this).addClass('is-active').attr('aria-selected','true');
			$items.each(function(){
				var itemCat=$(this).data('category')||'';
				$(this).toggleClass('unlockafe-mm-hidden',cat!==''&&itemCat!==cat);
			});
		});
		$tabs.find('.unlockafe-mm-tab').first().attr('aria-selected','true');
	}

	function setMobileOpen($root,open){
		$root.toggleClass('is-mobile-open',!!open);
		var $toggle=$root.children('.unlockafe-mega-menu__toggle').first();
		if($toggle.length){
			$toggle.attr('aria-expanded',open ? 'true' : 'false');
		}
		// Force list visibility on mobile to avoid theme CSS overriding our display rules.
		if(isMobile()){
			var $list=$root.children('.unlockafe-mega-menu__list').first();
			if($list.length){
				$list.css('display',open ? 'flex' : 'none');
			}
		}
		if(!open){
			$root.find('li.is-open').each(function(){closeItem($(this),parseInt($root.data('duration'),10)||250);});
		}
	}

	function init($root){
		var trigger=$root.data('trigger')||'hover';
		var duration=parseInt($root.data('duration'),10);
		if(isNaN(duration)){duration=250;}
		duration=Math.max(0,Math.min(2000,duration));
		$root.addClass('unlockafe-mm-js');
		$root[0].style.setProperty('--unlockafe-mm-duration',duration+'ms');
		var cols=parseInt($root.data('columns'),10);
		if(!isNaN(cols)){
			cols=Math.max(1,Math.min(6,cols));
			$root[0].style.setProperty('--unlockafe-mm-columns',String(cols));
		}

		$root.find('li.unlockafe-mega-menu__has-children').each(function(){
			var $li=$(this);
			var $a=$li.children('a');
			$a.attr('aria-haspopup','true');
			$a.attr('aria-expanded','false');
		});

		// Mobile hamburger toggle
		$root.off('click.unlockafeMegaToggle').on('click.unlockafeMegaToggle','.unlockafe-mega-menu__toggle',function(e){
			e.preventDefault();
			setMobileOpen($root,!$root.hasClass('is-mobile-open'));
		});

		// Mobile: tap-to-toggle submenus regardless of trigger setting
		$root.off('click.unlockafeMegaMobileSub').on('click.unlockafeMegaMobileSub','li.unlockafe-mega-menu__has-children > a',function(e){
			if(!isMobile()){
				return;
			}
			e.preventDefault();
			var $li=$(this).parent();
			if($li.hasClass('is-open')){
				closeItem($li,duration);
			}else{
				closeSiblings($li,duration);
				openItem($li,duration);
			}
		});

		// Reset mobile state when resizing up
		if(window.matchMedia){
			var mq=window.matchMedia('(max-width: 767px)');
			var handler=function(){
				if(!mq.matches){
					setMobileOpen($root,false);
				}
			};
			if(mq.addEventListener){
				mq.addEventListener('change',handler);
			}else if(mq.addListener){
				mq.addListener(handler);
			}
		}

		if(trigger==='click'){
			$root.on('click','li.unlockafe-mega-menu__has-children > a',function(e){
				if(isMobile()){
					return;
				}
				e.preventDefault();
				var $li=$(this).parent();
				if($li.hasClass('is-open')){
					closeItem($li,duration);
				}else{
					closeSiblings($li,duration);
					openItem($li,duration);
				}
			});

			$(document).on('click.unlockafeMegaMenu',function(e){
				if($(e.target).closest($root).length){return;}
				$root.find('li.is-open').each(function(){closeItem($(this),duration);});
			});
		}else{
			var closeDelay=160;
			$root.on('mouseenter','li.unlockafe-mega-menu__has-children',function(){
				if(isMobile()){
					return;
				}
				var $li=$(this);
				clearCloseTimer($li);
				closeSiblings($li,duration);
				openItem($li,duration);
			});
			$root.on('mouseleave','li.unlockafe-mega-menu__has-children',function(){
				if(isMobile()){
					return;
				}
				scheduleClose($(this),duration,closeDelay);
			});
		}

		$root.on('keydown','a',function(e){
			if(e.key!=='Escape'){return;}
			var $li=$(this).closest('li.is-open');
			if($li.length){
				closeItem($li,duration);
				$li.children('a').trigger('focus');
			}
		});

		initCategoryFilter($root);
	}

	function boot(){
		$('.unlockafe-mega-menu').each(function(){
			var $root=$(this);
			if($root.data('unlockafeMegaInit')){return;}
			$root.data('unlockafeMegaInit',true);
			init($root);
		});
	}

	$(window).on('elementor/frontend/init',function(){
		boot();
		if(window.elementorFrontend&&window.elementorFrontend.hooks){
			window.elementorFrontend.hooks.addAction('frontend/element_ready/unlockafe-mega-menu.default',function($scope){
				boot();
			});
		}
	});

	$(function(){boot();});
})(jQuery);
