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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _normalizeLabel = require('../helpers/normalizeLabel');

var _normalizeLabel2 = _interopRequireDefault(_normalizeLabel);

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

/**
 * Makes an input component labelable. Returns an enhanced component that accepts a label prop. If
 * a label is supplied, the base component is wrapped, and rendered in the wrapper along with the
 * label. If the label is a string, it is wrapped in a Label; otherwise, it is rendered as given.
 * If no label is supplied, the base component is returned unchanged. A msgkey prop is also
 * accepted, but has no effect. It may be used by preprocessors to generate a label.
 * @param {string|function} BaseComponent - The component to enhance.
 * @returns {function} The enhanced component.
 */
function labelable(BaseComponent) {
  var baseComponentName = BaseComponent.displayName || BaseComponent.name || 'Component';

  var propTypes = _extends({}, BaseComponent.propTypes, {
    label: _react.PropTypes.node,
    msgkey: _react.PropTypes.string
  });

  function Labelable(props) {
    var label = props.label,
        msgkey = props.msgkey,
        remainingProps = _objectWithoutProperties(props, ['label', 'msgkey']);

    var normalizedLabel = (0, _normalizeLabel2.default)(label);

    var baseComponent = _react2.default.createElement(BaseComponent, remainingProps);

    if (!normalizedLabel) {
      return baseComponent;
    }

    return _react2.default.createElement(
      'div',
      null,
      normalizedLabel,
      baseComponent
    );
  }

  Labelable.propTypes = propTypes;
  Labelable.displayName = 'labelable(' + baseComponentName + ')';

  return Labelable;
}