import { first, isEmpty } from 'lodash' import { BioimpedanceWithWeight } from '../models/bioimpedance-with-weight.model' import { CompetitionCompetitor } from '../../../models/ranking' import { ScoreCalculatorParams, ScoreCalculatorResult } from './score.calculator' export abstract class GenericScoreCalculator { static calcule({ bioimpedance, competitor }: ScoreCalculatorParams): ScoreCalculatorResult { const value = this.getValue(bioimpedance) if (isEmpty(competitor.history)) return { value, score: 0 } return { value, score: this.getDifferencePercentage(this.getFirstValue(competitor), value) || 0 } } protected static getValue(_bioimpedance: BioimpedanceWithWeight): number { return 0 } protected static getFirstValue = (competitor: CompetitionCompetitor): number => first(competitor.history.sort((before, after) => (before.done_at < after.done_at ? -1 : 1))).value private static getDifferencePercentage = (firstValue: number, currentValue: number): number => ((firstValue - currentValue) * 100) / firstValue }