'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _warning = require('warning');

var _warning2 = _interopRequireDefault(_warning);

var _SubrecordEditorContainer = require('../../containers/record/SubrecordEditorContainer');

var _SubrecordEditorContainer2 = _interopRequireDefault(_SubrecordEditorContainer);

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

var propTypes = {
  name: _propTypes2.default.string,
  showDetachButton: _propTypes2.default.bool,
  template: _propTypes2.default.string
};

var contextTypes = {
  config: _propTypes2.default.object,
  csid: _propTypes2.default.string,
  recordType: _propTypes2.default.string,
  readOnly: _propTypes2.default.bool
};

function Subrecord(props, context) {
  var config = context.config,
      csid = context.csid,
      recordType = context.recordType,
      readOnly = context.readOnly;
  var name = props.name,
      template = props.template,
      showDetachButton = props.showDetachButton;


  var subrecordConfig = (0, _get2.default)(config, ['recordTypes', recordType, 'subrecords', name]);

  process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(subrecordConfig, 'No subrecord is configured with name ' + name + ' for the record type ' + recordType + '.') : void 0;

  if (!subrecordConfig) {
    return null;
  }

  return _react2.default.createElement(_SubrecordEditorContainer2.default, {
    containerCsid: csid,
    name: name,
    config: config,
    subrecordConfig: subrecordConfig,
    formName: template,
    showDetachButton: showDetachButton,
    readOnly: readOnly
  });
}

Subrecord.propTypes = propTypes;
Subrecord.contextTypes = contextTypes;