All files / web/src/app/create/worksheets skills.ts

0% Statements 0/656
0% Branches 0/1
0% Functions 0/1
0% Lines 0/656

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
// Skill definitions for mastery mode
// Each skill represents a pedagogical milestone in learning addition/subtraction

import type { DisplayRules } from './displayRules'

/**
 * Skill IDs follow naming convention:
 * - Prefix: sd (single-digit), td (two-digit), 3d/4d/5d (multi-digit)
 * - Operation: addition skills have descriptive names, subtraction uses "sub" prefix, mixed uses "mixed" prefix
 * - Complexity: no-regroup, simple-regroup, ones-regroup, mixed-regroup, full-regroup
 */
export type SkillId =
  // Single-digit addition
  | 'sd-no-regroup'
  | 'sd-simple-regroup'
  // Two-digit addition
  | 'td-no-regroup'
  | 'td-ones-regroup'
  | 'td-mixed-regroup'
  | 'td-full-regroup'
  // Three-digit addition
  | '3d-no-regroup'
  | '3d-simple-regroup'
  | '3d-full-regroup'
  // Four/five-digit addition
  | '4d-mastery'
  | '5d-mastery'
  // Single-digit subtraction
  | 'sd-sub-no-borrow'
  // Two-digit subtraction
  | 'td-sub-no-borrow'
  | 'td-sub-ones-borrow'
  | 'td-sub-mixed-borrow'
  | 'td-sub-full-borrow'
  // Three-digit subtraction
  | '3d-sub-simple'
  | '3d-sub-complex'
  // Four/five-digit subtraction
  | '4d-sub-mastery'
  | '5d-sub-mastery'

export interface SkillDefinition {
  id: SkillId
  name: string
  description: string
  operator: 'addition' | 'subtraction'

  // Problem generation constraints
  digitRange: { min: number; max: number }
  regroupingConfig: {
    pAnyStart: number
    pAllStart: number
  }

  // Pedagogical settings
  recommendedScaffolding: DisplayRules
  recommendedProblemCount: number

  // Mastery validation (for future grading)
  masteryThreshold: number // 0.0-1.0 (e.g., 0.85 = 85% accuracy)
  minimumAttempts: number // Min problems to qualify for mastery

  // Dependency graph
  prerequisites: SkillId[] // Skills that must be mastered first
  recommendedReview: SkillId[] // 1-2 skills to include in review mix
}

/**
 * All skill definitions (21 total: 11 addition, 10 subtraction)
 * Organized in pedagogical progression order
 */
