import { FEYNMAN_TOPICS, type Topic } from "./topics"; // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- export interface PrerequisiteInfo { /** The topic ID being checked */ topicId: string; /** Whether all prerequisites are satisfied */ ready: boolean; /** Prerequisite topics that the user has demonstrated (mastered) */ met: Topic[]; /** Prerequisite topics the user has NOT demonstrated */ unmet: Topic[]; /** Full prerequisite chain (transitive), in dependency order (deepest first) */ chain: Topic[]; } // --------------------------------------------------------------------------- // Index for fast lookups // --------------------------------------------------------------------------- const topicById = new Map(); for (const t of FEYNMAN_TOPICS) { topicById.set(t.id, t); } // --------------------------------------------------------------------------- // Core functions // --------------------------------------------------------------------------- /** * Look up a topic by ID. Returns undefined if not found. */ export function getTopicById(id: string): Topic | undefined { return topicById.get(id); } /** * Get the direct prerequisite topics for a given topic. */ export function getDirectPrerequisites(topicId: string): Topic[] { const topic = topicById.get(topicId); if (!topic?.prerequisites?.length) return []; return topic.prerequisites .map((id) => topicById.get(id)) .filter((t): t is Topic => t !== undefined); } /** * Get the full transitive prerequisite chain for a topic (deepest-first). * Uses BFS to avoid infinite loops from any accidental cycles. */ export function getPrerequisiteChain(topicId: string): Topic[] { const chain: Topic[] = []; const visited = new Set(); const queue: string[] = []; // Seed with direct prereqs const topic = topicById.get(topicId); if (!topic?.prerequisites?.length) return []; for (const pid of topic.prerequisites) { if (!visited.has(pid)) { queue.push(pid); visited.add(pid); } } // BFS through the dependency graph while (queue.length > 0) { const id = queue.shift()!; const t = topicById.get(id); if (!t) continue; chain.push(t); if (t.prerequisites?.length) { for (const pid of t.prerequisites) { if (!visited.has(pid)) { queue.push(pid); visited.add(pid); } } } } // Reverse so deepest prerequisites come first (topological-ish order) return chain.reverse(); } /** * Check prerequisite status for a topic given a set of mastered topic IDs. * * A topic is "ready" when ALL its direct prerequisites have been mastered. * The chain includes transitive dependencies so the UI can show "you need A * before B, and B before this topic." */ export function checkPrerequisites( topicId: string, masteredTopicIds: Set, ): PrerequisiteInfo { const directPrereqs = getDirectPrerequisites(topicId); if (directPrereqs.length === 0) { return { topicId, ready: true, met: [], unmet: [], chain: [] }; } const met: Topic[] = []; const unmet: Topic[] = []; for (const prereq of directPrereqs) { if (masteredTopicIds.has(prereq.id)) { met.push(prereq); } else { unmet.push(prereq); } } const chain = getPrerequisiteChain(topicId); return { topicId, ready: unmet.length === 0, met, unmet, chain, }; } /** * Filter a list of topics to only those whose prerequisites are met. */ export function filterReadyTopics( topics: Topic[], masteredTopicIds: Set, ): Topic[] { return topics.filter((t) => { if (!t.prerequisites?.length) return true; return t.prerequisites.every((pid) => masteredTopicIds.has(pid)); }); } /** * Partition topics into ready (prereqs met) and locked (prereqs unmet). */ export function partitionByReadiness( topics: Topic[], masteredTopicIds: Set, ): { ready: Topic[]; locked: Topic[] } { const ready: Topic[] = []; const locked: Topic[] = []; for (const t of topics) { if (!t.prerequisites?.length || t.prerequisites.every((pid) => masteredTopicIds.has(pid))) { ready.push(t); } else { locked.push(t); } } return { ready, locked }; } /** * Get suggested next topics — prerequisite topics the user hasn't mastered yet * that would unlock the most other topics. * * Useful for "what should I study next?" recommendations. */ export function getSuggestedPrerequisites( masteredTopicIds: Set, limit: number = 3, ): Topic[] { // Count how many locked topics each unmastered topic would help unlock const unlockCount = new Map(); for (const topic of FEYNMAN_TOPICS) { if (!topic.prerequisites?.length) continue; for (const pid of topic.prerequisites) { if (!masteredTopicIds.has(pid)) { unlockCount.set(pid, (unlockCount.get(pid) ?? 0) + 1); } } } // Sort by unlock count descending, filter to known topics const sorted = [...unlockCount.entries()] .sort((a, b) => b[1] - a[1]) .slice(0, limit) .map(([id]) => topicById.get(id)) .filter((t): t is Topic => t !== undefined); return sorted; }