import R from 'ramda';
import React from 'react';
import { Seed } from 'react-seeds';

var meanOf = function meanOf(numbers) {
	return R.sum(numbers) / numbers.length;
};

var medianOf = function medianOf(numbers) {
	var sorted = R.sortBy(R.identity, numbers);
	if (sorted.length % 2 == 1) {
		return sorted[(sorted.length - 1) / 2];
	} else {
		var secondIndex = sorted.length / 2;
		return (sorted[secondIndex - 1] + sorted[secondIndex]) / 2;
	}
};

var roundBy = function roundBy(number, precision) {
	var factor = Math.pow(10, precision);
	return Math.round(number * factor) / factor;
};

export var distribution = function distribution(tags, references, text, children, Element, resolveContent) {
	var numbers = R.map(R.pipe(resolveContent, parseFloat), children);
	console.log('numbers', numbers);
	return React.createElement(
		Seed,
		{ column: true, alignItems: 'center' },
		React.createElement('div', { children: 'Mean: ' + roundBy(meanOf(numbers), 3) }),
		React.createElement('div', { children: 'Median: ' + roundBy(medianOf(numbers), 3) })
	);
};

export var useWithFallback = function useWithFallback(fallback) {
	return R.cond([[R.anyPass([R.has('distribution'), R.has('mean'), R.has('average')]), R.curry(distribution)], [R.T, fallback]]);
};