'use strict';

exports.__esModule = true;

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

exports.default = RecordEditor;

var _ramda = require('ramda');

var _ramda2 = _interopRequireDefault(_ramda);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactSeeds = require('react-seeds');

var _Choice = require('../ui/Choice');

var _Choice2 = _interopRequireDefault(_Choice);

var _Field = require('../ui/Field');

var _Field2 = _interopRequireDefault(_Field);

var _stylers = require('../stylers');

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var types = [{ value: 'string', title: 'Text' }, { value: 'boolean', title: 'Yes / No' }, { value: 'number', title: 'Number' }, { value: 'array', title: 'List' }, { value: 'object', title: 'Record' }];

var valueEditorForType = _ramda2.default.cond([[_ramda2.default.equals('string'), _ramda2.default.always(function (string) {
    return _react2.default.createElement(_Field2.default, _extends({ value: string }, _stylers.recordField));
})], [_ramda2.default.equals('boolean'), _ramda2.default.always(function (boolean) {
    return _react2.default.createElement(_Choice2.default, { value: boolean, items: [{ value: true, title: 'Yes' }, { value: false, title: ' No' }] });
})], [_ramda2.default.equals('number'), _ramda2.default.always(function (number) {
    return _react2.default.createElement('input', { type: 'number', value: number });
})], [_ramda2.default.equals('array'), function () {
    return function (items) {
        return _react2.default.createElement(
            'div',
            null,
            items.map(function (item, index) {
                return _react2.default.createElement(Item, { key: index, id: index, value: item });
            })
        );
    };
}], [_ramda2.default.equals('object'), function () {
    return function (object) {
        return _react2.default.createElement(RecordEditor, { object: object });
    };
}]]);

var valueEditorForValue = _ramda2.default.converge(_ramda2.default.call, [_ramda2.default.pipe(_ramda2.default.type, _ramda2.default.toLower, valueEditorForType), _ramda2.default.identity]);

function Item(_ref) {
    var id = _ref.id,
        value = _ref.value;

    return _react2.default.createElement(
        _reactSeeds.Seed,
        { column: true },
        _react2.default.createElement(
            _reactSeeds.Seed,
            { row: true, grow: 1 },
            _react2.default.createElement('input', { value: id, style: { fontWeight: 'bold' } }),
            _react2.default.createElement(_Choice2.default, { value: _ramda2.default.type(value).toLowerCase(), items: types })
        ),
        valueEditorForValue(value)
    );
}

function RecordEditor(_ref2) {
    var object = _ref2.object,
        seedProps = _objectWithoutProperties(_ref2, ['object']);

    return _react2.default.createElement(
        _reactSeeds.Seed,
        _extends({ column: true }, seedProps),
        Object.keys(object).map(function (key) {
            return _react2.default.createElement(Item, { key: key,
                id: key,
                value: object[key]
            });
        })
    );
}
module.exports = exports['default'];