'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ConnectedAuthorityControlledInput = 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 _parseAuthoritySpec = require('../../helpers/parseAuthoritySpec');

var _parseAuthoritySpec2 = _interopRequireDefault(_parseAuthoritySpec);

var _partialTermSearch = require('../../actions/partialTermSearch');

var _withConfig = require('../../enhancers/withConfig');

var _withConfig2 = _interopRequireDefault(_withConfig);

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 AuthorityControlledInput = _cspaceInput.components.AuthorityControlledInput;


var messages = (0, _reactIntl.defineMessages)({
  count: {
    'id': 'authorityControlledInput.count',
    'defaultMessage': '{count, plural, =0 {No matching terms} one {# matching term} other {# matching terms}} found'
  },
  moreCharsRequired: {
    'id': 'authorityControlledInput.moreCharsRequired',
    'defaultMessage': 'Continue typing to find matching terms'
  }
});

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


  return {
    formatMoreCharsRequiredMessage: function formatMoreCharsRequiredMessage() {
      return intl.formatMessage(messages.moreCharsRequired);
    },
    formatSearchResultMessage: function formatSearchResultMessage(count) {
      return intl.formatMessage(messages.count, { count: count });
    },
    formatVocabName: function formatVocabName(vocab) {
      return intl.formatMessage(vocab.messages.collectionName);
    },
    matches: (0, _reducers.getPartialTermSearchMatches)(state),
    recordTypes: config.recordTypes
  };
};

var mapDispatchToProps = function mapDispatchToProps(dispatch, ownProps) {
  var authority = ownProps.authority,
      config = ownProps.config;


  var authorities = (0, _parseAuthoritySpec2.default)(authority);

  return {
    addTerm: function addTerm(authorityName, vocabularyName, displayName) {
      var authorityConfig = config.recordTypes[authorityName];

      process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(authorityConfig, 'The authority record type \'' + authorityName + '\' is not configured. Check the authority prop of the input with name \'' + ownProps.name + '\'.') : void 0;

      if (authorityConfig && authorityConfig.vocabularies && authorityConfig.vocabularies[vocabularyName]) {
        dispatch((0, _partialTermSearch.addTerm)(authorityConfig, vocabularyName, displayName));
      }
    },
    findMatchingTerms: function findMatchingTerms(partialTerm) {
      authorities.forEach(function (authoritySpec) {
        var authorityName = authoritySpec.authorityName,
            vocabularyName = authoritySpec.vocabularyName;


        var authorityConfig = config.recordTypes[authorityName];

        process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(authorityConfig, 'The authority record type \'' + authorityName + '\' is not configured. Check the authority prop of the input with name \'' + ownProps.name + '\'.') : void 0;

        if (authorityConfig && authorityConfig.vocabularies && authorityConfig.vocabularies[vocabularyName]) {
          dispatch((0, _partialTermSearch.findMatchingTerms)(authorityConfig, vocabularyName, partialTerm));
        }
      });
    },
    onClose: function onClose() {
      dispatch((0, _partialTermSearch.clearMatchedTerms)());
    }
  };
};

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

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

var ConnectedAuthorityControlledInput = exports.ConnectedAuthorityControlledInput = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps)(AuthorityControlledInput);

var IntlizedConnectedAuthorityControlledInput = (0, _reactIntl.injectIntl)((0, _withConfig2.default)(ConnectedAuthorityControlledInput));

IntlizedConnectedAuthorityControlledInput.propTypes = _extends({}, AuthorityControlledInput.propTypes, {
  authority: _react.PropTypes.string.isRequired,
  config: _react.PropTypes.object
});

exports.default = IntlizedConnectedAuthorityControlledInput;