'use strict';

Object.defineProperty(exports, "__esModule", {
  value: 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 = Field;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _get2 = _interopRequireDefault(_get);

var _warning = require('warning');

var _warning2 = _interopRequireDefault(_warning);

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

var _configHelpers = require('../../helpers/configHelpers');

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

var pathHelpers = _cspaceInput.helpers.pathHelpers;


var propTypes = {
  // Code in this component doesn't use these props, but the propTypes need to exist, because
  // users of this component may check for them to determine if those props should be passed.
  // We want to receive all the props that our base components may need, and then we'll handle
  // distributing them to the base components that accept them.

  /* eslint-disable react/no-unused-prop-types */
  name: _react.PropTypes.string,
  // The value prop will be validated by the base component, so allow anything here.
  value: _react.PropTypes.any,
  parentPath: _react.PropTypes.array,
  subpath: _react.PropTypes.string,
  label: _react.PropTypes.node,
  onAddInstance: _react.PropTypes.func,
  onCommit: _react.PropTypes.func,
  onMoveInstance: _react.PropTypes.func,
  onRemoveInstance: _react.PropTypes.func
};

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

function Field(props, context) {
  var config = context.config,
      recordType = context.recordType;

  // Filter out numeric parts of the path, since they indicate repeating instances that won't be
  // present in the field descriptor.

  var path = (0, _configHelpers.dataPathToFieldDescriptorPath)(pathHelpers.getPath(props));
  var fields = (0, _get2.default)(config, ['recordTypes', recordType, 'fields']);

  process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(fields, 'No field descriptor found for the record type ' + recordType + '. The field with path ' + path + ' will not be rendered.') : void 0;

  if (!fields) {
    return null;
  }

  var field = (0, _get2.default)(fields, path);

  process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(field, 'The path ' + path + ' is not present in the field descriptors for the record type ' + recordType + '. The field will not be rendered.') : void 0;

  if (!field) {
    return null;
  }

  var viewConfig = field[_configHelpers.configKey].view;
  var BaseComponent = viewConfig.type;
  var configuredProps = viewConfig.props;
  var providedProps = {};

  var basePropTypes = BaseComponent.propTypes;

  Object.keys(props).forEach(function (propName) {
    if (propName in basePropTypes) {
      providedProps[propName] = props[propName];
    }
  });

  return _react2.default.createElement(BaseComponent, _extends({}, configuredProps, providedProps));
}

Field.contextTypes = contextTypes;
Field.propTypes = propTypes;