import enhanceAverageDurations from './enhanceAverageDurations'
import isDefined from './isDefined'

// Turns the durations of multiple domains into one array of durations
export default (domains, durations) => {

	// Enhance durations for all domains
	const enhancedDurations = domains.value.map((domain) => {

		const duration = durations.value[domain.data.id]
		const exists = duration != null

		return exists === true ? enhanceAverageDurations(duration.value, 14) : undefined

	})

	// Remove durations of domains that are still loading
	const filteredDurations = enhancedDurations.filter(isDefined)

	// Merge all durations to one array of durations
	return filteredDurations.reduce((acc, durations) => {

		// Durations is an array. Each item represents the average duration of one day.
		durations.forEach((duration, index) => {

			// The current day might be new as should be initialised first
			const initial = acc[index] == null ? 0 : acc[index]

			// Add the current day to the global array of days
			acc[index] = initial + duration

		})

		return acc

	}, [])

}
@@ -650,22 +650,39 @@
 ations%0A%09
-return
+const mergedDurations =
  filtere
@@ -1089,9 +1089,201 @@
 %7D, %5B%5D)%0A%0A
+%09// Convert merged, total durations into average durations%0A%09return mergedDurations.map((duration) =%3E %7B%0A%0A%09%09const totalDomains = filteredDurations.length%0A%0A%09%09return duration / totalDomains%0A%0A%09%7D)%0A%0A
 %7D

Convert into average durations