'use strict';

exports.__esModule = true;
exports.useWithFallback = exports.distribution = undefined;

var _ramda = require('ramda');

var _ramda2 = _interopRequireDefault(_ramda);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactSeeds = require('react-seeds');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

var medianOf = function medianOf(numbers) {
	var sorted = _ramda2.default.sortBy(_ramda2.default.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;
};

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

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