"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 _immutable = _interopRequireDefault(require("immutable"));
var _warning = _interopRequireDefault(require("warning"));
var _cspaceInput = require("cspace-input");
var _withConfig = _interopRequireDefault(require("../../enhancers/withConfig"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 ? Object.assign.bind() : 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 {
  labelable,
  repeatable
} = _cspaceInput.enhancers;
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 = {
  // eslint-disable-next-line react/forbid-foreign-prop-types
  ...BaseTermPickerInput.propTypes,
  config: _propTypes.default.shape({
    defaultDropdownFilter: _propTypes.default.string
  }),
  intl: _reactIntl.intlShape,
  name: _propTypes.default.string,
  perms: _propTypes.default.instanceOf(_immutable.default.Map),
  readTerms: _propTypes.default.func,
  source: _propTypes.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() {
    const {
      name,
      source,
      readTerms
    } = this.props;
    process.env.NODE_ENV !== "production" ? (0, _warning.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) {
    const {
      perms,
      source,
      readTerms
    } = this.props;
    const {
      perms: prevPerms
    } = prevProps;
    if (readTerms && perms !== prevPerms) {
      readTerms(source);
    }
  }
  formatStatusMessage(count) {
    const {
      intl
    } = this.props;
    return intl.formatMessage(messages.count, {
      count
    });
  }
  render() {
    const {
      config,
      intl,
      perms,
      readTerms,
      source,
      ...remainingProps
    } = this.props;
    return /*#__PURE__*/_react.default.createElement(BaseTermPickerInput, _extends({
      filter: config.defaultDropdownFilter,
      formatStatusMessage: this.formatStatusMessage
    }, remainingProps));
  }
}
TermPickerInput.propTypes = propTypes;
const IntlAwareTermPickerInput = (0, _reactIntl.injectIntl)((0, _withConfig.default)(TermPickerInput));
IntlAwareTermPickerInput.propTypes = TermPickerInput.propTypes;
var _default = IntlAwareTermPickerInput;
exports.default = _default;