import { QuizLetter } from '@logo-quiz/store' ; import { EMPTY_SPACE , FLUSH_LOGO , GUESS_LETTER , LogoActionTypes , LogoState , LogoStatus , NO_LETTER , REMOVE_LETTER_FROM_GUESS , REQUEST_LOGO , REQUEST_LOGO_SUCCESS , VERIFY_LOGO , VERIFY_LOGO_SUCCESS , } from './types' ; const initialState = { guess : [ ] , options : [ ] , logo : { } , isLoading : false , isVerifying : false , status : LogoStatus . Indeterminate , realImageUrl : '' , nextLogo : null , } ; export function logoReducer ( state = initialState , action ) { const guess = state . guess . slice ( ) ; switch ( action . type ) { case GUESS_LETTER : const availableIndex = state . guess . findIndex ( el => el . id === NO_LETTER . id ) ; if ( availableIndex !== - 1 ) { guess [ availableIndex ] = action . letter ; } return { ... state , guess , status : LogoStatus . Indeterminate } ; case REMOVE_LETTER_FROM_GUESS : const index = state . guess . findIndex ( el => el . id === action . letter . id ) ; if ( index !== - 1 ) { guess [ index ] = NO_LETTER ; } return { ... state , guess , status : LogoStatus . Indeterminate } ; case FLUSH_LOGO : return { ... state , ... initialState , } ; case REQUEST_LOGO : return { ... state , isLoading : true , } ; case REQUEST_LOGO_SUCCESS : const charMap = { '*' : NO_LETTER , _ : EMPTY_SPACE , } ; let initGuess = action . logo . obfuscatedName . split ( '' ) . map ( letter => charMap [ letter ] || { char : letter , id : - 3 } ) ; const initOptions = action . logo . letters . split ( '' ) . map ( ( char , id ) => ( { char , id } ) ) ; const logoName = action . logo . name ; if ( logoName ) { const cacheOptions = initOptions . slice ( ) ; initGuess = [ ] ; logoName . split ( '' ) . forEach ( letter => { const quizLetterIdx = cacheOptions . findIndex ( ( { char } ) => char === letter ) ; if ( quizLetterIdx < 0 ) { letter = letter === ' ' ? '_' : letter ; initGuess . push ( charMap [ letter ] || { char : letter , id : - 3 } ) ; } else { initGuess . push ( cacheOptions [ quizLetterIdx ] ) ; cacheOptions . splice ( quizLetterIdx , 1 ) ; } } ) ; } return { ... state , isLoading : false , logo : action . logo , guess : initGuess , options : initOptions , } ; case VERIFY_LOGO : return { ... state , isVerifying : true , isLoading : true , } ; case VERIFY_LOGO_SUCCESS : return { ... state , status : action . data . status ? LogoStatus . Valid : LogoStatus . Invalid , realImageUrl : action . data . realImageUrl , isVerifying : false , nextLogo : action . data . nextLogo , isGameCompleted : action . data . isGameCompleted , level : action . data . level , } ; default : return state ; } }