"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 _configHelpers = require("../../helpers/configHelpers");

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 {
  ChooserInput
} = _cspaceInput.components;
const itemLimit = 10;
const messages = (0, _reactIntl.defineMessages)({
  value: {
    "id": "recordSearchInput.value",
    "defaultMessage": "{count, plural, =0 {} one {} other {# records: }}{items}{remainingCount, plural, =0 {} other {, and # more}}"
  }
});
const propTypes = {
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  openSearchModal: _propTypes.default.func
};

class RecordSearchInput extends _react.Component {
  constructor(props) {
    super(props);
    this.formatValue = this.formatValue.bind(this);
    this.handleChooseButtonClick = this.handleChooseButtonClick.bind(this);
  }

  formatValue(value) {
    const {
      config
    } = this.props;

    if (value && value.size > 0) {
      const descriptions = value.valueSeq().take(itemLimit).map(item => {
        const recordType = (0, _configHelpers.getRecordTypeNameByUri)(config, item.get('uri'));
        const firstColumnName = (0, _configHelpers.getFirstColumnName)(config, recordType);
        return item.get(firstColumnName) || item.get('csid') || '?';
      }).toJS();
      return /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.value, {
        values: {
          items: descriptions.join(', '),
          count: value.size,
          remainingCount: Math.max(value.size - itemLimit, 0)
        }
      }));
    }

    return value;
  }

  handleChooseButtonClick() {
    const {
      openSearchModal
    } = this.props;

    if (openSearchModal) {
      openSearchModal();
    }
  }

  render() {
    return /*#__PURE__*/_react.default.createElement(ChooserInput, _extends({
      formatValue: this.formatValue,
      onChooseButtonClick: this.handleChooseButtonClick
    }, this.props));
  }

}

exports.default = RecordSearchInput;
RecordSearchInput.propTypes = propTypes;