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; };

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

var resolveContentIn = R.curry(function resolveItemIn(source, path) {
	//path = R.filter(R.isNil)
	console.log('resolveContentIn', path, source);
	path = R.insertAll(1, ['content'], path);
	return R.path(path, source);
});

var variationForItemIn = R.curry(function adjustItemIn(source, path) {
	var id = R.head(path);
	return R.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 (R.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) {
				R.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 = R.tail(path);
				variation.adjustPath(innerPath, alter);
			} else {
				console.log('RESOLVE', value.mentions);
				var resolved = R.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: []
	};
}

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

	var resolveContent = resolveContentUsing(ingredients);

	var extra = {
		elementFromText: elementFromText
	};

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

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

	return Element;
};

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

export var DefaultMaster = function DefaultMaster(_ref4) {
	var children = _ref4.children;
	return React.createElement(Seed, {
		padding: { top: '1rem' },
		children: children
	});
};

export var 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 React.createElement(
			Master,
			{ ingredients: ingredients },
			contentTree.map(function (section, sectionIndex) {
				return React.createElement(
					Section,
					{ key: sectionIndex },
					section.map(function (element, elementIndex) {
						return React.createElement(Element, _extends({ key: elementIndex }, element));
					})
				);
			})
		);
	};
};