'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ConnectedOptionPickerInput = undefined;

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactRedux = require('react-redux');

var _reactIntl = require('react-intl');

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

var _reducers = require('../../reducers');

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

const OptionPickerInput = _cspaceInput.components.OptionPickerInput;


const messages = (0, _reactIntl.defineMessages)({
  count: {
    'id': 'optionPickerInput.count',
    'defaultMessage': '{count, plural, =0 {No matching options} one {# matching option} other {# matching options}} found'
  }
});

const mapStateToProps = (state, ownProps) => {
  const intl = ownProps.intl,
        optionListName = ownProps.source;


  const options = (0, _reducers.getOptionList)(state, optionListName);

  return {
    options,
    formatOptionLabel: option => option.message ? intl.formatMessage(option.message) : option.value,
    formatStatusMessage: count => intl.formatMessage(messages.count, { count })
  };
};

const mapDispatchToProps = {};

const mergeProps = (stateProps, dispatchProps, ownProps) => {
  const intl = ownProps.intl,
        source = ownProps.source,
        remainingOwnProps = _objectWithoutProperties(ownProps, ['intl', 'source']);

  return Object.assign({}, remainingOwnProps, stateProps, dispatchProps);
};

const ConnectedOptionPickerInput = exports.ConnectedOptionPickerInput = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps)(OptionPickerInput);

const IntlizedConnectedOptionPickerInput = (0, _reactIntl.injectIntl)(ConnectedOptionPickerInput);

IntlizedConnectedOptionPickerInput.propTypes = Object.assign({}, OptionPickerInput.propTypes, {
  source: _propTypes2.default.string
});

exports.default = IntlizedConnectedOptionPickerInput;