/**
 * Advance Mega Menu – Elementor editor:
 * 1) "Add Category" / "Add Submenu" click must not add a new top-level menu.
 * 2) "Load dummy data" button: fetch dummy menu items and set menu_items so user can edit.
 */
(function () {
	'use strict';

	function isInnerAddButton(el) {
		if (!el || !el.closest) return false;
		var btn = el.closest('.elementor-repeater-add');
		if (!btn) return false;
		var control = btn.closest('.elementor-control-categories, .elementor-control-submenu_items');
		return control && control.closest('.elementor-control-menu_items');
	}

	// Only run when click is inside Menu Items section (optimization: avoid work on every panel click)
	document.addEventListener('click', function (e) {
		if (!e.target || !e.target.closest || !e.target.closest('.elementor-control-menu_items')) return;
		if (!isInnerAddButton(e.target)) return;

		var wrapper = e.target.closest('.elementor-control-categories, .elementor-control-submenu_items');
		if (!wrapper) return;

		function stopBubble(ev) {
			ev.stopPropagation();
			wrapper.removeEventListener('click', stopBubble);
		}
		wrapper.addEventListener('click', stopBubble);
	}, true);

	function runLoadDummyData(container) {
		if (!container) return;
		// Resolve container: getContainer(id) if we have view.model.id
		var id = container.model && container.model.get && container.model.get('id');
		if (id && elementor.getContainer) {
			container = elementor.getContainer(id);
		}
		var model = container && (container.model || container);
		if (!model || model.get('widgetType') !== 'unlockafe-advance-mega-menu') return;
		jQuery.post(unlockafe_amm_editor.ajaxurl, {
			action: 'unlockafe_get_dummy_mega_menu',
			nonce: unlockafe_amm_editor.nonce
		}).done(function (r) {
			if (r.success && r.data) {
				var data = r.data;
				// Prefer Elementor document command so panel and preview both update
				if (typeof $e !== 'undefined' && $e.run && container) {
					try {
						$e.run('document/elements/settings', {
							container: container,
							settings: { menu_items: data }
						});
					} catch (err) {
						model.setSetting('menu_items', data);
					}
				} else {
					model.setSetting('menu_items', data);
				}
				if (model.trigger) model.trigger('change');
				if (container.render && typeof container.render === 'function') container.render();
				if (elementor.notifications && elementor.notifications.showToast) {
					elementor.notifications.showToast({ message: 'Dummy data loaded. You can edit all menus, submenus, categories and images below.' });
				}
			}
		}).fail(function () {
			if (elementor.notifications && elementor.notifications.showToast) {
				elementor.notifications.showToast({ message: 'Could not load dummy data.' });
			}
		});
	}

	// Load dummy data: channel event (Elementor button) + DOM click fallback so button always works
	function initLoadDummyData() {
		if (typeof unlockafe_amm_editor === 'undefined') return;
		if (typeof elementor !== 'undefined' && elementor.channels && elementor.channels.editor) {
			elementor.channels.editor.on('unlockafe:load_dummy_menu', function (controlView) {
				var container = (controlView && (controlView.container || (controlView.model && controlView.model.container))) || (elementor.getCurrentElement && elementor.getCurrentElement());
				runLoadDummyData(container || (elementor.getCurrentElement && elementor.getCurrentElement()));
			});
		}
		// DOM fallback: click on "Load dummy data into menu" button
		document.addEventListener('click', function (e) {
			var btn = e.target && e.target.closest && e.target.closest('button');
			if (!btn || !btn.textContent || btn.textContent.indexOf('Load dummy data') === -1) return;
			var controlWrap = btn.closest('.elementor-control-load_dummy_data_btn');
			if (!controlWrap || !controlWrap.closest('.elementor-control-menu_items')) return;
			var container = elementor.getCurrentElement && elementor.getCurrentElement();
			runLoadDummyData(container);
		}, true);
	}
	if (typeof elementor !== 'undefined') initLoadDummyData(); else jQuery(window).on('elementor:init', initLoadDummyData);
})();
