import {sum} from './index'

export function aggregateBuildCycles(numPlayers, numBuildCycles = 1) {
  if (numPlayers === null || numBuildCycles === null || isNaN(numPlayers) || isNaN(numBuildCycles)) {
    return null
  }
  return numPlayers * numBuildCycles
}

export function relativeContribution(rcScores) {
  if (!Array.isArray(rcScores) || !rcScores.length) {
    return null
  }
  const rcScoresSum = sum(rcScores)
  return Math.round(rcScoresSum / rcScores.length)
}

export function expectedContribution(playerHours, teamHours) {
  if (playerHours === null || isNaN(playerHours) || !teamHours) {
    return null
  }
  return Math.round((playerHours / teamHours) * 100)
}

export function expectedContributionDelta(ec, rc) {
  if (ec === null || rc === null || isNaN(ec) || isNaN(rc)) {
    return null
  }
  return rc - ec
}

export function effectiveContributionCycles(abc, rc) {
  if (abc === null || rc === null || isNaN(abc) || isNaN(rc)) {
    return null
  }
  return abc * rc
}

export function learningSupport(lsScores) {
  return averageScore(lsScores)
}

export function cultureContrbution(ccScores) {
  return averageScore(ccScores)
}

export const SCORE_MIN = 1
export const SCORE_MAX = 7
export function averageScore(scores) {
  if (!Array.isArray(scores) || !scores.length) {
    return null
  }
  const adjustedScores = scores.filter(n => (n >= SCORE_MIN && n <= SCORE_MAX))
  const adjustedScoresSum = sum(adjustedScores.map(n => ((n - 1) / 6)))
  return Math.round((adjustedScoresSum / adjustedScores.length) * 100)
}

@@ -1202,16 +1202,65 @@
 MAX = 7%0A
+export const SCORE_RANGE = SCORE_MAX - SCORE_MIN%0A
 export f
@@ -1439,16 +1439,40 @@
 RE_MAX))
+.map(n =%3E n - SCORE_MIN)
 %0A  const
@@ -1524,32 +1524,55 @@
  =%3E 
-((n - 1) / 6)))%0A  return
+n / SCORE_RANGE))%0A  const averageScorePercent =
  Mat
@@ -1622,18 +1622,47 @@
 .length) * 100)%0A
+  return averageScorePercent%0A
 %7D%0A

use min/max score constants to adjust/shift survey scores