All files / web/src/app/create/worksheets/components/config-sidebar ScaffoldingTab.tsx

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

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

import { css } from '@styled/css'
import { stack } from '@styled/patterns'
import { useWorksheetConfig } from '../WorksheetConfigContext'
import { useTheme } from '@/contexts/ThemeContext'
import { RuleThermometer } from '../config-panel/RuleThermometer'
import type { DisplayRules } from '../../displayRules'
import { defaultAdditionConfig } from '@/app/create/worksheets/config-schemas'
import { getSkillById } from '../../skills'

export function ScaffoldingTab() {
  const { formState, onChange } = useWorksheetConfig()
  const { resolvedTheme } = useTheme()
  const isDark = resolvedTheme === 'dark'

  const displayRules: DisplayRules = formState.displayRules ?? defaultAdditionConfig.displayRules

  // Check if we're in mastery+mixed mode (needs operator-specific rules)
  const isMasteryMixed = formState.mode === 'mastery' && formState.operator === 'mixed'

  // Get resolved display rules for showing what 'auto' defers to
  let resolvedDisplayRules: DisplayRules | undefined
  let resolvedAdditionRules: DisplayRules | undefined
  let resolvedSubtractionRules: DisplayRules | undefined

  if (formState.mode === 'mastery') {
    const operator = formState.operator ?? 'addition'

    if (operator === 'mixed') {
      // Mixed mode: Get BOTH addition and subtraction skill recommendations
      const addSkillId = formState.currentAdditionSkillId
      const subSkillId = formState.currentSubtractionSkillId

      if (addSkillId) {
        const skill = getSkillById(addSkillId as any)
        resolvedAdditionRules = skill?.recommendedScaffolding
      }

      if (subSkillId) {
        const skill = getSkillById(subSkillId as any)
        resolvedSubtractionRules = skill?.recommendedScaffolding
      }
    } else {
      // Single operator: Use its skill's recommendations
      const skillId =
        operator === 'addition'
          ? formState.currentAdditionSkillId
          : formState.currentSubtractionSkillId

      if (skillId) {
        const skill = getSkillById(skillId as any)
        resolvedDisplayRules = skill?.recommendedScaffolding
      }
    }
  }

  const updateRule = (key: keyof DisplayRules, value: DisplayRules[keyof DisplayRules]) => {
    const newDisplayRules = {
      ...displayRules,
      [key]: value,
    }

    console.log('[ScaffoldingTab] updateRule called:', {
      key,
      value,
      isMasteryMixed,
      newDisplayRules,
    })

    // In mastery+mixed mode, update both general AND operator-specific display rules
    if (isMasteryMixed) {
      console.log('[ScaffoldingTab] Updating ALL operator-specific rules (mastery+mixed)')
      onChange({
        displayRules: newDisplayRules,
        additionDisplayRules: newDisplayRules,
        subtractionDisplayRules: newDisplayRules,
      })
    } else {
      console.log('[ScaffoldingTab] Updating only global displayRules')
      onChange({
        displayRules: newDisplayRules,
      })
    }
  }

  // Get skill names for auto notice
  const getAutoNoticeText = () => {
    if (formState.mode !== 'mastery') return null

    const operator = formState.operator ?? 'addition'

    if (operator === 'mixed') {
      const addSkill = formState.currentAdditionSkillId
        ? getSkillById(formState.currentAdditionSkillId as any)
        : null
      const subSkill = formState.currentSubtractionSkillId
        ? getSkillById(formState.currentSubtractionSkillId as any)
        : null

      if (addSkill && subSkill) {
        return `Auto uses ${addSkill.name} (addition) and ${subSkill.name} (subtraction) recommendations`
      }
    } else {
      const skillId =
        operator === 'addition'
          ? formState.currentAdditionSkillId
          : formState.currentSubtractionSkillId
      const skill = skillId ? getSkillById(skillId as any) : null

      if (skill) {
        return `Auto uses ${skill.name} recommendations`
      }
    }

    return null
  }

  const autoNoticeText = getAutoNoticeText()

  return (
    <div data-component="scaffolding-tab" className={stack({ gap: '3' })}>
      {/* Quick presets */}
      <div
        className={css({
          display: 'flex',
          justifyContent: 'space-between',
          alignItems: 'center',
        })}
      >
        <div
          className={css({
            fontSize: 'xs',
            fontWeight: 'semibold',
            color: isDark ? 'gray.400' : 'gray.500',
            textTransform: 'uppercase',
            letterSpacing: 'wider',
          })}
        >
          Quick Presets
        </div>
        <div className={css({ display: 'flex', gap: '1.5' })}>
          {/* Auto preset - defer to mastery progression (only show in mastery mode) */}
          {formState.mode === 'mastery' && (
            <button
              onClick={() => {
                const newDisplayRules = {
                  ...displayRules,
                  carryBoxes: 'auto' as const,
                  answerBoxes: 'auto' as const,
                  placeValueColors: 'auto' as const,
                  tenFrames: 'auto' as const,
                  borrowNotation: 'auto' as const,
                  borrowingHints: 'auto' as const,
                }
                // In mastery+mixed mode, update operator-specific rules too
                if (isMasteryMixed) {
                  onChange({
                    displayRules: newDisplayRules,
                    additionDisplayRules: newDisplayRules,
                    subtractionDisplayRules: newDisplayRules,
                  })
                } else {
                  onChange({
                    displayRules: newDisplayRules,
                  })
                }
              }}
              className={css({
                px: '2',
                py: '0.5',
                fontSize: '2xs',
                color: isDark ? 'green.300' : 'green.600',
                bg: isDark ? 'green.900/30' : 'green.50',
                border: '1px solid',
                borderColor: isDark ? 'green.700' : 'green.300',
                rounded: 'md',
                cursor: 'pointer',
                _hover: { bg: isDark ? 'green.800/40' : 'green.100' },
              })}
            >
              Auto
            </button>
          )}
          <button
            onClick={() => {
              const newDisplayRules = {
                ...displayRules,
                carryBoxes: 'always' as const,
                answerBoxes: 'always' as const,
                placeValueColors: 'always' as const,
                tenFrames: 'always' as const,
                borrowNotation: 'always' as const,
                borrowingHints: 'always' as const,
              }
              // In mastery+mixed mode, update operator-specific rules too
              if (isMasteryMixed) {
                onChange({
                  displayRules: newDisplayRules,
                  additionDisplayRules: newDisplayRules,
                  subtractionDisplayRules: newDisplayRules,
                })
              } else {
                onChange({
                  displayRules: newDisplayRules,
                })
              }
            }}
            className={css({
              px: '2',
              py: '0.5',
              fontSize: '2xs',
              color: isDark ? 'brand.300' : 'brand.600',
              border: '1px solid',
              borderColor: isDark ? 'brand.500' : 'brand.300',
              bg: isDark ? 'gray.700' : 'white',
              rounded: 'md',
              cursor: 'pointer',
              _hover: { bg: isDark ? 'gray.600' : 'brand.50' },
            })}
          >
            All Always
          </button>
          <button
            onClick={() => {
              const newDisplayRules = {
                ...displayRules,
                carryBoxes: 'never' as const,
                answerBoxes: 'never' as const,
                placeValueColors: 'never' as const,
                tenFrames: 'never' as const,
                borrowNotation: 'never' as const,
                borrowingHints: 'never' as const,
              }
              // In mastery+mixed mode, update operator-specific rules too
              if (isMasteryMixed) {
                onChange({
                  displayRules: newDisplayRules,
                  additionDisplayRules: newDisplayRules,
                  subtractionDisplayRules: newDisplayRules,
                })
              } else {
                onChange({
                  displayRules: newDisplayRules,
                })
              }
            }}
            className={css({
              px: '2',
              py: '0.5',
              fontSize: '2xs',
              color: isDark ? 'gray.300' : 'gray.600',
              border: '1px solid',
              borderColor: isDark ? 'gray.500' : 'gray.300',
              bg: isDark ? 'gray.700' : 'white',
              rounded: 'md',
              cursor: 'pointer',
              _hover: { bg: isDark ? 'gray.600' : 'gray.50' },
            })}
          >
            Minimal
          </button>
        </div>
      </div>

      {/* Auto notice - shows what skill auto defers to */}
      {autoNoticeText && (
        <div
          className={css({
            fontSize: '2xs',
            color: isDark ? 'gray.500' : 'gray.500',
            fontStyle: 'italic',
            px: '1',
          })}
        >
          {autoNoticeText}
        </div>
      )}

      {/* Pedagogical scaffolding thermometers */}
      <RuleThermometer
        label="Answer Boxes"
        description="Guide students to write organized, aligned answers"
        value={displayRules.answerBoxes}
        onChange={(value) => updateRule('answerBoxes', value)}
        isDark={isDark}
        resolvedValue={resolvedDisplayRules?.answerBoxes}
        resolvedAdditionValue={resolvedAdditionRules?.answerBoxes}
        resolvedSubtractionValue={resolvedSubtractionRules?.answerBoxes}
      />

      <RuleThermometer
        label="Place Value Colors"
        description="Reinforce place value understanding visually"
        value={displayRules.placeValueColors}
        onChange={(value) => updateRule('placeValueColors', value)}
        isDark={isDark}
        resolvedValue={resolvedDisplayRules?.placeValueColors}
        resolvedAdditionValue={resolvedAdditionRules?.placeValueColors}
        resolvedSubtractionValue={resolvedSubtractionRules?.placeValueColors}
      />

      <RuleThermometer
        label={
          formState.operator === 'subtraction'
            ? 'Borrow Boxes'
            : formState.operator === 'mixed'
              ? 'Carry/Borrow Boxes'
              : 'Carry Boxes'
        }
        description={
          formState.operator === 'subtraction'
            ? 'Help students track borrowing during subtraction'
            : formState.operator === 'mixed'
              ? 'Help students track regrouping (carrying in addition, borrowing in subtraction)'
              : 'Help students track regrouping during addition'
        }
        value={displayRules.carryBoxes}
        onChange={(value) => updateRule('carryBoxes', value)}
        isDark={isDark}
        resolvedValue={resolvedDisplayRules?.carryBoxes}
        resolvedAdditionValue={resolvedAdditionRules?.carryBoxes}
        resolvedSubtractionValue={resolvedSubtractionRules?.carryBoxes}
      />

      {(formState.operator === 'subtraction' || formState.operator === 'mixed') && (
        <RuleThermometer
          label="Borrowed 10s Box"
          description="Box for adding 10 to borrowing digit"
          value={displayRules.borrowNotation}
          onChange={(value) => updateRule('borrowNotation', value)}
          isDark={isDark}
          resolvedValue={resolvedDisplayRules?.borrowNotation}
          resolvedAdditionValue={resolvedAdditionRules?.borrowNotation}
          resolvedSubtractionValue={resolvedSubtractionRules?.borrowNotation}
        />
      )}

      {(formState.operator === 'subtraction' || formState.operator === 'mixed') && (
        <RuleThermometer
          label="Borrowing Hints"
          description="Show arrows and calculations guiding the borrowing process"
          value={displayRules.borrowingHints}
          onChange={(value) => updateRule('borrowingHints', value)}
          isDark={isDark}
          resolvedValue={resolvedDisplayRules?.borrowingHints}
          resolvedAdditionValue={resolvedAdditionRules?.borrowingHints}
          resolvedSubtractionValue={resolvedSubtractionRules?.borrowingHints}
        />
      )}

      <RuleThermometer
        label="Ten-Frames"
        description="Visualize regrouping with concrete counting tools"
        value={displayRules.tenFrames}
        onChange={(value) => updateRule('tenFrames', value)}
        isDark={isDark}
        resolvedValue={resolvedDisplayRules?.tenFrames}
        resolvedAdditionValue={resolvedAdditionRules?.tenFrames}
        resolvedSubtractionValue={resolvedSubtractionRules?.tenFrames}
      />
    </div>
  )
}