'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; };

exports.default = SearchResultItemLink;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

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

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

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

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

const propTypes = {
  config: _propTypes2.default.object,
  item: _propTypes2.default.instanceOf(_immutable2.default.Map),
  message: _propTypes2.default.object,
  rel: _propTypes2.default.string,
  state: _propTypes2.default.object
};

function SearchResultItemLink(props) {
  const config = props.config,
        item = props.item,
        message = props.message,
        rel = props.rel,
        state = props.state;

  // 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 _react2.default.createElement(
    _reactRouterDom.Link,
    { rel: rel, to: location },
    _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, message, { values: { recordNumber } }))
  );
}

SearchResultItemLink.propTypes = propTypes;