(function($){"use strict";
	var STORAGE_KEY = 'unlockafe_cookie_consent_v1';
	function getState(){ try{ return JSON.parse(localStorage.getItem(STORAGE_KEY)||'null'); }catch(e){ return null; } }
	function setState(state){ try{ localStorage.setItem(STORAGE_KEY, JSON.stringify(state)); }catch(e){} }
	function applyBrandColor($root, color){ if(color){ $root[0].style.setProperty('--unlockafe-cookie-brand', color); } }
	function readPreferences($widget){ var prefs = {}; $widget.find('.cc-row[data-key]').each(function(){ var $row=$(this); var key=$row.data('key'); var required=String($row.data('required'))==='1'; if(required){ prefs[key]=true; } else { prefs[key]=!!$row.find('.cc-toggle').is(':checked'); } }); return prefs; }
	function applySavedPreferences($widget, prefs){ if(!prefs) return; $widget.find('.cc-row[data-key]').each(function(){ var $row=$(this); var key=$row.data('key'); if($row.data('required')==='1'){return;} if(prefs.hasOwnProperty(key)){ $row.find('.cc-toggle').prop('checked', !!prefs[key]); } }); }
	function toggleTrigger($w, show){ var s=$w.data('settings')||{}; var $t=$w.find('.cc-trigger'); if(s.triggerEnabled===false){ $t.hide(); return; } show ? $t.show() : $t.hide(); }
	function openCustomize($w){ var s=$w.data('settings')||{}; $w.find('.cc-customize').addClass('show'); var $ov=$w.find('.cc-overlay'); $ov.toggleClass('no-blur', s.overlayBlur===false); $ov.show(); trapFocus($w); }
	function closeCustomize($w){ $w.find('.cc-customize').removeClass('show'); $w.find('.cc-overlay').hide(); releaseFocus($w); }
	function trapFocus($w){ var $d=$w.find('.cc-customize-inner'); var focusables=$d.find('button,[href],input,select,textarea,[tabindex]:not([tabindex="-1"])').filter(':visible'); if(!focusables.length){return;} var first=focusables[0], last=focusables[focusables.length-1]; $d.on('keydown.ccTrap', function(e){ if(e.key==='Tab'){ if(e.shiftKey && document.activeElement===first){ e.preventDefault(); last.focus(); } else if(!e.shiftKey && document.activeElement===last){ e.preventDefault(); first.focus(); } } if(e.key==='Escape'){ e.preventDefault(); closeCustomize($w); } }); setTimeout(function(){ $(first).trigger('focus'); }, 0); }
	function releaseFocus($w){ $w.find('.cc-customize-inner').off('keydown.ccTrap'); }
	function initWidget($widget){
		var settings = $widget.data('settings')||{};
		applyBrandColor($widget, settings.brandColor);
		$widget.addClass('position-'+(settings.position||'bottom-right'));
		
		// Check if we're in Elementor editor mode
		var isEditorMode = typeof elementorFrontend !== 'undefined' && elementorFrontend.isEditMode;
		
		// In editor mode, always show the widget
		if (isEditorMode) {
			$widget.addClass('show');
			toggleTrigger($widget, false);
		} else {
			var state = getState();
			if(state && (state.status==='accepted' || state.status==='rejected' || state.status==='customized')){ 
				applySavedPreferences($widget, state.preferences||{}); 
				toggleTrigger($widget, true); 
				return; 
			}
			$widget.addClass('show'); 
			toggleTrigger($widget, false);
		}

		$widget.on('click', '.cc-btn-accept', function(e){ if($(this).hasClass('cc-btn-save')){ var prefs=readPreferences($widget); setState({status:'customized', preferences:prefs, ts: Date.now()}); $widget.removeClass('show'); closeCustomize($widget); toggleTrigger($widget, true); return; } setState({status:'accepted', preferences:{}, ts: Date.now()}); $widget.removeClass('show'); closeCustomize($widget); toggleTrigger($widget, true); });
		$widget.on('click', '.cc-btn-reject', function(){ var prefs=readPreferences($widget); Object.keys(prefs).forEach(function(k){ if($widget.find('.cc-row[data-key="'+k+'"]').data('required')==='1'){ prefs[k]=true; } else { prefs[k]=false; }}); setState({status:'rejected', preferences:prefs, ts: Date.now()}); $widget.removeClass('show'); closeCustomize($widget); toggleTrigger($widget, true); });
		$widget.on('click', '.cc-btn-customize', function(){ openCustomize($widget); });
		$widget.on('click', '.cc-close', function(){ closeCustomize($widget); });
		$widget.on('click', '.cc-overlay', function(){ closeCustomize($widget); });
		$widget.on('click', '.cc-btn-accept-all', function(){ $widget.find('.cc-btn-accept').not('.cc-btn-save').trigger('click'); });
		$widget.on('click', '.cc-btn-reject-all', function(){ $widget.find('.cc-btn-reject').first().trigger('click'); });
		$widget.on('click', '.cc-trigger', function(){ openCustomize($widget); });
		$widget.on('click', '.cc-customize-body .cc-row > div:first-child', function(e){ var $row=$(this).closest('.cc-row'); $row.toggleClass('open'); });
	}
	$(window).on('elementor/frontend/init', function(){ 
		elementorFrontend.hooks.addAction('frontend/element_ready/global', function($scope){ 
			$scope.find('.unlockafe-cookie-consent').each(function(){ 
				initWidget($(this)); 
			}); 
		});
		
		// Handle editor preview mode
		if (elementorFrontend.isEditMode) {
			// Initialize cookie consent in editor preview
			elementorFrontend.hooks.addAction('frontend/element_ready/global', function($scope) {
				if ($scope.find('.unlockafe-cookie-consent').length) {
					setTimeout(function() {
						$scope.find('.unlockafe-cookie-consent').each(function() {
							initWidget($(this));
						});
					}, 100);
				}
			});
			
			// Also initialize on editor preview refresh
			$(window).on('elementor:preview:loaded', function() {
				setTimeout(function() {
					$('.unlockafe-cookie-consent').each(function() {
						initWidget($(this));
					});
				}, 200);
			});
		}
	});
	
	$(function(){ 
		$('.unlockafe-cookie-consent').each(function(){ 
			initWidget($(this)); 
		});
		
		// Also initialize in editor mode after a delay
		if (typeof elementorFrontend !== 'undefined' && elementorFrontend.isEditMode) {
			setTimeout(function() {
				$('.unlockafe-cookie-consent').each(function() {
					var $widget = $(this);
					initWidget($widget);
					// Force show in editor mode
					$widget.addClass('show');
					console.log('Cookie Consent: Initialized in editor mode');
				});
			}, 300);
		}
	});
})(jQuery); 