'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ConnectedTermPickerInput = 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 _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 TermPickerInput = _cspaceInput.components.TermPickerInput;


var messages = (0, _reactIntl.defineMessages)({
  count: {
    'id': 'termPickerInputContainer.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,
      source = ownProps.source;


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

  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,
      source = ownProps.source;


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

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

  return {};
};

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

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

var ConnectedTermPickerInput = exports.ConnectedTermPickerInput = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps)(TermPickerInput);

var IntlizedConnectedTermPickerInput = (0, _reactIntl.injectIntl)(ConnectedTermPickerInput);

IntlizedConnectedTermPickerInput.propTypes = Object.assign({}, TermPickerInput.propTypes, {
  source: _propTypes2.default.string.isRequired
});

exports.default = IntlizedConnectedTermPickerInput;