"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

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

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

var _withConfig = _interopRequireDefault(require("../../enhancers/withConfig"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _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; }; return _extends.apply(this, arguments); }

const {
  OptionPickerInput: BaseOptionPickerInput
} = _cspaceInput.components;
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 = { // eslint-disable-next-line react/forbid-foreign-prop-types
  ...BaseOptionPickerInput.propTypes,
  config: _propTypes.default.shape({
    defaultDropdownFilter: _propTypes.default.string
  }),
  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 {
      labelFormatter,
      message,
      value
    } = option;
    const {
      intl
    } = this.props;

    if (labelFormatter) {
      return labelFormatter(intl, option);
    }

    if (message) {
      return intl.formatMessage(message);
    }

    return value;
  }

  formatStatusMessage(count) {
    const {
      intl
    } = this.props;
    return intl.formatMessage(messages.count, {
      count
    });
  }

  render() {
    const {
      config,
      intl,
      ...remainingProps
    } = this.props;
    return /*#__PURE__*/_react.default.createElement(BaseOptionPickerInput, _extends({
      filter: config.defaultDropdownFilter,
      formatOptionLabel: this.formatOptionLabel,
      formatStatusMessage: this.formatStatusMessage
    }, remainingProps));
  }

}

OptionPickerInput.propTypes = propTypes;
const IntlAwareOptionPickerInput = (0, _reactIntl.injectIntl)((0, _withConfig.default)(OptionPickerInput));
IntlAwareOptionPickerInput.propTypes = OptionPickerInput.propTypes;
var _default = IntlAwareOptionPickerInput;
exports.default = _default;