export const SKILL_DEFINITIONS: SkillDefinition[] = [
  // ============================================================================
  // ADDITION SKILLS (11 total)
  // ============================================================================

  // Single-Digit Addition (2 skills)
  {
    id: 'sd-no-regroup',
    name: 'Single-digit without regrouping',
    description: 'Simple single-digit addition like 3+5, 2+4',
    operator: 'addition',
    digitRange: { min: 1, max: 1 },
    regroupingConfig: { pAnyStart: 0, pAllStart: 0 },
    recommendedScaffolding: {
      carryBoxes: 'always',
      answerBoxes: 'always',
      placeValueColors: 'always',
      tenFrames: 'never', // Not needed for simple problems
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'never',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 20,
    masteryThreshold: 0.9,
    minimumAttempts: 20,
    prerequisites: [],
    recommendedReview: [],
  },

  {
    id: 'sd-simple-regroup',
    name: 'Single-digit with regrouping',
    description: 'Single-digit addition with carrying like 7+8, 9+6',
    operator: 'addition',
    digitRange: { min: 1, max: 1 },
    regroupingConfig: { pAnyStart: 1.0, pAllStart: 0 },
    recommendedScaffolding: {
      carryBoxes: 'whenRegrouping',
      answerBoxes: 'always',
      placeValueColors: 'always',
      tenFrames: 'whenRegrouping', // Help visualize making ten
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'never',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 20,
    masteryThreshold: 0.9,
    minimumAttempts: 20,
    prerequisites: ['sd-no-regroup'],
    recommendedReview: ['sd-no-regroup'],
  },

  // Two-Digit Addition (4 skills)
  {
    id: 'td-no-regroup',
    name: 'Two-digit without regrouping',
    description: 'Two-digit addition without carrying like 23+45, 31+28',
    operator: 'addition',
    digitRange: { min: 2, max: 2 },
    regroupingConfig: { pAnyStart: 0, pAllStart: 0 },
    recommendedScaffolding: {
      carryBoxes: 'always', // Show structure even when not needed
      answerBoxes: 'always',
      placeValueColors: 'always',
      tenFrames: 'never',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'never',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 15,
    masteryThreshold: 0.85,
    minimumAttempts: 15,
    prerequisites: ['sd-simple-regroup'],
    recommendedReview: ['sd-simple-regroup'],
  },

  {
    id: 'td-ones-regroup',
    name: 'Two-digit with ones place regrouping',
    description: 'Two-digit addition with carrying in ones place like 38+27, 49+15',
    operator: 'addition',
    digitRange: { min: 2, max: 2 },
    regroupingConfig: { pAnyStart: 0.5, pAllStart: 0 },
    recommendedScaffolding: {
      carryBoxes: 'whenRegrouping',
      answerBoxes: 'always',
      placeValueColors: 'always',
      tenFrames: 'whenRegrouping',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'never',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 15,
    masteryThreshold: 0.85,
    minimumAttempts: 15,
    prerequisites: ['td-no-regroup'],
    recommendedReview: ['td-no-regroup'],
  },

  {
    id: 'td-mixed-regroup',
    name: 'Two-digit with mixed regrouping',
    description: 'Two-digit addition with varied regrouping like 67+58, 84+73',
    operator: 'addition',
    digitRange: { min: 2, max: 2 },
    regroupingConfig: { pAnyStart: 0.7, pAllStart: 0.2 },
    recommendedScaffolding: {
      carryBoxes: 'whenRegrouping',
      answerBoxes: 'whenMultipleRegroups',
      placeValueColors: 'whenRegrouping',
      tenFrames: 'whenRegrouping',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'never',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 15,
    masteryThreshold: 0.85,
    minimumAttempts: 15,
    prerequisites: ['td-ones-regroup'],
    recommendedReview: ['td-no-regroup', 'td-ones-regroup'],
  },

  {
    id: 'td-full-regroup',
    name: 'Two-digit with frequent regrouping',
    description: 'Two-digit addition with high regrouping frequency like 88+99, 76+67',
    operator: 'addition',
    digitRange: { min: 2, max: 2 },
    regroupingConfig: { pAnyStart: 0.9, pAllStart: 0.5 },
    recommendedScaffolding: {
      carryBoxes: 'whenMultipleRegroups',
      answerBoxes: 'whenMultipleRegroups',
      placeValueColors: 'whenMultipleRegroups',
      tenFrames: 'whenRegrouping',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'never',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 15,
    masteryThreshold: 0.8,
    minimumAttempts: 15,
    prerequisites: ['td-mixed-regroup'],
    recommendedReview: ['td-ones-regroup', 'td-mixed-regroup'],
  },

  // Three-Digit Addition (3 skills)
  {
    id: '3d-no-regroup',
    name: 'Three-digit without regrouping',
    description: 'Three-digit addition without carrying like 234+451, 123+456',
    operator: 'addition',
    digitRange: { min: 3, max: 3 },
    regroupingConfig: { pAnyStart: 0, pAllStart: 0 },
    recommendedScaffolding: {
      carryBoxes: 'never',
      answerBoxes: 'always',
      placeValueColors: 'always',
      tenFrames: 'never',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'never',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 12,
    masteryThreshold: 0.85,
    minimumAttempts: 12,
    prerequisites: ['td-full-regroup'],
    recommendedReview: ['td-mixed-regroup', 'td-full-regroup'],
  },

  {
    id: '3d-simple-regroup',
    name: 'Three-digit with occasional regrouping',
    description: 'Three-digit addition with some carrying like 367+258, 484+273',
    operator: 'addition',
    digitRange: { min: 3, max: 3 },
    regroupingConfig: { pAnyStart: 0.5, pAllStart: 0.1 },
    recommendedScaffolding: {
      carryBoxes: 'whenRegrouping',
      answerBoxes: 'whenMultipleRegroups',
      placeValueColors: 'whenRegrouping',
      tenFrames: 'never',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'never',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 12,
    masteryThreshold: 0.8,
    minimumAttempts: 12,
    prerequisites: ['3d-no-regroup'],
    recommendedReview: ['td-full-regroup', '3d-no-regroup'],
  },

  {
    id: '3d-full-regroup',
    name: 'Three-digit with frequent regrouping',
    description: 'Three-digit addition with high regrouping like 888+999, 767+676',
    operator: 'addition',
    digitRange: { min: 3, max: 3 },
    regroupingConfig: { pAnyStart: 0.9, pAllStart: 0.6 },
    recommendedScaffolding: {
      carryBoxes: 'whenMultipleRegroups',
      answerBoxes: 'never',
      placeValueColors: 'when3PlusDigits',
      tenFrames: 'never',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'never',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 12,
    masteryThreshold: 0.8,
    minimumAttempts: 12,
    prerequisites: ['3d-simple-regroup'],
    recommendedReview: ['3d-no-regroup', '3d-simple-regroup'],
  },

  // Four/Five-Digit Addition (2 skills)
  {
    id: '4d-mastery',
    name: 'Four-digit mastery',
    description: 'Four-digit addition with varied regrouping like 3847+2956',
    operator: 'addition',
    digitRange: { min: 4, max: 4 },
    regroupingConfig: { pAnyStart: 0.8, pAllStart: 0.4 },
    recommendedScaffolding: {
      carryBoxes: 'never',
      answerBoxes: 'never',
      placeValueColors: 'when3PlusDigits',
      tenFrames: 'never',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'never',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 10,
    masteryThreshold: 0.8,
    minimumAttempts: 10,
    prerequisites: ['3d-full-regroup'],
    recommendedReview: ['3d-simple-regroup', '3d-full-regroup'],
  },

  {
    id: '5d-mastery',
    name: 'Five-digit mastery',
    description: 'Five-digit addition with varied regrouping like 38472+29563',
    operator: 'addition',
    digitRange: { min: 5, max: 5 },
    regroupingConfig: { pAnyStart: 0.85, pAllStart: 0.5 },
    recommendedScaffolding: {
      carryBoxes: 'never',
      answerBoxes: 'never',
      placeValueColors: 'never',
      tenFrames: 'never',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'never',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 10,
    masteryThreshold: 0.75,
    minimumAttempts: 10,
    prerequisites: ['4d-mastery'],
    recommendedReview: ['3d-full-regroup', '4d-mastery'],
  },

  // ============================================================================
  // SUBTRACTION SKILLS (10 total)
  // ============================================================================

  // Single-Digit Subtraction (2 skills)
  {
    id: 'sd-sub-no-borrow',
    name: 'Single-digit without borrowing',
    description: 'Simple subtraction like 8-3, 9-4',
    operator: 'subtraction',
    digitRange: { min: 1, max: 1 },
    regroupingConfig: { pAnyStart: 0, pAllStart: 0 },
    recommendedScaffolding: {
      carryBoxes: 'never',
      answerBoxes: 'always',
      placeValueColors: 'always',
      tenFrames: 'never',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'always',
      borrowingHints: 'always',
    },
    recommendedProblemCount: 20,
    masteryThreshold: 0.9,
    minimumAttempts: 20,
    prerequisites: [],
    recommendedReview: [],
  },

  // Two-Digit Subtraction (4 skills)
  {
    id: 'td-sub-no-borrow',
    name: 'Two-digit without borrowing',
    description: 'Two-digit subtraction without borrowing like 68-43',
    operator: 'subtraction',
    digitRange: { min: 2, max: 2 },
    regroupingConfig: { pAnyStart: 0, pAllStart: 0 },
    recommendedScaffolding: {
      carryBoxes: 'never',
      answerBoxes: 'always',
      placeValueColors: 'always',
      tenFrames: 'never',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'always',
      borrowingHints: 'always',
    },
    recommendedProblemCount: 15,
    masteryThreshold: 0.85,
    minimumAttempts: 15,
    prerequisites: ['sd-sub-no-borrow'],
    recommendedReview: ['sd-sub-no-borrow'],
  },

  {
    id: 'td-sub-ones-borrow',
    name: 'Two-digit with ones place borrowing',
    description: 'Two-digit subtraction with borrowing in ones like 52-27',
    operator: 'subtraction',
    digitRange: { min: 2, max: 2 },
    regroupingConfig: { pAnyStart: 0.5, pAllStart: 0 },
    recommendedScaffolding: {
      carryBoxes: 'never',
      answerBoxes: 'always',
      placeValueColors: 'always',
      tenFrames: 'whenRegrouping',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'whenRegrouping',
      borrowingHints: 'whenRegrouping',
    },
    recommendedProblemCount: 15,
    masteryThreshold: 0.85,
    minimumAttempts: 15,
    prerequisites: ['td-sub-no-borrow'],
    recommendedReview: ['td-sub-no-borrow'],
  },

  {
    id: 'td-sub-mixed-borrow',
    name: 'Two-digit with mixed borrowing',
    description: 'Two-digit subtraction with varied borrowing like 73-48',
    operator: 'subtraction',
    digitRange: { min: 2, max: 2 },
    regroupingConfig: { pAnyStart: 0.7, pAllStart: 0.2 },
    recommendedScaffolding: {
      carryBoxes: 'never',
      answerBoxes: 'whenMultipleRegroups',
      placeValueColors: 'whenRegrouping',
      tenFrames: 'whenRegrouping',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'whenRegrouping',
      borrowingHints: 'whenMultipleRegroups',
    },
    recommendedProblemCount: 15,
    masteryThreshold: 0.85,
    minimumAttempts: 15,
    prerequisites: ['td-sub-ones-borrow'],
    recommendedReview: ['td-sub-no-borrow', 'td-sub-ones-borrow'],
  },

  {
    id: 'td-sub-full-borrow',
    name: 'Two-digit with frequent borrowing',
    description: 'Two-digit subtraction with high borrowing frequency like 91-78',
    operator: 'subtraction',
    digitRange: { min: 2, max: 2 },
    regroupingConfig: { pAnyStart: 0.9, pAllStart: 0.5 },
    recommendedScaffolding: {
      carryBoxes: 'never',
      answerBoxes: 'whenMultipleRegroups',
      placeValueColors: 'whenMultipleRegroups',
      tenFrames: 'whenRegrouping',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'whenRegrouping',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 15,
    masteryThreshold: 0.8,
    minimumAttempts: 15,
    prerequisites: ['td-sub-mixed-borrow'],
    recommendedReview: ['td-sub-ones-borrow', 'td-sub-mixed-borrow'],
  },

  // Three-Digit Subtraction (2 skills - simplified from addition)
  {
    id: '3d-sub-simple',
    name: 'Three-digit with occasional borrowing',
    description: 'Three-digit subtraction with some borrowing like 567-238',
    operator: 'subtraction',
    digitRange: { min: 3, max: 3 },
    regroupingConfig: { pAnyStart: 0.5, pAllStart: 0.1 },
    recommendedScaffolding: {
      carryBoxes: 'never',
      answerBoxes: 'whenMultipleRegroups',
      placeValueColors: 'whenRegrouping',
      tenFrames: 'never',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'whenRegrouping',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 12,
    masteryThreshold: 0.8,
    minimumAttempts: 12,
    prerequisites: ['td-sub-full-borrow'],
    recommendedReview: ['td-sub-mixed-borrow', 'td-sub-full-borrow'],
  },

  {
    id: '3d-sub-complex',
    name: 'Three-digit with frequent borrowing',
    description: 'Three-digit subtraction with high borrowing like 801-567',
    operator: 'subtraction',
    digitRange: { min: 3, max: 3 },
    regroupingConfig: { pAnyStart: 0.9, pAllStart: 0.6 },
    recommendedScaffolding: {
      carryBoxes: 'never',
      answerBoxes: 'never',
      placeValueColors: 'when3PlusDigits',
      tenFrames: 'never',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'whenRegrouping',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 12,
    masteryThreshold: 0.8,
    minimumAttempts: 12,
    prerequisites: ['3d-sub-simple'],
    recommendedReview: ['td-sub-full-borrow', '3d-sub-simple'],
  },

  // Four/Five-Digit Subtraction (2 skills)
  {
    id: '4d-sub-mastery',
    name: 'Four-digit subtraction mastery',
    description: 'Four-digit subtraction with varied borrowing like 5847-2956',
    operator: 'subtraction',
    digitRange: { min: 4, max: 4 },
    regroupingConfig: { pAnyStart: 0.8, pAllStart: 0.4 },
    recommendedScaffolding: {
      carryBoxes: 'never',
      answerBoxes: 'never',
      placeValueColors: 'when3PlusDigits',
      tenFrames: 'never',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'whenRegrouping',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 10,
    masteryThreshold: 0.8,
    minimumAttempts: 10,
    prerequisites: ['3d-sub-complex'],
    recommendedReview: ['3d-sub-simple', '3d-sub-complex'],
  },

  {
    id: '5d-sub-mastery',
    name: 'Five-digit subtraction mastery',
    description: 'Five-digit subtraction with varied borrowing like 58472-29563',
    operator: 'subtraction',
    digitRange: { min: 5, max: 5 },
    regroupingConfig: { pAnyStart: 0.85, pAllStart: 0.5 },
    recommendedScaffolding: {
      carryBoxes: 'never',
      answerBoxes: 'never',
      placeValueColors: 'never',
      tenFrames: 'never',
      problemNumbers: 'always',
      cellBorders: 'always',
      borrowNotation: 'never',
      borrowingHints: 'never',
    },
    recommendedProblemCount: 10,
    masteryThreshold: 0.75,
    minimumAttempts: 10,
    prerequisites: ['4d-sub-mastery'],
    recommendedReview: ['3d-sub-complex', '4d-sub-mastery'],
  },
]

/**
 * Helper: Get skill definition by ID
 */
export function getSkillById(id: SkillId): SkillDefinition | undefined {
  return SKILL_DEFINITIONS.find((skill) => skill.id === id)
}

/**
 * Helper: Get all skills for a given operator
 */
export function getSkillsByOperator(operator: 'addition' | 'subtraction'): SkillDefinition[] {
  return SKILL_DEFINITIONS.filter((skill) => skill.operator === operator)
}

/**
 * Helper: Find next skill in progression (null if at end or prerequisites not met)
 */
export function findNextSkill(
  currentSkillId: SkillId,
  masteryStates: Map<SkillId, boolean>,
  operator: 'addition' | 'subtraction'
): SkillDefinition | null {
  const skills = getSkillsByOperator(operator)
  const currentIndex = skills.findIndex((s) => s.id === currentSkillId)

  if (currentIndex === -1 || currentIndex === skills.length - 1) {
    return null // Not found or at end
  }

  const nextSkill = skills[currentIndex + 1]

  // Check if prerequisites are met
  const prereqsMet = nextSkill.prerequisites.every(
    (prereqId) => masteryStates.get(prereqId) === true
  )

  return prereqsMet ? nextSkill : null
}

/**
 * Helper: Find previous skill in progression
 */
export function findPreviousSkill(
  currentSkillId: SkillId,
  operator: 'addition' | 'subtraction'
): SkillDefinition | null {
  const skills = getSkillsByOperator(operator)
  const currentIndex = skills.findIndex((s) => s.id === currentSkillId)

  if (currentIndex <= 0) {
    return null // Not found or at start
  }

  return skills[currentIndex - 1]
}

/**
 * Helper: Get the first unmastered skill with prerequisites met
 */
export function findCurrentSkill(
  masteryStates: Map<SkillId, boolean>,
  operator: 'addition' | 'subtraction'
): SkillDefinition {
  const skills = getSkillsByOperator(operator)

  for (const skill of skills) {
    // Check if already mastered
    if (masteryStates.get(skill.id) === true) continue

    // Check if prerequisites are met
    const prereqsMet = skill.prerequisites.every((prereqId) => masteryStates.get(prereqId) === true)

    if (prereqsMet) {
      return skill // First non-mastered skill with prerequisites met
    }
  }

  // All skills mastered! Return the last skill
  return skills[skills.length - 1]
}

/**
 * Helper: Extract digit complexity from a skill (1-5)
 */
export function getDigitComplexity(skill: SkillDefinition): number {
  return skill.digitRange.max
}