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

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

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

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

  var defaultProps = {
    formatOptionLabel: function formatOptionLabel(option) {
      return typeof option.label === 'undefined' ? option.value : option.label;
    },
    options: []
  };

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

    var labeledOptions = options.map(function (option) {
      return Object.assign({}, option, {
        label: formatOptionLabel(option)
      });
    });

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

  WithLabeledOptions.propTypes = propTypes;
  WithLabeledOptions.defaultProps = defaultProps;
  WithLabeledOptions.displayName = 'withLabeledOptions(' + baseComponentName + ')';

  return WithLabeledOptions;
}