'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 = nestable;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

/**
 * Enhances an input component so that it may be nested within another input component.
 * Returns an enhanced component that accepts a subpath prop, and defaultSubpath and parentPath
 * from context.
 * @param {string|function} BaseComponent - The component to enhance.
 * @returns {function} The enhanced component.
 */
function nestable(BaseComponent) {
  var baseComponentName = BaseComponent.displayName || BaseComponent.name || 'Component';

  var propTypes = _extends({}, BaseComponent.propTypes, {
    subpath: _react.PropTypes.oneOfType([_react.PropTypes.arrayOf(_react.PropTypes.string), _react.PropTypes.string])
  });

  function Nestable(props) {
    var subpath = props.subpath,
        remainingProps = _objectWithoutProperties(props, ['subpath']);

    return _react2.default.createElement(BaseComponent, remainingProps);
  }

  Nestable.propTypes = propTypes;
  Nestable.displayName = 'nestable(' + baseComponentName + ')';

  return Nestable;
}