import filter from "lodash.filter" ; import sumBy from "lodash.sumby" ; import { translateQuizSections } from "../../i18n" ; import type { QuizQuestion , QuizSectionContent } from "../../types/quiz" ; export const getCardsFromSection = ( { quizQuestions , sectionIndex } ) : QuizQuestion [ ] => { const quizQuestionsContent = translateQuizSections ( "EarnScreen.earns" ) as QuizSectionContent [ ] ; const { allQuestions , myCompletedQuestions } = quizQuestions ; const cards = quizQuestionsContent [ sectionIndex ] . content . map ( ( card ) => { card . value = allQuestions [ card . id ] ; return card ; } ) ; cards . filter ( ( card ) => { card . fullfilled = myCompletedQuestions && Boolean ( myCompletedQuestions [ card . id ] ) ; } ) ; let allPreviousFullfilled = true ; let nonEnabledMessage = "" ; cards . forEach ( ( card ) => { card . enabled = true ; if ( allPreviousFullfilled === false ) { card . enabled = false ; card . nonEnabledMessage = nonEnabledMessage ; } if ( ! card . fullfilled && allPreviousFullfilled ) { allPreviousFullfilled = false ; nonEnabledMessage = card . title ; } } ) ; return cards ; } ; export const sectionCompletedPct = ( { quizQuestions , sectionIndex } ) : number => { try { const earns = getCardsFromSection ( { quizQuestions , sectionIndex } ) ; return earns . filter ( ( item ) => item . fullfilled ) . length / earns . length ; } catch ( err ) { return 0 ; } } ; export const remainingSatsOnSection = ( { quizQuestions , sectionIndex } ) : number => sumBy ( filter ( getCardsFromSection ( { quizQuestions , sectionIndex } ) , { fullfilled : false } ) , "value" ) ;