import { Result } from '../useStore' ; const AVERAGE_CHARS_PER_WORD = 5 ; /**
 * For interim results, the first few seconds would give wild results. To curb
 * this, adjust the duration for the first few seconds to make it not show a
 * spike.
 */ const MINIMUM_INTERIM_DURATION = 3000 ; /**
 * Calculates wpm and accuracy
 */ export function buildResult ( options ) { const { durationMs } = options ; const adjustedDuration = options . isInterim ? Math . max ( durationMs , MINIMUM_INTERIM_DURATION ) : durationMs ; const expected = getLength ( options . tokens ) ; const mistakeCount = options . finishedTokens . map ( ( t ) => t ?. mistakes || 0 ) . reduce ( ( a , b ) => a + b ) ; const accuracy = 1 - mistakeCount / expected ; const netWords = ( expected - mistakeCount ) / AVERAGE_CHARS_PER_WORD ; const wpm = netWords / ( adjustedDuration / 60000 ) ; const result = { wpm , accuracy , mistakeCount , durationMs } ; return result ; } /**
 * Converts a list of tokens to a string, normalising whitespaces
 */ export function getLength ( tokens ) { return tokens . reduce ( ( acc , token ) => { if ( ! token ) return acc ; return token . type === 'whitespace' ? acc + 1 : acc + token . value . length ; } , 0 ) ; }