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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _optionHelpers = require('../helpers/optionHelpers');

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

/**
 * Normalizes options before passing them on to the base component.
 * @param {string|function} BaseComponent - The component to enhance.
 * @returns {function} The enhanced component.
 */
function withNormalizedOptions(BaseComponent) {
  var baseComponentName = BaseComponent.displayName || BaseComponent.name || 'Component';

  var propTypes = _extends({}, BaseComponent.propTypes, {
    blankable: _react.PropTypes.bool,
    options: _react.PropTypes.arrayOf(_react.PropTypes.shape({
      value: _react.PropTypes.string
    }))
  });

  var defaultProps = {
    blankable: true
  };

  function WithNormalizedOptions(props) {
    var blankable = props.blankable,
        options = props.options,
        remainingProps = _objectWithoutProperties(props, ['blankable', 'options']);

    var normalizedOptions = (0, _optionHelpers.normalizeOptions)(options, blankable);

    return _react2.default.createElement(BaseComponent, _extends({}, remainingProps, {
      options: normalizedOptions
    }));
  }

  WithNormalizedOptions.propTypes = propTypes;
  WithNormalizedOptions.defaultProps = defaultProps;
  WithNormalizedOptions.displayName = 'withNormalizedOptions(' + baseComponentName + ')';

  return WithNormalizedOptions;
}