All files / web/src/app/create/worksheets/components ConfigPanel.tsx

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

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                                                                                                                                                                                                                                                                                                                                                       
'use client'

import { useEffect, useState } from 'react'
import { stack } from '@styled/patterns'
import { css } from '@styled/css'
import type { WorksheetFormState } from '@/app/create/worksheets/types'
import { defaultAdditionConfig } from '@/app/create/worksheets/config-schemas'
import { WorksheetConfigProvider } from './WorksheetConfigContext'
import { DifficultyMethodSelector } from './DifficultyMethodSelector'
import { StudentNameInput } from './config-panel/StudentNameInput'
import { OperatorSection } from './config-panel/OperatorSection'
import { ProgressiveDifficultyToggle } from './config-panel/ProgressiveDifficultyToggle'
import { CustomModeControls } from './config-panel/CustomModeControls'
import { MasteryModePanel } from './config-panel/MasteryModePanel'
import { DisplayControlsPanel } from './DisplayControlsPanel'
import { validateProblemSpace } from '@/app/create/worksheets/utils/validateProblemSpace'

interface ConfigPanelProps {
  formState: WorksheetFormState
  onChange: (updates: Partial<WorksheetFormState>) => void
  isDark?: boolean
}

export function ConfigPanel({ formState, onChange, isDark = false }: ConfigPanelProps) {
  const [warnings, setWarnings] = useState<string[]>([])

  // Validate problem space whenever relevant config changes
  useEffect(() => {
    const problemsPerPage = formState.problemsPerPage ?? 20
    const pages = formState.pages ?? 1
    const digitRange = formState.digitRange ?? { min: 2, max: 2 }
    const pAnyStart = formState.pAnyStart ?? 0
    const operator = formState.operator ?? 'addition'

    const validation = validateProblemSpace(problemsPerPage, pages, digitRange, pAnyStart, operator)

    console.log('[CONFIG PANEL] Problem space validation:', {
      problemsPerPage,
      pages,
      digitRange,
      pAnyStart,
      operator,
      estimatedSpace: validation.estimatedUniqueProblems,
      requested: validation.requestedProblems,
      risk: validation.duplicateRisk,
      warningCount: validation.warnings.length,
    })

    setWarnings(validation.warnings)
  }, [
    formState.problemsPerPage,
    formState.pages,
    formState.digitRange,
    formState.pAnyStart,
    formState.operator,
  ])

  // Handler for difficulty method switching (smart vs mastery)
  const handleMethodChange = (newMethod: 'custom' | 'mastery') => {
    const currentMethod = formState.mode === 'mastery' ? 'mastery' : 'custom'
    if (currentMethod === newMethod) {
      return // No change needed
    }

    // Preserve displayRules when switching
    const displayRules = formState.displayRules ?? defaultAdditionConfig.displayRules

    if (newMethod === 'custom') {
      onChange({
        mode: 'custom',
        displayRules,
        difficultyProfile: 'earlyLearner',
      } as unknown as Partial<WorksheetFormState>)
    } else {
      onChange({
        mode: 'mastery',
        displayRules,
      } as unknown as Partial<WorksheetFormState>)
    }
  }

  // Determine current method for selector
  const currentMethod = formState.mode === 'mastery' ? 'mastery' : 'custom'

  return (
    <WorksheetConfigProvider formState={formState} updateFormState={onChange}>
      <div data-component="config-panel" className={stack({ gap: '3' })}>
        {/* Problem Space Warnings */}
        {warnings.length > 0 && (
          <div
            data-element="problem-space-warning"
            className={css({
              bg: 'yellow.50',
              border: '1px solid',
              borderColor: 'yellow.200',
              rounded: 'md',
              p: '3',
              display: 'flex',
              flexDirection: 'column',
              gap: '2',
            })}
          >
            <div
              className={css({
                display: 'flex',
                alignItems: 'center',
                gap: '2',
                fontWeight: 'semibold',
                fontSize: 'sm',
                color: 'yellow.800',
              })}
            >
              <span>⚠️</span>
              <span>Duplicate Problem Risk</span>
            </div>
            <div
              className={css({
                fontSize: 'xs',
                color: 'yellow.900',
                whiteSpace: 'pre-wrap',
                lineHeight: '1.5',
              })}
            >
              {warnings.join('\n\n')}
            </div>
          </div>
        )}

        {/* Student Name */}
        <StudentNameInput
          value={formState.name}
          onChange={(name) => onChange({ name })}
          isDark={isDark}
        />

        {/* Operator Selector */}
        <OperatorSection
          operator={formState.operator}
          onChange={(operator) => onChange({ operator })}
          isDark={isDark}
        />

        {/* Progressive Difficulty Toggle */}
        <ProgressiveDifficultyToggle
          interpolate={formState.interpolate}
          onChange={(interpolate) => onChange({ interpolate })}
          isDark={isDark}
        />

        {/* Display Controls - Always visible for manual adjustment */}
        <DisplayControlsPanel formState={formState} onChange={onChange} isDark={isDark} />

        {/* Difficulty Method Selector (Smart vs Mastery) */}
        <DifficultyMethodSelector
          currentMethod={currentMethod}
          onChange={handleMethodChange}
          isDark={isDark}
        />

        {/* Method-specific preset controls */}
        {currentMethod === 'custom' && (
          <CustomModeControls formState={formState} onChange={onChange} />
        )}

        {currentMethod === 'mastery' && (
          <MasteryModePanel formState={formState} onChange={onChange} isDark={isDark} />
        )}
      </div>
    </WorksheetConfigProvider>
  )
}