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 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 41x 41x 41x 41x 41x 41x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { z } from 'zod'
import { getProfileFromConfig } from './difficultyProfiles'
import { WORKSHEET_LIMITS } from './constants/validation'
/**
* Versioned worksheet config schemas with type-safe validation and migration
*
* ADDING NEW VERSIONS:
* 1. Create new schema (e.g., additionConfigV2Schema)
* 2. Add migration function (e.g., migrateAdditionV1toV2)
* 3. Update CURRENT_VERSION constant
* 4. Add case to migrateAdditionConfig()
*
* ADDING NEW WORKSHEET TYPES:
* 1. Create schema with version field
* 2. Create migration function
* 3. Export parseXXXConfig() helper
*/
// =============================================================================
// SHARED DISPLAY RULE TYPES
// =============================================================================
/**
* Display rule values for conditional scaffolding
*
* - "auto": Defer to mastery progression's skill recommendation (mastery mode only)
* - "always": Always show this scaffolding element
* - "never": Never show this scaffolding element
* - "whenRegrouping": Show only when problem requires regrouping
* - "whenMultipleRegroups": Show only when problem has multiple regroups
* - "when3PlusDigits": Show only when problem has 3+ digits
*/
const displayRuleValues = [
'auto',
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
] as const
// =============================================================================
// ADDITION WORKSHEETS
// =============================================================================
/** Current schema version for addition worksheets */
const ADDITION_CURRENT_VERSION = 4
/**
* Addition worksheet config - Version 1
* Initial schema with ten-frames support
*/
export const additionConfigV1Schema = z.object({
version: z.literal(1),
problemsPerPage: z.number().int().min(1).max(WORKSHEET_LIMITS.MAX_PROBLEMS_PER_PAGE),
cols: z.number().int().min(1).max(WORKSHEET_LIMITS.MAX_COLS),
pages: z.number().int().min(1).max(WORKSHEET_LIMITS.MAX_PAGES),
orientation: z.enum(['portrait', 'landscape']),
name: z.string(),
pAnyStart: z.number().min(0).max(1),
pAllStart: z.number().min(0).max(1),
interpolate: z.boolean(),
showCarryBoxes: z.boolean(),
showAnswerBoxes: z.boolean(),
showPlaceValueColors: z.boolean(),
showProblemNumbers: z.boolean(),
showCellBorder: z.boolean(),
showTenFrames: z.boolean(),
showTenFramesForAll: z.boolean(),
fontSize: z
.number()
.int()
.min(WORKSHEET_LIMITS.FONT_SIZE.MIN)
.max(WORKSHEET_LIMITS.FONT_SIZE.MAX),
// Problem reproducibility (CRITICAL for sharing worksheets)
seed: z.number().int().min(0).optional(),
prngAlgorithm: z.string().optional(),
})
export type AdditionConfigV1 = z.infer<typeof additionConfigV1Schema>
/**
* Addition worksheet config - Version 2
* Custom difficulty system with conditional display rules
*/
export const additionConfigV2Schema = z.object({
version: z.literal(2),
problemsPerPage: z.number().int().min(1).max(WORKSHEET_LIMITS.MAX_PROBLEMS_PER_PAGE),
cols: z.number().int().min(1).max(WORKSHEET_LIMITS.MAX_COLS),
pages: z.number().int().min(1).max(WORKSHEET_LIMITS.MAX_PAGES),
orientation: z.enum(['portrait', 'landscape']),
name: z.string(),
pAnyStart: z.number().min(0).max(1),
pAllStart: z.number().min(0).max(1),
interpolate: z.boolean(),
// V2: Display rules replace individual booleans
displayRules: z.object({
carryBoxes: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
answerBoxes: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
placeValueColors: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
tenFrames: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
problemNumbers: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
cellBorders: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
borrowNotation: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
borrowingHints: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
}),
// V2: Track which difficulty profile is active
difficultyProfile: z.string().optional(),
// V2: Keep fontSize and showTenFramesForAll for now (may refactor later)
fontSize: z
.number()
.int()
.min(WORKSHEET_LIMITS.FONT_SIZE.MIN)
.max(WORKSHEET_LIMITS.FONT_SIZE.MAX),
showTenFramesForAll: z.boolean(),
// Problem reproducibility (CRITICAL for sharing worksheets)
seed: z.number().int().min(0).optional(),
prngAlgorithm: z.string().optional(),
})
export type AdditionConfigV2 = z.infer<typeof additionConfigV2Schema>
/**
* Addition worksheet config - Version 3
* Two-mode system: Custom Difficulty vs Manual Control
*/
// Shared base fields for both modes
const additionConfigV3BaseSchema = z.object({
version: z.literal(3),
// Core worksheet settings
problemsPerPage: z.number().int().min(1).max(WORKSHEET_LIMITS.MAX_PROBLEMS_PER_PAGE),
cols: z.number().int().min(1).max(WORKSHEET_LIMITS.MAX_COLS),
pages: z.number().int().min(1).max(WORKSHEET_LIMITS.MAX_PAGES),
orientation: z.enum(['portrait', 'landscape']),
name: z.string(),
fontSize: z
.number()
.int()
.min(WORKSHEET_LIMITS.FONT_SIZE.MIN)
.max(WORKSHEET_LIMITS.FONT_SIZE.MAX),
// Regrouping probabilities (shared between modes)
pAnyStart: z.number().min(0).max(1),
pAllStart: z.number().min(0).max(1),
interpolate: z.boolean(),
// Problem reproducibility (CRITICAL for sharing worksheets)
seed: z.number().int().min(0).optional(),
prngAlgorithm: z.string().optional(),
})
// Custom Difficulty Mode
const additionConfigV3CustomSchema = additionConfigV3BaseSchema.extend({
mode: z.literal('custom'),
// Conditional display rules
displayRules: z.object({
carryBoxes: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
answerBoxes: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
placeValueColors: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
tenFrames: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
problemNumbers: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
cellBorders: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
borrowNotation: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
borrowingHints: z.enum([
'always',
'never',
'whenRegrouping',
'whenMultipleRegroups',
'when3PlusDigits',
]),
}),
// Optional: Which custom difficulty profile is selected
difficultyProfile: z.string().optional(),
// showTenFramesForAll is deprecated in V3 custom mode
// (controlled by displayRules.tenFrames)
})
// Manual Control Mode
const additionConfigV3ManualSchema = additionConfigV3BaseSchema.extend({
mode: z.literal('manual'),
// Simple boolean toggles
showCarryBoxes: z.boolean(),
showAnswerBoxes: z.boolean(),
showPlaceValueColors: z.boolean(),
showTenFrames: z.boolean(),
showProblemNumbers: z.boolean(),
showCellBorder: z.boolean(),
showTenFramesForAll: z.boolean(),
// Optional: Which manual preset is selected
manualPreset: z.string().optional(),
})
// V3 uses discriminated union on 'mode'
export const additionConfigV3Schema = z.discriminatedUnion('mode', [
additionConfigV3CustomSchema,
additionConfigV3ManualSchema,
])
export type AdditionConfigV3 = z.infer<typeof additionConfigV3Schema>
export type AdditionConfigV3Custom = z.infer<typeof additionConfigV3CustomSchema>
export type AdditionConfigV3Manual = z.infer<typeof additionConfigV3ManualSchema>
/**
* Addition worksheet config - Version 4
* Adds support for variable digit ranges (1-5 digits per number)
*/
// Shared base fields for V4
const additionConfigV4BaseSchema = z.object({
version: z.literal(4),
// Core worksheet settings
problemsPerPage: z.number().int().min(1).max(WORKSHEET_LIMITS.MAX_PROBLEMS_PER_PAGE),
cols: z.number().int().min(1).max(WORKSHEET_LIMITS.MAX_COLS),
pages: z.number().int().min(1).max(WORKSHEET_LIMITS.MAX_PAGES),
orientation: z.enum(['portrait', 'landscape']),
name: z.string(),
fontSize: z
.number()
.int()
.min(WORKSHEET_LIMITS.FONT_SIZE.MIN)
.max(WORKSHEET_LIMITS.FONT_SIZE.MAX),
// V4: Digit range for problem generation
digitRange: z
.object({
min: z
.number()
.int()
.min(WORKSHEET_LIMITS.DIGIT_RANGE.MIN)
.max(WORKSHEET_LIMITS.DIGIT_RANGE.MAX),
max: z
.number()
.int()
.min(WORKSHEET_LIMITS.DIGIT_RANGE.MIN)
.max(WORKSHEET_LIMITS.DIGIT_RANGE.MAX),
})
.refine((data) => data.min <= data.max, {
message: 'min must be less than or equal to max',
}),
// V4: Operator selection (addition, subtraction, or mixed)
operator: z.enum(['addition', 'subtraction', 'mixed']).default('addition'),
// Regrouping probabilities (shared between modes)
pAnyStart: z.number().min(0).max(1),
pAllStart: z.number().min(0).max(1),
interpolate: z.boolean(),
// V4: Include answer key pages at end of PDF
includeAnswerKey: z.boolean().optional().default(false),
// V4: Include QR code linking to shared worksheet on each page
includeQRCode: z.boolean().optional().default(false),
// Problem reproducibility (CRITICAL for sharing worksheets)
seed: z.number().int().min(0).optional(),
prngAlgorithm: z.string().optional(),
})
// Custom Difficulty Mode for V4
const additionConfigV4CustomSchema = additionConfigV4BaseSchema.extend({
mode: z.literal('custom'),
// Conditional display rules (with 'auto' for deferring to custom difficulty)
displayRules: z.object({
carryBoxes: z.enum(displayRuleValues),
answerBoxes: z.enum(displayRuleValues),
placeValueColors: z.enum(displayRuleValues),
tenFrames: z.enum(displayRuleValues),
problemNumbers: z.enum(displayRuleValues),
cellBorders: z.enum(displayRuleValues),
borrowNotation: z.enum(displayRuleValues),
borrowingHints: z.enum(displayRuleValues),
}),
// Optional: Which custom difficulty profile is selected
difficultyProfile: z.string().optional(),
})
// Manual Control Mode for V4
// Now uses displayRules like Custom/Mastery modes for 1:1 correspondence
const additionConfigV4ManualSchema = additionConfigV4BaseSchema.extend({
mode: z.literal('manual'),
// Manual mode now uses conditional display rules (same as Custom/Mastery)
// 'auto' is available but typically not used in manual mode
displayRules: z.object({
carryBoxes: z.enum(displayRuleValues),
answerBoxes: z.enum(displayRuleValues),
placeValueColors: z.enum(displayRuleValues),
tenFrames: z.enum(displayRuleValues),
problemNumbers: z.enum(displayRuleValues),
cellBorders: z.enum(displayRuleValues),
borrowNotation: z.enum(displayRuleValues),
borrowingHints: z.enum(displayRuleValues),
}),
// Optional: Which manual preset is selected
manualPreset: z.string().optional(),
})
// Mastery Progression Mode for V4
const additionConfigV4MasterySchema = additionConfigV4BaseSchema.extend({
mode: z.literal('mastery'),
// Mastery mode uses displayRules with 'auto' for deferring to skill recommendations
displayRules: z.object({
carryBoxes: z.enum(displayRuleValues),
answerBoxes: z.enum(displayRuleValues),
placeValueColors: z.enum(displayRuleValues),
tenFrames: z.enum(displayRuleValues),
problemNumbers: z.enum(displayRuleValues),
cellBorders: z.enum(displayRuleValues),
borrowNotation: z.enum(displayRuleValues),
borrowingHints: z.enum(displayRuleValues),
}),
// Optional: Separate display rules for mixed mode (operator-specific scaffolding)
// When operator='mixed', additionDisplayRules applies to addition problems,
// subtractionDisplayRules applies to subtraction problems
// Each can independently use 'auto' to defer to the operator-specific skill recommendation
additionDisplayRules: z
.object({
carryBoxes: z.enum(displayRuleValues),
answerBoxes: z.enum(displayRuleValues),
placeValueColors: z.enum(displayRuleValues),
tenFrames: z.enum(displayRuleValues),
problemNumbers: z.enum(displayRuleValues),
cellBorders: z.enum(displayRuleValues),
borrowNotation: z.enum(displayRuleValues),
borrowingHints: z.enum(displayRuleValues),
})
.optional(),
subtractionDisplayRules: z
.object({
carryBoxes: z.enum(displayRuleValues),
answerBoxes: z.enum(displayRuleValues),
placeValueColors: z.enum(displayRuleValues),
tenFrames: z.enum(displayRuleValues),
problemNumbers: z.enum(displayRuleValues),
cellBorders: z.enum(displayRuleValues),
borrowNotation: z.enum(displayRuleValues),
borrowingHints: z.enum(displayRuleValues),
})
.optional(),
// Optional: Current step in mastery progression path
currentStepId: z.string().optional(),
// Optional: Current skills for mixed mode (operator-specific progression)
currentAdditionSkillId: z.string().optional(),
currentSubtractionSkillId: z.string().optional(),
})
// V4 uses discriminated union on 'mode'
export const additionConfigV4Schema = z.discriminatedUnion('mode', [
additionConfigV4CustomSchema,
additionConfigV4ManualSchema,
additionConfigV4MasterySchema,
])
export type AdditionConfigV4 = z.infer<typeof additionConfigV4Schema>
export type AdditionConfigV4Custom = z.infer<typeof additionConfigV4CustomSchema>
export type AdditionConfigV4Manual = z.infer<typeof additionConfigV4ManualSchema>
export type AdditionConfigV4Mastery = z.infer<typeof additionConfigV4MasterySchema>
/** Union of all addition config versions (add new versions here) */
export const additionConfigSchema = z.discriminatedUnion('version', [
additionConfigV1Schema,
additionConfigV2Schema,
additionConfigV3Schema,
additionConfigV4Schema,
])
export type AdditionConfig = z.infer<typeof additionConfigSchema>
/**
* Default addition config (always latest version - V4 Custom Mode)
*/
export const defaultAdditionConfig: AdditionConfigV4Custom = {
version: 4,
mode: 'custom',
problemsPerPage: 20,
cols: 5,
pages: 1,
orientation: 'landscape',
name: '',
digitRange: { min: 2, max: 2 }, // V4: Default to 2-digit problems (backward compatible)
operator: 'addition',
pAnyStart: 0.25,
pAllStart: 0,
interpolate: true,
displayRules: {
carryBoxes: 'whenRegrouping',
answerBoxes: 'always',
placeValueColors: 'always',
tenFrames: 'whenRegrouping',
problemNumbers: 'always',
cellBorders: 'always',
borrowNotation: 'whenRegrouping',
borrowingHints: 'never',
},
difficultyProfile: 'earlyLearner',
fontSize: 16,
includeAnswerKey: false,
includeQRCode: false,
}
/**
* Migrate V1 config to V2
* Converts boolean display flags to conditional display rules
*/
function migrateAdditionV1toV2(v1: AdditionConfigV1): AdditionConfigV2 {
// Convert V1 boolean flags to V2 rule modes
// V1 booleans were always on/off, so map true→'always', false→'never'
const displayRules: AdditionConfigV2['displayRules'] = {
carryBoxes: v1.showCarryBoxes ? 'always' : 'never',
answerBoxes: v1.showAnswerBoxes ? 'always' : 'never',
placeValueColors: v1.showPlaceValueColors ? 'always' : 'never',
tenFrames: v1.showTenFrames ? 'always' : 'never',
problemNumbers: v1.showProblemNumbers ? 'always' : 'never',
cellBorders: v1.showCellBorder ? 'always' : 'never',
borrowNotation: 'whenRegrouping', // V1 didn't have this field, use reasonable default
borrowingHints: 'never', // V1 didn't have this field, use reasonable default
}
// Try to match config to a known profile
const profileName = getProfileFromConfig(v1.pAllStart, v1.pAnyStart, displayRules)
return {
version: 2,
problemsPerPage: v1.problemsPerPage,
cols: v1.cols,
pages: v1.pages,
orientation: v1.orientation,
name: v1.name,
pAnyStart: v1.pAnyStart,
pAllStart: v1.pAllStart,
interpolate: v1.interpolate,
displayRules,
difficultyProfile: profileName === 'custom' ? undefined : profileName,
showTenFramesForAll: v1.showTenFramesForAll,
fontSize: v1.fontSize,
}
}
/**
* Migrate V2 config to V3
* Determines mode based on whether difficultyProfile is set
*/
function migrateAdditionV2toV3(v2: AdditionConfigV2): AdditionConfigV3 {
// If user has a difficultyProfile set, they're using custom mode
if (v2.difficultyProfile) {
return {
version: 3,
mode: 'custom',
problemsPerPage: v2.problemsPerPage,
cols: v2.cols,
pages: v2.pages,
orientation: v2.orientation,
name: v2.name,
fontSize: v2.fontSize,
pAnyStart: v2.pAnyStart,
pAllStart: v2.pAllStart,
interpolate: v2.interpolate,
displayRules: v2.displayRules,
difficultyProfile: v2.difficultyProfile,
// CRITICAL: Preserve seed/prngAlgorithm for problem reproducibility
seed: v2.seed,
prngAlgorithm: v2.prngAlgorithm,
}
}
// No preset → Manual mode
// Convert displayRules to boolean flags
return {
version: 3,
mode: 'manual',
problemsPerPage: v2.problemsPerPage,
cols: v2.cols,
pages: v2.pages,
orientation: v2.orientation,
name: v2.name,
fontSize: v2.fontSize,
pAnyStart: v2.pAnyStart,
pAllStart: v2.pAllStart,
interpolate: v2.interpolate,
showCarryBoxes: v2.displayRules.carryBoxes === 'always',
showAnswerBoxes: v2.displayRules.answerBoxes === 'always',
showPlaceValueColors: v2.displayRules.placeValueColors === 'always',
showTenFrames: v2.displayRules.tenFrames === 'always',
showProblemNumbers: v2.displayRules.problemNumbers === 'always',
showCellBorder: v2.displayRules.cellBorders === 'always',
showTenFramesForAll: v2.showTenFramesForAll,
// CRITICAL: Preserve seed/prngAlgorithm for problem reproducibility
seed: v2.seed,
prngAlgorithm: v2.prngAlgorithm,
}
}
/**
* Migrate V3 config to V4
* Adds digitRange field with default of { min: 2, max: 2 } for backward compatibility
*/
function migrateAdditionV3toV4(v3: AdditionConfigV3): AdditionConfigV4 {
// V3 configs didn't have digitRange or operator, so default to 2-digit addition problems
const baseFields = {
version: 4 as const,
problemsPerPage: v3.problemsPerPage,
cols: v3.cols,
pages: v3.pages,
orientation: v3.orientation,
name: v3.name,
fontSize: v3.fontSize,
digitRange: { min: 2, max: 2 }, // V4: Default to 2-digit for backward compatibility
operator: 'addition' as const, // V4: Default to addition for backward compatibility
pAnyStart: v3.pAnyStart,
pAllStart: v3.pAllStart,
interpolate: v3.interpolate,
// CRITICAL: Preserve seed/prngAlgorithm for problem reproducibility
seed: v3.seed,
prngAlgorithm: v3.prngAlgorithm,
// V4: New fields with backward-compatible defaults
includeAnswerKey: false,
includeQRCode: false,
}
if (v3.mode === 'custom') {
return {
...baseFields,
mode: 'custom',
displayRules: v3.displayRules,
difficultyProfile: v3.difficultyProfile,
}
} else {
// V3 Manual mode used boolean flags, V4 Manual mode uses displayRules
// Convert boolean flags to displayRules (true → 'always', false → 'never')
return {
...baseFields,
mode: 'manual',
displayRules: {
carryBoxes: v3.showCarryBoxes ? 'always' : 'never',
answerBoxes: v3.showAnswerBoxes ? 'always' : 'never',
placeValueColors: v3.showPlaceValueColors ? 'always' : 'never',
tenFrames: v3.showTenFrames ? 'always' : 'never',
problemNumbers: v3.showProblemNumbers ? 'always' : 'never',
cellBorders: v3.showCellBorder ? 'always' : 'never',
borrowNotation: 'always', // V4: Default to 'always' for backward compatibility
borrowingHints: 'never', // V4: Default to 'never' for backward compatibility
},
manualPreset: v3.manualPreset,
}
}
}
/**
* Migrate addition config from any version to latest (V4)
* @throws {Error} if config is invalid or migration fails
*/
export function migrateAdditionConfig(rawConfig: unknown): AdditionConfigV4 {
// First, try to parse as any known version
const parsed = additionConfigSchema.safeParse(rawConfig)
if (!parsed.success) {
// If parsing fails completely, return defaults
console.warn('Failed to parse addition config, using defaults:', parsed.error)
return defaultAdditionConfig
}
const config = parsed.data
// Migrate to latest version
switch (config.version) {
case 1:
// Migrate V1 → V2 → V3 → V4
return migrateAdditionV3toV4(migrateAdditionV2toV3(migrateAdditionV1toV2(config)))
case 2:
// Migrate V2 → V3 → V4
return migrateAdditionV3toV4(migrateAdditionV2toV3(config))
case 3:
// Migrate V3 → V4
return migrateAdditionV3toV4(config)
case 4:
// Already latest version
return config
default:
// Unknown version, return defaults
console.warn(`Unknown addition config version: ${(config as any).version}`)
return defaultAdditionConfig
}
}
/**
* Parse and validate addition config from JSON string
* Automatically migrates old versions to latest (V4)
*/
export function parseAdditionConfig(jsonString: string): AdditionConfigV4 {
try {
const raw = JSON.parse(jsonString)
return migrateAdditionConfig(raw)
} catch (error) {
console.error('Failed to parse addition config JSON:', error)
return defaultAdditionConfig
}
}
/**
* Serialize addition config to JSON string
* Ensures version field is set to current version (V4)
*/
export function serializeAdditionConfig(config: Omit<AdditionConfigV4, 'version'>): string {
const versioned: AdditionConfigV4 = {
...config,
version: ADDITION_CURRENT_VERSION,
} as AdditionConfigV4
return JSON.stringify(versioned)
}
// =============================================================================
// FUTURE WORKSHEET TYPES (subtraction, multiplication, etc.)
// =============================================================================
// Example structure for future worksheet types:
//
// export const subtractionConfigV1Schema = z.object({
// version: z.literal(1),
// // ... fields specific to subtraction worksheets
// })
//
// export function parseSubtractionConfig(jsonString: string): SubtractionConfigV1 {
// // ... similar to parseAdditionConfig
// }
|