'use strict';

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

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

var _react = require('react');

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

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

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

var _warning = require('warning');

var _warning2 = _interopRequireDefault(_warning);

var _vocabulary = require('../../actions/vocabulary');

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

var VocabularyControlledInput = _cspaceInput.components.VocabularyControlledInput;


var messages = (0, _reactIntl.defineMessages)({
  count: {
    'id': 'vocabularyControlledInput.count',
    'defaultMessage': '{count, plural, =0 {No matching terms} one {# matching term} other {# matching terms}} found'
  }
});

var mapStateToProps = function mapStateToProps(state, ownProps) {
  var intl = ownProps.intl,
      vocabularyName = ownProps.vocabularyName;


  var vocabulary = (0, _reducers.getVocabulary)(state, vocabularyName);

  return {
    formatStatusMessage: function formatStatusMessage(count) {
      return intl.formatMessage(messages.count, { count: count });
    },
    // isLoading: vocabulary ? vocabulary.isReadPending : true,
    terms: vocabulary ? vocabulary.items : null
  };
};

var mapDispatchToProps = function mapDispatchToProps(dispatch, ownProps) {
  var name = ownProps.name,
      vocabularyName = ownProps.vocabularyName;


  process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(vocabularyName, 'The vocabulary controlled input with name \'' + name + '\' is not associated with a vocabulary. Set the \'vocabularyName\' prop.') : void 0;

  if (vocabularyName) {
    return {
      onMount: function onMount() {
        dispatch((0, _vocabulary.readVocabularyItems)(vocabularyName));
      }
    };
  }

  return {};
};

var mergeProps = function mergeProps(stateProps, dispatchProps, ownProps) {
  var intl = ownProps.intl,
      onMount = ownProps.onMount,
      vocabularyName = ownProps.vocabularyName,
      remainingOwnProps = _objectWithoutProperties(ownProps, ['intl', 'onMount', 'vocabularyName']);

  return _extends({}, remainingOwnProps, stateProps, dispatchProps);
};

var ConnectedVocabularyControlledInput = exports.ConnectedVocabularyControlledInput = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps)(VocabularyControlledInput);

var IntlizedConnectedVocabularyControlledInput = (0, _reactIntl.injectIntl)(ConnectedVocabularyControlledInput);

IntlizedConnectedVocabularyControlledInput.propTypes = _extends({}, VocabularyControlledInput.propTypes, {
  vocabularyName: _react.PropTypes.string.isRequired
});

exports.default = IntlizedConnectedVocabularyControlledInput;