'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 _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 BaseOptionPickerInput = _cspaceInput.components.OptionPickerInput;


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

const propTypes = Object.assign({}, BaseOptionPickerInput.propTypes, {
  config: _propTypes2.default.object,
  intl: _reactIntl.intlShape
});

/**
 * A wrapper around OptionPickerInput from cspace-input that applies default props from config, and
 * supplies i18n.
 */
class OptionPickerInput extends _react.Component {
  constructor() {
    super();

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

  formatOptionLabel(option) {
    const message = option.message,
          value = option.value;
    const intl = this.props.intl;


    return message ? intl.formatMessage(message) : value;
  }

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


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

  render() {
    var _props = this.props;

    const config = _props.config,
          intl = _props.intl,
          remainingProps = _objectWithoutProperties(_props, ['config', 'intl']);

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

OptionPickerInput.propTypes = propTypes;

const IntlAwareOptionPickerInput = (0, _reactIntl.injectIntl)((0, _withConfig2.default)(OptionPickerInput));

IntlAwareOptionPickerInput.propTypes = OptionPickerInput.propTypes;

exports.default = IntlAwareOptionPickerInput;