'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 _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');

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

var _withConfig2 = _interopRequireDefault(_withConfig);

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 labelable = _cspaceInput.enhancers.labelable,
      repeatable = _cspaceInput.enhancers.repeatable;


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

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

const propTypes = Object.assign({}, BaseTermPickerInput.propTypes, {
  config: _propTypes2.default.object,
  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,
 * applies default props from config, and supplies i18n.
 */
class TermPickerInput extends _react.Component {
  constructor() {
    super();

    this.formatStatusMessage = this.formatStatusMessage.bind(this);
  }

  componentDidMount() {
    var _props = this.props;
    const 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);
    }
  }

  componentDidUpdate(prevProps) {
    var _props2 = this.props;
    const perms = _props2.perms,
          source = _props2.source,
          readTerms = _props2.readTerms;
    const prevPerms = prevProps.perms;


    if (readTerms && perms !== prevPerms) {
      readTerms(source);
    }
  }

  formatStatusMessage(count) {
    const intl = this.props.intl;


    return intl.formatMessage(messages.count, { count });
  }

  render() {
    var _props3 = this.props;

    const config = _props3.config,
          intl = _props3.intl,
          perms = _props3.perms,
          readTerms = _props3.readTerms,
          source = _props3.source,
          remainingProps = _objectWithoutProperties(_props3, ['config', 'intl', 'perms', 'readTerms', 'source']);

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

TermPickerInput.propTypes = propTypes;

const IntlAwareTermPickerInput = (0, _reactIntl.injectIntl)((0, _withConfig2.default)(TermPickerInput));

IntlAwareTermPickerInput.propTypes = TermPickerInput.propTypes;

exports.default = IntlAwareTermPickerInput;