import { getUserFromToken } from '../../utils/auth.js' function normalizeConfig(config) { if (!config || typeof config !== 'object') return undefined const normalized = { season: config.season ? String(config.season) : undefined, teamName: config.teamName ? String(config.teamName) : undefined, teamAgeGroup: config.teamAgeGroup ? String(config.teamAgeGroup) : undefined } if (!normalized.season && !normalized.teamName && !normalized.teamAgeGroup) { return undefined } return normalized } function parseSections(value) { if (!value || typeof value !== 'string') return [] try { const parsed = JSON.parse(value) if (!Array.isArray(parsed)) return [] return parsed .filter(section => section?.id) .map((section, index) => ({ key: section.key ? String(section.key) : `${String(section.id)}-${index}`, id: String(section.id), enabled: section.enabled !== false, config: normalizeConfig(section.config) })) } catch { return [] } } export default defineEventHandler(async (event) => { const token = getCookie(event, 'auth_token') || getHeader(event, 'authorization')?.replace(/^Bearer\s+/i, '') const user = token ? await getUserFromToken(token) : null const rawCookieSections = getCookie(event, 'homepage_sections') const cookieSections = parseSections(rawCookieSections) const userSections = Array.isArray(user?.homepageSettings?.sections) ? user.homepageSettings.sections .filter(section => section?.id) .map((section, index) => ({ key: section.key ? String(section.key) : `${String(section.id)}-${index}`, id: String(section.id), enabled: section.enabled !== false, config: normalizeConfig(section.config) })) : [] const isLoggedIn = !!user return { isLoggedIn, storage: isLoggedIn ? 'user' : 'cookie', sections: isLoggedIn ? userSections : cookieSections } })