import R from 'ramda';
import React from 'react';
import JSONComponent from './JSONComponent';

var toRecord = R.converge(R.call, [R.pipe(R.prop('children'), R.ifElse(R.isEmpty, function () {
	return withContent;
}, function (children) {
	return withChildren(children);
})), R.prop('text'), R.pipe(R.prop('tags'),
//R.filter(R.equals(true)),
R.keys)]);

var withContent = R.curry(function (content, tags) {
	return R.ifElse(R.isEmpty, R.always(content), R.pipe(R.reverse, R.reduce(R.flip(R.objOf), content)))(tags);
});

var withChildren = R.curry(function (children, content, tags) {
	return withContent(R.map(toRecord, children), tags);
});

// TODO: references, arrays with children
var toRecords = R.map(R.pipe(R.map(toRecord), R.ifElse(R.propSatisfies(R.gt(R.__, 1), 'length'), R.pipe(R.map(R.when(R.is(String), R.objOf('content'))), R.mergeAll), R.head)));

export var Preview = function Preview(_ref) {
	var ingredients = _ref.ingredients,
	    contentTree = _ref.contentTree;
	return React.createElement(JSONComponent, { json: toRecords(contentTree), isDeserialized: true });
};

export var title = 'Records';

export { head } from './Raw';