'use strict';

exports.__esModule = true;

var _ramda = require('ramda');

var _ramda2 = _interopRequireDefault(_ramda);

var _isObject = require('lodash/isObject');

var _isObject2 = _interopRequireDefault(_isObject);

var _isNumber = require('lodash/isNumber');

var _isNumber2 = _interopRequireDefault(_isNumber);

var _isBoolean = require('lodash/isBoolean');

var _isBoolean2 = _interopRequireDefault(_isBoolean);

var _react = require('react');

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

var li = _ramda2.default.partial(_react.createElement, ['li', null]);
var dl = _ramda2.default.partial(_react.createElement, ['dl', null]);
var dtAttrs = _ramda2.default.partial(_react.createElement, ['dt']);
var ddAttrs = _ramda2.default.partial(_react.createElement, ['dd']);
var ol = _ramda2.default.partial(_react.createElement, ['ol', null]);
var code = _ramda2.default.partial(_react.createElement, ['code', null]);
var em = _ramda2.default.partial(_react.createElement, ['em', null]);
var p = _ramda2.default.partial(_react.createElement, ['p', null]);

var renderObjectItem = function renderObjectItem(key, value) {
	return [dtAttrs({ key: 'key-' + key }, key), ddAttrs({ key: 'value-' + key }, renderJSON(value))];
};

var renderArrayItem = function renderArrayItem(value) {
	return li(renderJSON(value));
};

var renderObject = _ramda2.default.pipe(_ramda2.default.toPairs,
//R.sortBy(R.prop(0)),
_ramda2.default.map(_ramda2.default.apply(renderObjectItem)), _ramda2.default.tap(function (items) {
	return console.log('rendered', items);
}), _ramda2.default.flatten, dl);

var renderArray = _ramda2.default.pipe(_ramda2.default.map(renderArrayItem), ol);

var renderNumber = _ramda2.default.pipe(code, p);

var renderBoolean = _ramda2.default.pipe(function (boolean) {
	return boolean ? 'yes' : 'no';
}, em, p);

var renderString = p;

var renderJSON = _ramda2.default.cond([[Array.isArray, renderArray], [_isObject2.default, renderObject], [_isNumber2.default, renderNumber], [_isBoolean2.default, renderBoolean], [_ramda2.default.T, renderString]]);

var renderJSONError = function renderJSONError(error) {
	return (0, _react.createElement)('h2', null, 'JSON is invalid ' + error);
};

var renderSerializedJSON = _ramda2.default.tryCatch(_ramda2.default.pipe(JSON.parse, renderJSON), renderJSONError);

var JSONComponent = _ramda2.default.converge(_ramda2.default.call, [_ramda2.default.ifElse(_ramda2.default.prop('isDeserialized'), _ramda2.default.always(renderJSON), _ramda2.default.always(renderSerializedJSON)), _ramda2.default.prop('json')]);

exports.default = JSONComponent;
module.exports = exports['default'];