/* Parcel Selector — Confetti animation helper.
 * Auto-extracted from PHP for WP.org compliance. */

		window.PSLE_Confetti = window.PSLE_Confetti || (function(){
			var COLORS = ['#FF6600','#FF8C00','#FFD700','#10b981','#059669','#34d399','#3b82f6','#8b5cf6','#f43f5e','#ffffff','#fbbf24','#ec4899','#06b6d4','#a855f7','#f97316'];
			var SHAPES = [{w:10,h:10,br:'2px'},{w:16,h:7,br:'1px'},{w:9,h:9,br:'50%'},{w:20,h:5,br:'2px'},{w:6,h:15,br:'1px'},{w:12,h:12,br:'3px'}];
			function spawnWave(panel, waveDelay, count, distMin, distMax, durMin, durMax, delayMax) {
				setTimeout(function(){
					var container = panel.parentElement || document.body;
					// Ensure container has position:relative for absolute positioning to work
					var cs = window.getComputedStyle(container);
					if (cs.position === 'static') container.style.position = 'relative';
					var rect = panel.getBoundingClientRect();
					var pRect = container.getBoundingClientRect();
					var cx = (rect.left - pRect.left) + rect.width/2;
					var cy = (rect.top - pRect.top) + 68;
					for (var i=0; i<count; i++) (function(){
						var el = document.createElement('div');
						el.className = 'confetti-particle';
						var sh = SHAPES[Math.floor(Math.random()*SHAPES.length)];
						var color = COLORS[Math.floor(Math.random()*COLORS.length)];
						var baseAngle = -90 + (Math.random()-0.5)*240;
						var rad = baseAngle * Math.PI/180;
						var dist = distMin + Math.random()*(distMax-distMin);
						var tx = Math.cos(rad)*dist;
						var ty = Math.sin(rad)*dist;
						var rot = (Math.random()-0.5)*1200 + 'deg';
						var dur = (durMin + Math.random()*(durMax-durMin)).toFixed(2) + 's';
						var delay = (Math.random()*delayMax).toFixed(3) + 's';
						el.style.cssText = ['left:'+cx+'px','top:'+cy+'px','width:'+sh.w+'px','height:'+sh.h+'px','background:'+color,'--tx:'+tx.toFixed(1)+'px','--ty:'+ty.toFixed(1)+'px','--rot:'+rot,'--dur:'+dur,'--delay:'+delay,'--br:'+sh.br].join(';');
						container.appendChild(el);
						var removeAfter = (parseFloat(dur) + parseFloat(delay) + 0.3) * 1000;
						setTimeout(function(){ if (el.parentNode) el.parentNode.removeChild(el); }, removeAfter);
					})();
				}, waveDelay);
			}
			// v4.1.11 REQUEST-25: shared AudioContext, unlocked on first user gesture (autoplay policy fix)
			var _PSLE_AC = null;
			function _ensureAC() {
				if (_PSLE_AC) return _PSLE_AC;
				var AC = window.AudioContext || window.webkitAudioContext;
				if (!AC) return null;
				try { _PSLE_AC = new AC(); } catch(e) { return null; }
				return _PSLE_AC;
			}
			// Unlock on first user gesture anywhere in the document (consent button, sender form, size click)
			document.addEventListener('click', function unlock() {
				var c = _ensureAC();
				if (c && c.state === 'suspended') { c.resume(); }
				document.removeEventListener('click', unlock);
			}, { once: true, capture: true });

			function playBeep() {
				try {
					var ctx = _ensureAC();
					if (!ctx) return;
					if (ctx.state === 'suspended') { ctx.resume(); }
					var tones = [523.25, 659.25, 783.99]; // C5 E5 G5 (major chord)
					tones.forEach(function(freq, i) {
						var osc = ctx.createOscillator();
						var gain = ctx.createGain();
						osc.type = 'sine';
						osc.frequency.value = freq;
						gain.gain.setValueAtTime(0, ctx.currentTime + i * 0.12);
						gain.gain.linearRampToValueAtTime(0.18, ctx.currentTime + i * 0.12 + 0.02);
						gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + i * 0.12 + 0.4);
						osc.connect(gain).connect(ctx.destination);
						osc.start(ctx.currentTime + i * 0.12);
						osc.stop(ctx.currentTime + i * 0.12 + 0.4);
					});
				} catch(e) { /* ignore audio errors */ }
			}
			function fire(panel) {
				if (!panel) panel = document.getElementById('psle-stages') || document.body;
				playBeep();
				spawnWave(panel, 0,    60, 120, 320, 2.2, 3.8, 0.35);
				spawnWave(panel, 700,  40,  80, 220, 2.8, 4.5, 0.5);
				spawnWave(panel, 1600, 25,  50, 160, 3.0, 5.0, 0.7);
			}
			function toast(msg) {
				var t = document.createElement('div');
				t.className = 'psle-toast-paid';
				t.innerHTML = '&#10003; ' + msg;
				document.body.appendChild(t);
				setTimeout(function(){ if (t.parentNode) t.parentNode.removeChild(t); }, 3800);
			}
			return { fire: fire, toast: toast };
		})();