"use strict";

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

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

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

var _reactRouterDom = require("react-router-dom");

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

var _immutable = _interopRequireDefault(require("immutable"));

var _cspaceRefname = require("cspace-refname");

var _configHelpers = require("../../helpers/configHelpers");

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

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 propTypes = {
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  item: _propTypes.default.instanceOf(_immutable.default.Map),
  message: _propTypes.default.shape({
    id: _propTypes.default.string,
    defaultMessage: _propTypes.default.string
  }),
  rel: _propTypes.default.string,
  // eslint-disable-next-line react/forbid-prop-types
  state: _propTypes.default.object
};

function SearchResultItemLink(props) {
  const {
    config,
    item,
    message,
    rel,
    state
  } = props; // TODO: Get meta-number field from config. The docNumber list result field only appears in
  // searches for multiple record types.

  const recordNumber = item.get('docNumber');
  const csid = item.get('csid') || item.get('docId');
  const refName = item.get('refName');
  const servicePath = (0, _cspaceRefname.getServicePath)(refName);
  const recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServicePath)(config, servicePath);
  const pathParts = ['/record', recordTypeConfig.name];

  if (recordTypeConfig.serviceConfig.serviceType === 'authority') {
    const vocabularyShortID = (0, _cspaceRefname.getVocabularyShortID)(refName);
    const vocabularyConfig = (0, _configHelpers.getVocabularyConfigByShortID)(recordTypeConfig, vocabularyShortID);

    if (vocabularyConfig) {
      pathParts.push(vocabularyConfig.name);
    }
  }

  pathParts.push(csid);
  const location = {
    state,
    pathname: pathParts.join('/')
  };
  return /*#__PURE__*/_react.default.createElement(_reactRouterDom.Link, {
    rel: rel,
    to: location
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, message, {
    values: {
      recordNumber
    }
  })));
}

SearchResultItemLink.propTypes = propTypes;