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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _warning = require('warning');

var _warning2 = _interopRequireDefault(_warning);

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

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var labelable = _cspaceInput.enhancers.labelable,
    repeatable = _cspaceInput.enhancers.repeatable;


var BaseTermPickerInput = repeatable(labelable(_cspaceInput.baseComponents.TermPickerInput));

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

var propTypes = Object.assign({}, BaseTermPickerInput.propTypes, {
  intl: _reactIntl.intlShape,
  name: _propTypes2.default.string,
  perms: _propTypes2.default.instanceOf(_immutable2.default.Map),
  readTerms: _propTypes2.default.func,
  source: _propTypes2.default.string
});

/**
 * A wrapper around TermPickerInput from cspace-input that implements some lifecycle methods
 * and supplies i18n.
 */

var TermPickerInput = function (_Component) {
  _inherits(TermPickerInput, _Component);

  function TermPickerInput() {
    _classCallCheck(this, TermPickerInput);

    var _this = _possibleConstructorReturn(this, (TermPickerInput.__proto__ || Object.getPrototypeOf(TermPickerInput)).call(this));

    _this.formatStatusMessage = _this.formatStatusMessage.bind(_this);
    return _this;
  }

  _createClass(TermPickerInput, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _props = this.props,
          name = _props.name,
          source = _props.source,
          readTerms = _props.readTerms;


      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 && readTerms) {
        readTerms(source);
      }
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      var _props2 = this.props,
          perms = _props2.perms,
          source = _props2.source,
          readTerms = _props2.readTerms;
      var prevPerms = prevProps.perms;


      if (readTerms && perms !== prevPerms) {
        readTerms(source);
      }
    }
  }, {
    key: 'formatStatusMessage',
    value: function formatStatusMessage(count) {
      var intl = this.props.intl;


      return intl.formatMessage(messages.count, { count: count });
    }
  }, {
    key: 'render',
    value: function render() {
      var _props3 = this.props,
          intl = _props3.intl,
          perms = _props3.perms,
          readTerms = _props3.readTerms,
          source = _props3.source,
          remainingProps = _objectWithoutProperties(_props3, ['intl', 'perms', 'readTerms', 'source']);

      return _react2.default.createElement(BaseTermPickerInput, _extends({
        formatStatusMessage: this.formatStatusMessage
      }, remainingProps));
    }
  }]);

  return TermPickerInput;
}(_react.Component);

TermPickerInput.propTypes = propTypes;

var IntlAwareTermPickerInput = (0, _reactIntl.injectIntl)(TermPickerInput);

IntlAwareTermPickerInput.propTypes = TermPickerInput.propTypes;

exports.default = IntlAwareTermPickerInput;