File size: 5,339 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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<string, Topic>();
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<string>();
  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<string>,
): 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<string>,
): 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<string>,
): { 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<string>,
  limit: number = 3,
): Topic[] {
  // Count how many locked topics each unmastered topic would help unlock
  const unlockCount = new Map<string, number>();

  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;
}