import { AxesResults , Results } from 'store/results/types' ; import { axes as axesSides } from './resultsCalculator' ; export type SpheresType = { economics : number ; social : number ; } ; export enum SpheresVariant { Classic , Extended } const sum = ( numbers ) => numbers . reduce ( ( total , aNumber ) => total + aNumber , 0 ) ; const average = ( numbers ) => sum ( numbers ) / numbers . length ; const calcEconomicsSphere = ( axes , method ) : number => { const calcExtended = ( ) => { const econAxesSides = axesSides . slice ( 0 , 2 ) ; const econLeft = econAxesSides . map ( ( side ) => axes [ side . left ] ) ; const econRight = econAxesSides . map ( ( side ) => axes [ side . right ] ) ; const econLeftAverage = average ( econLeft ) ; const econRightAverage = average ( econRight ) ; const econDifference = econRightAverage - econLeftAverage ; return econDifference / 100 ; } ; const calcClassic = ( ) => { const { left , right } = axesSides [ 0 ] ; const econLeft = axes [ left ] ; const econRight = axes [ right ] ; const econDifference = econRight - econLeft ; return econDifference / 100 ; } ; return method === SpheresVariant . Classic ? calcClassic ( ) : calcExtended ( ) ; } ; const calcSocialSphere = ( axes , method ) : number => { const calcExtended = ( ) => { const socialAxesSides = axesSides . slice ( 2 ) . filter ( ( side ) => ! side . notApplicableToSpheres ) ; const socialLeft = socialAxesSides . map ( ( side ) => axes [ side . left ] ) ; const socialRight = socialAxesSides . map ( ( side ) => axes [ side . right ] ) ; const socialLeftAverage = average ( socialLeft ) ; const socialRightAverage = average ( socialRight ) ; const socialDifference = socialRightAverage - socialLeftAverage ; return socialDifference / 100 ; } ; const calcClassic = ( ) => { const { left , right } = axesSides [ 2 ] ; const socialLeft = axes [ left ] ; const socialRight = axes [ right ] ; const socialDifference = socialRight - socialLeft ; return socialDifference / 100 ; } ; return method === SpheresVariant . Classic ? calcClassic ( ) : calcExtended ( ) ; } ; export const calcSpheresResults = ( results , method ) : SpheresType => ( { economics : calcEconomicsSphere ( results . axes , method ) , social : calcSocialSphere ( results . axes , method ) , } ) ; export default calcSpheresResults ;