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

var Error = function Error(message) {
	return React.createElement(
		Seed,
		{ text: { color: 'red' } },
		message
	);
};

export var whatsize = function whatsize(tags, references, text, children, Element, resolveContent) {
	if (R.isNil(tags.within)) {
		return Error('Add tag #within');
	} else if (R.isNil(tags.count)) {
		return Error('Add tag #count');
	}

	var within = parseFloat(resolveContent(tags.within));
	var count = parseFloat(resolveContent(tags.count));

	return React.createElement(
		Seed,
		null,
		'Within ',
		within,
		' \xB7 Count ',
		count,
		' \xB7 Individual size = ',
		within / count
	);
};

export var howmany = function howmany(tags, references, text, children, Element, resolveContent) {
	if (R.isNil(tags.within)) {
		return Error('Add tag #within');
	} else if (R.isNil(tags.size)) {
		return Error('Add tag #size');
	}

	var within = parseFloat(resolveContent(tags.within));
	var size = parseFloat(resolveContent(tags.size));
	var count = within / size;
	var remainder = within % size;

	return React.createElement(
		Seed,
		null,
		React.createElement(
			Seed,
			{ row: true },
			React.createElement(Seed, { basis: Math.floor(count) * size, background: { color: '#e41' }, height: '1rem' }),
			React.createElement(Seed, { basis: remainder, background: { color: '#ed1' } })
		),
		'Within ',
		within,
		' \xB7 Size ',
		size,
		' \xB7 Count = ',
		count,
		' \xB7 Remainder = ',
		remainder
	);
};

export var useWithFallback = function useWithFallback(fallback) {
	return R.cond([[R.has('whatsize'), R.curry(whatsize)], [R.has('howmany'), R.curry(howmany)], [R.T, fallback]]);
};