/**
 * Advance Mega Sidebar – open/close panel, expand/collapse menu items.
 */
(function ($) {
	'use strict';

	function initAdvanceMegaSidebar() {
		var $widgets = $('.unlockafe-ams');
		if (!$widgets.length) return;

		$widgets.each(function () {
			var root = this;
			if (root._unlockafeAmsInit) return;
			root._unlockafeAmsInit = true;

			var trigger = root.querySelector('[data-ams-trigger]');
			var panel = root.querySelector('.unlockafe-ams-panel');
			var closers = root.querySelectorAll('[data-ams-close]');
			var backdrop = root.querySelector('.unlockafe-ams-backdrop');

			function open() {
				root.classList.add('is-open');
				document.body.classList.add('unlockafe-ams-body-open');
				if (panel) panel.setAttribute('aria-hidden', 'false');
				if (trigger) trigger.setAttribute('aria-expanded', 'true');
			}

			function close() {
				root.classList.remove('is-open');
				document.body.classList.remove('unlockafe-ams-body-open');
				if (panel) panel.setAttribute('aria-hidden', 'true');
				if (trigger) trigger.setAttribute('aria-expanded', 'false');
				// Collapse all submenus
				root.querySelectorAll('.unlockafe-ams-nav__item.is-expanded').forEach(function (item) {
					item.classList.remove('is-expanded');
					var sub = item.querySelector('.unlockafe-ams-sub');
					var btn = item.querySelector('[data-ams-expand]');
					if (sub) sub.setAttribute('hidden', '');
					if (btn) btn.setAttribute('aria-expanded', 'false');
				});
			}

			if (trigger) {
				trigger.addEventListener('click', function (e) {
					e.preventDefault();
					if (root.classList.contains('is-open')) close();
					else open();
				});
			}

			closers.forEach(function (el) {
				el.addEventListener('click', function (e) {
					e.preventDefault();
					close();
				});
			});

			if (backdrop) {
				backdrop.addEventListener('click', close);
			}

			// Expand/collapse submenu
			root.querySelectorAll('[data-ams-expand]').forEach(function (btn) {
				btn.addEventListener('click', function (e) {
					e.preventDefault();
					var id = btn.getAttribute('data-ams-expand');
					var sub = id ? root.querySelector('#' + id) : null;
					var item = btn.closest('.unlockafe-ams-nav__item');
					if (!sub || !item) return;
					var isExpanded = item.classList.contains('is-expanded');
					if (isExpanded) {
						item.classList.remove('is-expanded');
						sub.setAttribute('hidden', '');
						btn.setAttribute('aria-expanded', 'false');
					} else {
						item.classList.add('is-expanded');
						sub.removeAttribute('hidden');
						btn.setAttribute('aria-expanded', 'true');
					}
				});
			});

			// Category tabs inside mega submenu (filter cards by category)
			root.querySelectorAll('.unlockafe-ams-sub-tabs [data-ams-cat]').forEach(function (tabBtn) {
				tabBtn.addEventListener('click', function (e) {
					e.preventDefault();
					var sub = tabBtn.closest('.unlockafe-ams-sub');
					if (!sub) return;
					var cat = tabBtn.getAttribute('data-ams-cat') || '';

					// Active state
					sub.querySelectorAll('.unlockafe-ams-sub-tab').forEach(function (b) {
						if (b === tabBtn) {
							b.classList.add('is-active');
						} else {
							b.classList.remove('is-active');
						}
					});

					// Filter cards
					sub.querySelectorAll('.unlockafe-ams-card').forEach(function (card) {
						var c = card.getAttribute('data-category') || '';
						if (!cat || cat === 'all') {
							card.style.display = '';
						} else {
							// Support multiple category ids separated by comma
							var list = c.split(',').map(function (s) { return s.trim(); }).filter(Boolean);
							if (list.indexOf(cat) !== -1) {
								card.style.display = '';
							} else {
								card.style.display = 'none';
							}
						}
					});
				});
			});
		});
	}

	// Escape to close (global, one handler)
	function onKeydown(e) {
		if (e.key !== 'Escape') return;
		var openRoot = document.querySelector('.unlockafe-ams.is-open');
		if (openRoot) {
			openRoot.classList.remove('is-open');
			document.body.classList.remove('unlockafe-ams-body-open');
			var panel = openRoot.querySelector('.unlockafe-ams-panel');
			var trigger = openRoot.querySelector('[data-ams-trigger]');
			if (panel) panel.setAttribute('aria-hidden', 'true');
			if (trigger) trigger.setAttribute('aria-expanded', 'false');
			openRoot.querySelectorAll('.unlockafe-ams-nav__item.is-expanded').forEach(function (item) {
				item.classList.remove('is-expanded');
				var sub = item.querySelector('.unlockafe-ams-sub');
				var btn = item.querySelector('[data-ams-expand]');
				if (sub) sub.setAttribute('hidden', '');
				if (btn) btn.setAttribute('aria-expanded', 'false');
			});
		}
	}
	document.addEventListener('keydown', onKeydown);

	if (typeof elementorFrontend !== 'undefined') {
		$(window).on('elementor/frontend/init', function () {
			elementorFrontend.hooks.addAction('frontend/element_ready/unlockafe-advance-mega-sidebar.default', function () {
				initAdvanceMegaSidebar();
			});
		});
	}

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