// Cookie Utility Functions class CookieManager { // Set a cookie static setCookie(name, value, days = 30) { const expires = new Date(); expires.setTime(expires.getTime() + (days * 24 * 60 * 60 * 1000)); document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/;SameSite=Lax`; } // Get a cookie static getCookie(name) { const nameEQ = name + "="; const ca = document.cookie.split(';'); for (let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) === ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length); } return null; } // Delete a cookie static deleteCookie(name) { document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 UTC;path=/;`; } // Check if cookies are enabled static areCookiesEnabled() { try { this.setCookie('test', 'test'); const enabled = this.getCookie('test') === 'test'; this.deleteCookie('test'); return enabled; } catch (e) { return false; } } } // Location-specific cookie functions class LocationCookieManager { static COOKIE_NAME = 'ninjacross_last_location'; static COOKIE_EXPIRY_DAYS = 90; // 3 months // Save last selected location static saveLastLocation(locationId, locationName) { if (!locationId || !locationName) return; const locationData = { id: locationId, name: locationName, timestamp: new Date().toISOString() }; try { CookieManager.setCookie( this.COOKIE_NAME, JSON.stringify(locationData), this.COOKIE_EXPIRY_DAYS ); console.log('✅ Location saved to cookie:', locationName); } catch (error) { console.error('❌ Failed to save location to cookie:', error); } } // Get last selected location static getLastLocation() { try { const cookieValue = CookieManager.getCookie(this.COOKIE_NAME); if (!cookieValue) return null; const locationData = JSON.parse(cookieValue); // Check if cookie is not too old (optional: 30 days max) const cookieDate = new Date(locationData.timestamp); const maxAge = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds if (Date.now() - cookieDate.getTime() > maxAge) { this.clearLastLocation(); return null; } return locationData; } catch (error) { console.error('❌ Failed to parse location cookie:', error); this.clearLastLocation(); return null; } } // Clear last location static clearLastLocation() { CookieManager.deleteCookie(this.COOKIE_NAME); console.log('🗑️ Location cookie cleared'); } // Check if location cookie exists static hasLastLocation() { return this.getLastLocation() !== null; } } // Export for use in other scripts window.CookieManager = CookieManager; window.LocationCookieManager = LocationCookieManager;