'use strict';

exports.__esModule = true;
exports.parseInput = undefined;

var _ramda = require('ramda');

var _ramda2 = _interopRequireDefault(_ramda);

var _parseInt = require('lodash/parseInt');

var _parseInt2 = _interopRequireDefault(_parseInt);

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

var _require = require('lofi'),
    parseElement = _require.parseElement;

var rejectEmptyStrings = require('./rejectEmptyStrings');

var addChildTo = _ramda2.default.curry(function (element, parent) {
	return _ramda2.default.mergeWith(_ramda2.default.concat, parent, {
		children: [element]
	});
});

var prefixAndBodyRegex = /^([0-9]?[-0-9.]*[-.])?\s*(.*)$/;
var orderedPrefixRegex = /^[0-9]/;

var parseInput = exports.parseInput = _ramda2.default.pipe(_ramda2.default.split('\n\n'), // sections
_ramda2.default.map(_ramda2.default.pipe(_ramda2.default.split('\n'), // elements
rejectEmptyStrings, // must have content
_ramda2.default.reduce(function (items, itemInput) {
	var _R$tail = _ramda2.default.tail(_ramda2.default.match(prefixAndBodyRegex, itemInput)),
	    prefix = _R$tail[0],
	    body = _R$tail[1]; // just get captured


	if (body == null) {
		return items;
	}

	var element = parseElement(body);

	// Skip #research
	if (element.tags.research) {
		return items;
	}

	var isChild = prefix != null;
	if (isChild) {
		if (items.length === 0) {
			var ordered = _ramda2.default.test(orderedPrefixRegex, prefix);
			return [{
				text: null,
				tags: { list: true, ordered: ordered },
				references: [],
				children: [element]
			}];
		} else {
			return _ramda2.default.adjust(addChildTo(element), -1, items);
		}
	} else {
		return _ramda2.default.concat(items, element);
	}
}, []))), _ramda2.default.reject(_ramda2.default.isEmpty));