'use strict';

exports.__esModule = true;
exports.renderTreeUsing = exports.DefaultMaster = exports.DefaultSection = exports.renderElement = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

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 resolveContentIn = _ramda2.default.curry(function resolveItemIn(source, path) {
	//path = R.filter(R.isNil)
	console.log('resolveContentIn', path, source);
	path = _ramda2.default.insertAll(1, ['content'], path);
	return _ramda2.default.path(path, source);
});

var variationForItemIn = _ramda2.default.curry(function adjustItemIn(source, path) {
	var id = _ramda2.default.head(path);
	return _ramda2.default.path([id, 'variationReference'], source);
});

var resolveContentUsing = function resolveContentUsing(ingredients) {
	var resolveIngredientReference = resolveContentIn(ingredients);
	var variationForPath = variationForItemIn(ingredients);
	return function (value) {
		var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
		    _ref$single = _ref.single,
		    single = _ref$single === undefined ? true : _ref$single,
		    set = _ref.set,
		    alter = _ref.alter;

		if (_ramda2.default.isNil(value)) {
			return;
		} else if (value.mentions != null && value.mentions.length > 0 && value.mentions[0] != null) {
			console.log('MENTIONS', { set: set, alter: alter });
			if (set) {
				_ramda2.default.forEach(function (item) {
					return item.set(set);
				}, result);
			} else if (alter) {
				var path = value.mentions[0];
				var variation = variationForPath(path);
				console.log('altering variation', variation);
				var innerPath = _ramda2.default.tail(path);
				variation.adjustPath(innerPath, alter);
			} else {
				console.log('RESOLVE', value.mentions);
				var resolved = _ramda2.default.map(resolveIngredientReference, value.mentions);
				console.log('RESOLVED', resolved);
				if (resolved == null) {
					return;
				}
				return single ? resolved[0] : resolved;
			}
		} else if (value.texts != null) {
			return value.texts;
		} else {
			return value;
		}
	};
};

function elementFromText(text) {
	return {
		text: text,
		references: [],
		tags: {},
		children: []
	};
}

var renderElement = exports.renderElement = function renderElement(_ref2) {
	var ingredients = _ref2.ingredients,
	    elementRendererForTags = _ref2.elementRendererForTags;

	var resolveContent = resolveContentUsing(ingredients);

	var extra = {
		elementFromText: elementFromText
	};

	var Element = _ramda2.default.converge(_ramda2.default.call, [_ramda2.default.pipe(_ramda2.default.props(['defaultTags', 'tags']), _ramda2.default.apply(_ramda2.default.mergeWith(_ramda2.default.merge)), elementRendererForTags), _ramda2.default.prop('mentions'), _ramda2.default.prop('texts'), _ramda2.default.prop('children'), function (ignore) {
		return Element;
	}, // Have to put in closure as it is currently being assigned
	_ramda2.default.always(resolveContent), _ramda2.default.always(extra)]);

	Element.renderArray = function (options, array) {
		return array.map(function (element, index) {
			return _react2.default.createElement(Element, _extends({ key: index
			}, options, element));
		});
	};

	return Element;
};

var DefaultSection = exports.DefaultSection = function DefaultSection(_ref3) {
	var children = _ref3.children;
	return _react2.default.createElement(_reactSeeds.Seed, { Component: 'section'
		//column
		, margin: { base: '2rem', top: 0 },
		children: children
	});
};

var DefaultMaster = exports.DefaultMaster = function DefaultMaster(_ref4) {
	var children = _ref4.children;
	return _react2.default.createElement(_reactSeeds.Seed, {
		padding: { top: '1rem' },
		children: children
	});
};

var renderTreeUsing = exports.renderTreeUsing = function renderTreeUsing(_ref5) {
	var elementRendererForTags = _ref5.elementRendererForTags,
	    _ref5$Section = _ref5.Section,
	    Section = _ref5$Section === undefined ? DefaultSection : _ref5$Section,
	    _ref5$Master = _ref5.Master,
	    Master = _ref5$Master === undefined ? DefaultMaster : _ref5$Master;
	return function (_ref6) {
		var ingredients = _ref6.ingredients,
		    contentTree = _ref6.contentTree;

		// FIXME: use valueForIngredient instead that encapsulates ingredientVariationIndexes 
		var Element = renderElement({ ingredients: ingredients, elementRendererForTags: elementRendererForTags });

		return _react2.default.createElement(
			Master,
			{ ingredients: ingredients },
			contentTree.map(function (section, sectionIndex) {
				return _react2.default.createElement(
					Section,
					{ key: sectionIndex },
					section.map(function (element, elementIndex) {
						return _react2.default.createElement(Element, _extends({ key: elementIndex }, element));
					})
				);
			})
		);
	};
};