import type { Lesson, TopicsConfig, Topic, Section } from '../types/lesson'; const modules = import.meta.glob('../../../content/**/*.json', { eager: true }); function getModule(path: string): T | null { const entry = modules[path]; if (!entry) return null; // Vite eager imports have a `default` property for JSON files const mod = entry as { default?: T } & T; return mod.default ?? (entry as T); } export function getSections(): Section[] { const config = getModule('../../../content/topics.json'); return config?.sections ?? []; } export function getTopics(): Topic[] { const config = getModule('../../../content/topics.json'); return config?.topics ?? []; } export function getLesson(topicId: string, lessonId: string): Lesson | null { // Try common path patterns const paths = [ `../../../content/${topicId}/${lessonId}.json`, `../../../content/lessons/${topicId}/${lessonId}.json`, `../../../content/${topicId}/lessons/${lessonId}.json`, ]; for (const path of paths) { const lesson = getModule(path); if (lesson) return lesson; } // Fallback: search through all modules for matching lesson for (const [path, mod] of Object.entries(modules)) { if (path.endsWith('.json') && !path.endsWith('topics.json')) { const data = (mod as { default?: Lesson }).default ?? (mod as Lesson); if (data.id === lessonId && data.topicId === topicId) { return data; } } } return null; } export function getLessonsForTopic(topicId: string): Lesson[] { const lessons: Lesson[] = []; for (const [path, mod] of Object.entries(modules)) { if (path.endsWith('.json') && !path.endsWith('topics.json')) { const data = (mod as { default?: Lesson }).default ?? (mod as Lesson); if (data.topicId === topicId) { lessons.push(data); } } } return lessons; }