// Utilities

/**
 * Return values scaled by baseline
 */
export const baselineScale = (values, baseline) => {
  return values.map(d => {
    return baseline ? ((d / baseline) - 1) * 100 : -1
  })
}

/**
 * Trim history data to fit in length 'numWeeks'
 */
export const trimHistory = (historyActual, numWeeks) => {
  let historyTrimmed = historyActual.slice()

  if (numWeeks === 52) {
    // Clip everyone else to remove 53rd week
    historyTrimmed = historyTrimmed.filter(d => d.week % 100 !== 53)
  } else if (historyTrimmed.length === 52) {
    // Expand to add 53rd week
    // Adding a dummy year 1000, this will also help identify the adjustment
    historyTrimmed.splice(23, 0, {
      week: 100053,
      data: (historyTrimmed[22].data + historyTrimmed[23].data) / 2.0
    })
  }

  return historyTrimmed.map(d => d.data)
}

/**
 * Return range for choropleth color scale
 */
export const choroplethDataRange = (seasonsData, relativeToggle) => {
  let maxVals = []
  let minVals = []

  seasonsData.map(seasonData => {
    seasonData.regions.map(regionData => {
      let actual = regionData.actual.map(d => d.actual).filter(d => d)

      if (relativeToggle) {
        // Use baseline scaled data
        maxVals.push(Math.max(...actual.map(d => ((d / regionData.baseline) - 1) * 100)))
        minVals.push(Math.min(...actual.map(d => ((d / regionData.baseline) - 1) * 100)))
      } else {
        maxVals.push(Math.max(...actual))
        minVals.push(Math.min(...actual))
      }
    })
  })

  return [Math.min(...minVals), Math.max(...maxVals)]
}

@@ -1503,22 +1503,27 @@
   %7D)%0A%0A  
-return
+let range =
  %5BMath.m
@@ -1559,11 +1559,326 @@
 axVals)%5D
+%0A%0A  // Patch range to work for new season data%0A  if (relativeToggle) %7B%0A    // Fix the range as -150 to 150%25 in worst case%0A    let clipped = Math.max(...range.map(Math.abs), 150)%0A    return %5B-clipped, clipped%5D%0A  %7D else %7B%0A    // Fix the range as 0 to 10 in worst case%0A    return %5Brange%5B0%5D, Math.max(10, range%5B1%5D)%5D%0A  %7D
 %0A%7D%0A

Patch choropleth range to handle new seasons