'use strict';

exports.__esModule = true;
exports.head = exports.title = exports.Preview = undefined;

var _Raw = require('./Raw');

Object.defineProperty(exports, 'head', {
	enumerable: true,
	get: function get() {
		return _Raw.head;
	}
});

var _ramda = require('ramda');

var _ramda2 = _interopRequireDefault(_ramda);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _JSONComponent = require('./JSONComponent');

var _JSONComponent2 = _interopRequireDefault(_JSONComponent);

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

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

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

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

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

var Preview = exports.Preview = function Preview(_ref) {
	var ingredients = _ref.ingredients,
	    contentTree = _ref.contentTree;
	return _react2.default.createElement(_JSONComponent2.default, { json: toRecords(contentTree), isDeserialized: true });
};

var title = exports.title = 'Records';