'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 = SearchResultTitleBar;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _CsidLink = require('../navigation/CsidLink');

var _CsidLink2 = _interopRequireDefault(_CsidLink);

var _TitleBar = require('../sections/TitleBar');

var _TitleBar2 = _interopRequireDefault(_TitleBar);

var _AdvancedSearchBuilder = require('./AdvancedSearchBuilder');

var _AdvancedSearchBuilder2 = _interopRequireDefault(_AdvancedSearchBuilder);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

const messages = (0, _reactIntl.defineMessages)({
  title: {
    'id': 'searchResultTitleBar.title',
    'defaultMessage': 'Search Result'
  },
  keyword: {
    'id': 'searchResultTitleBar.keyword',
    'defaultMessage': 'containing {keyword}'
  },
  related: {
    'id': 'searchResultTitleBar.related',
    'defaultMessage': 'related to {record}'
  }
});

const propTypes = {
  config: _propTypes2.default.object,
  searchDescriptor: _propTypes2.default.instanceOf(_immutable2.default.Map),
  searchName: _propTypes2.default.string
};

function SearchResultTitleBar(props) {
  const config = props.config,
        searchDescriptor = props.searchDescriptor,
        searchName = props.searchName,
        remainingProps = _objectWithoutProperties(props, ['config', 'searchDescriptor', 'searchName']);

  const recordType = searchDescriptor.get('recordType');
  const vocabulary = searchDescriptor.get('vocabulary');
  const csid = searchDescriptor.get('csid');
  const subresource = searchDescriptor.get('subresource');
  const searchQuery = searchDescriptor.get('searchQuery');

  const recordTypeConfig = (0, _get2.default)(config, ['recordTypes', recordType]);
  const vocabularyConfig = vocabulary ? (0, _get2.default)(recordTypeConfig, ['vocabularies', vocabulary]) : undefined;
  const subresourceConfig = subresource ? (0, _get2.default)(config, ['subresources', subresource]) : undefined;

  const advancedSearchCondition = searchQuery.get('as');
  const kw = searchQuery.get('kw');
  const rel = searchQuery.get('rel');

  let queryTitle;

  if (rel) {
    const recordLink = _react2.default.createElement(_CsidLink2.default, { config: config, searchName: `${searchName}.rel`, csid: rel });

    queryTitle = _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.related, {
      values: { record: recordLink }
    }));
  } else {
    queryTitle = kw ? _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.keyword, { values: { keyword: kw } })) : null;
  }

  let collectionName;

  if (subresourceConfig) {
    const recordLink = _react2.default.createElement(_CsidLink2.default, { config: config, searchName: `${searchName}.csid`, csid: csid });

    collectionName = _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, subresourceConfig.messages.collectionName, {
      values: { record: recordLink }
    }));
  } else if (vocabularyConfig) {
    collectionName = _react2.default.createElement(_reactIntl.FormattedMessage, vocabularyConfig.messages.collectionName);
  } else {
    collectionName = _react2.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.messages.record.collectionName);
  }

  let advancedTitle;

  if (advancedSearchCondition) {
    advancedTitle = _react2.default.createElement(_AdvancedSearchBuilder2.default, {
      condition: advancedSearchCondition,
      config: config,
      inline: true,
      readOnly: true,
      recordType: recordType
    });
  }

  const title = _react2.default.createElement(
    'div',
    null,
    collectionName,
    ' ',
    queryTitle
  );

  const aside = _react2.default.createElement(_reactIntl.FormattedMessage, messages.title);

  return _react2.default.createElement(_TitleBar2.default, _extends({
    title: title,
    aside: aside,
    subtitle: advancedTitle
  }, remainingProps));
}

SearchResultTitleBar.propTypes = propTypes;