'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _reactIntl = require('react-intl');

var _cspaceInput = require('cspace-input');

var _RecordButtonBarContainer = require('../../containers/record/RecordButtonBarContainer');

var _RecordButtonBarContainer2 = _interopRequireDefault(_RecordButtonBarContainer);

var _RecordHistory = require('../../components/record/RecordHistory');

var _RecordHistory2 = _interopRequireDefault(_RecordHistory);

var _recordDataHelpers = require('../../helpers/recordDataHelpers');

var _RecordEditor = require('../../../styles/cspace-ui/RecordEditor.css');

var _RecordEditor2 = _interopRequireDefault(_RecordEditor);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Label = _cspaceInput.components.Label;


function getLabel(component, messages) {
  var _component$props = component.props,
      msgkey = _component$props.msgkey,
      name = _component$props.name;


  var key = msgkey || name;
  var message = messages.field[key];

  if (!message || message.hidden) {
    return null;
  }

  return _react2.default.createElement(
    Label,
    null,
    _react2.default.createElement(_reactIntl.FormattedMessage, message)
  );
}

function renderTemplate(component, messages, handlers) {
  var overrideProps = {};
  var type = component.type;

  if (type) {
    var _propTypes = type.propTypes;

    if (_propTypes) {
      if (_propTypes.name) {
        if (_propTypes.label && typeof component.props.label === 'undefined') {
          overrideProps.label = getLabel(component, messages);
        }
      }

      Object.keys(handlers).forEach(function (handlerName) {
        if (_propTypes[handlerName] && !component.props[handlerName]) {
          overrideProps[handlerName] = handlers[handlerName];
        }
      });
    }
  }

  return _react2.default.cloneElement(component, overrideProps, _react2.default.Children.map(component.props.children, function (child) {
    return renderTemplate(child, messages, handlers);
  }));
}

var propTypes = {
  config: _react.PropTypes.object,
  recordType: _react.PropTypes.string.isRequired,
  vocabulary: _react.PropTypes.string,
  csid: _react.PropTypes.string,
  data: _react.PropTypes.instanceOf(_immutable2.default.Map),
  isModified: _react.PropTypes.bool,
  isSavePending: _react.PropTypes.bool,
  onAddInstance: _react.PropTypes.func,
  onCommit: _react.PropTypes.func,
  onMoveInstance: _react.PropTypes.func,
  onRemoveInstance: _react.PropTypes.func
};

var defaultProps = {
  data: _immutable2.default.Map()
};

var childContextTypes = {
  config: _react.PropTypes.object,
  recordType: _react.PropTypes.string,
  csid: _react.PropTypes.string
};

var RecordEditor = function (_Component) {
  _inherits(RecordEditor, _Component);

  function RecordEditor() {
    _classCallCheck(this, RecordEditor);

    return _possibleConstructorReturn(this, (RecordEditor.__proto__ || Object.getPrototypeOf(RecordEditor)).apply(this, arguments));
  }

  _createClass(RecordEditor, [{
    key: 'getChildContext',
    value: function getChildContext() {
      var _props = this.props,
          config = _props.config,
          csid = _props.csid,
          recordType = _props.recordType;


      return {
        config: config,
        csid: csid,
        recordType: recordType
      };
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          config = _props2.config,
          csid = _props2.csid,
          data = _props2.data,
          isModified = _props2.isModified,
          isSavePending = _props2.isSavePending,
          recordType = _props2.recordType,
          vocabulary = _props2.vocabulary,
          onAddInstance = _props2.onAddInstance,
          onCommit = _props2.onCommit,
          onMoveInstance = _props2.onMoveInstance,
          onRemoveInstance = _props2.onRemoveInstance;


      var recordTypeConfig = config.recordTypes[recordType];

      if (!recordTypeConfig) {
        return null;
      }

      var vocabularyConfig = vocabulary ? recordTypeConfig.vocabularies[vocabulary] : undefined;

      var forms = recordTypeConfig.forms,
          messages = recordTypeConfig.messages;


      var handlers = {
        onAddInstance: onAddInstance,
        onCommit: onCommit,
        onMoveInstance: onMoveInstance,
        onRemoveInstance: onRemoveInstance
      };

      var formTemplate = forms.default;

      var formContent = _react2.default.cloneElement(formTemplate, {
        name: _recordDataHelpers.DOCUMENT_PROPERTY_NAME,
        value: data.get(_recordDataHelpers.DOCUMENT_PROPERTY_NAME),
        children: _react2.default.Children.map(formTemplate.props.children, function (child) {
          return renderTemplate(child, messages, handlers);
        })
      });

      return _react2.default.createElement(
        'form',
        {
          autoComplete: 'off',
          className: _RecordEditor2.default.common
        },
        _react2.default.createElement(
          'header',
          null,
          _react2.default.createElement(_RecordButtonBarContainer2.default, {
            csid: csid,
            isModified: isModified,
            isSavePending: isSavePending,
            recordTypeConfig: recordTypeConfig,
            vocabularyConfig: vocabularyConfig
          }),
          _react2.default.createElement(_RecordHistory2.default, {
            data: data,
            isModified: isModified,
            isSavePending: isSavePending
          })
        ),
        formContent
      );
    }
  }]);

  return RecordEditor;
}(_react.Component);

exports.default = RecordEditor;


RecordEditor.propTypes = propTypes;
RecordEditor.defaultProps = defaultProps;
RecordEditor.childContextTypes = childContextTypes;