(function () { 'use strict'; if (!window.EcomAPI) return; let pendingEmail = ''; function esc(v) { return String(v ?? '').replace(/&/g, '&').replace(//g, '>'); } function ensurePanel() { let panel = document.getElementById('otpVerificationPanel'); if (panel) return panel; panel = document.createElement('div'); panel.id = 'otpVerificationPanel'; panel.className = 'otp-panel'; panel.style.cssText = 'display:none;position:fixed;inset:0;z-index:9999;background:rgba(10,20,15,.55);padding:24px;align-items:center;justify-content:center;'; panel.innerHTML = `

Verify your email

`; document.body.appendChild(panel); panel.addEventListener('click', (e) => { if (e.target === panel) hideOtpPanel(); }); document.getElementById('otpVerifyBtn')?.addEventListener('click', () => verifyPendingOtp()); document.getElementById('otpResendBtn')?.addEventListener('click', () => resendPendingOtp()); document.getElementById('otpCloseBtn')?.addEventListener('click', hideOtpPanel); document.getElementById('otpCodeInput')?.addEventListener('keydown', (e) => { if (e.key === 'Enter') verifyPendingOtp(); }); return panel; } function setOtpMessage(msg) { const el = document.getElementById('otpPanelMessage'); if (el) el.textContent = msg || ''; } function showOtpPanel(email, message) { pendingEmail = String(email || '').trim(); const panel = ensurePanel(); panel.style.display = 'flex'; setOtpMessage(message || ('Enter the 6-digit code sent to ' + pendingEmail)); const input = document.getElementById('otpCodeInput'); if (input) { input.value = ''; setTimeout(() => input.focus(), 50); } } function hideOtpPanel() { const panel = document.getElementById('otpVerificationPanel'); if (panel) panel.style.display = 'none'; } async function verifyPendingOtp() { const otp = String(document.getElementById('otpCodeInput')?.value || '').trim(); if (!pendingEmail) return setOtpMessage('Missing email address.'); if (otp.length < 4) return setOtpMessage('Enter the verification code from your email.'); const data = await EcomAPI.verifyOtp(pendingEmail, otp); if (data.success) { hideOtpPanel(); window.dispatchEvent(new CustomEvent('ecom:auth-verified', { detail: data })); if (typeof window.loadAccountProfile === 'function') await window.loadAccountProfile(); if (typeof window.loadAccount === 'function') await window.loadAccount(); } else { setOtpMessage(data.message || 'Invalid code. Try again or resend.'); } return data; } async function resendPendingOtp() { if (!pendingEmail) return setOtpMessage('Missing email address.'); const data = await EcomAPI.resendOtp(pendingEmail); setOtpMessage(data.message || 'A new code has been sent.'); return data; } window.EcomOtpAuth = { show: showOtpPanel, hide: hideOtpPanel, verify: verifyPendingOtp, resend: resendPendingOtp, pendingEmail: () => pendingEmail, handleRegisterResponse(data, email) { if (data.success && data.requiresVerification) { showOtpPanel(email || data.email, data.message); return true; } return false; }, handleLoginResponse(data, email) { if (data.requiresVerification) { showOtpPanel(email || data.email, data.message || 'Verify your email with the OTP sent to your inbox.'); return true; } return false; }, }; })();