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

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 _AdvancedSearchBuilder = require('./AdvancedSearchBuilder');

var _AdvancedSearchBuilder2 = _interopRequireDefault(_AdvancedSearchBuilder);

var _SearchToRelateTitleBar = require('../../../styles/cspace-ui/SearchToRelateTitleBar.css');

var _SearchToRelateTitleBar2 = _interopRequireDefault(_SearchToRelateTitleBar);

var _Subtitle = require('../../../styles/cspace-ui/Subtitle.css');

var _Subtitle2 = _interopRequireDefault(_Subtitle);

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

var messages = (0, _reactIntl.defineMessages)({
  title: {
    'id': 'searchToRelateTitleBar.title',
    'defaultMessage': 'Relate {collectionName} {query}'
  },
  keyword: {
    'id': 'searchToRelateTitleBar.keyword',
    'defaultMessage': 'containing {keyword}'
  }
});

var propTypes = {
  config: _propTypes2.default.object,
  isSearchInitiated: _propTypes2.default.bool,
  recordType: _propTypes2.default.string,
  vocabulary: _propTypes2.default.string,
  searchDescriptor: _propTypes2.default.instanceOf(_immutable2.default.Map)
};

function SearchToRelateTitleBar(props) {
  var config = props.config,
      isSearchInitiated = props.isSearchInitiated,
      searchDescriptor = props.searchDescriptor;


  var collectionName = void 0;

  if (!isSearchInitiated) {
    var _recordType = props.recordType,
        _vocabulary = props.vocabulary;


    var _recordTypeConfig = (0, _get2.default)(config, ['recordTypes', _recordType]);

    if (!_recordTypeConfig) {
      return null;
    }

    var _vocabularyConfig = _vocabulary ? (0, _get2.default)(_recordTypeConfig, ['vocabularies', _vocabulary]) : undefined;

    if (_vocabularyConfig) {
      collectionName = _react2.default.createElement(_reactIntl.FormattedMessage, _vocabularyConfig.messages.collectionName);
    } else {
      collectionName = _react2.default.createElement(_reactIntl.FormattedMessage, _recordTypeConfig.messages.record.collectionName);
    }

    return _react2.default.createElement(
      'header',
      { className: _SearchToRelateTitleBar2.default.common },
      _react2.default.createElement(
        'h1',
        null,
        _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.title, { values: { collectionName: collectionName, query: '' } }))
      )
    );
  }

  var recordType = searchDescriptor.get('recordType');
  var vocabulary = searchDescriptor.get('vocabulary');

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

  var searchQuery = searchDescriptor.get('searchQuery');
  var advancedSearchCondition = searchQuery.get('as');
  var kw = searchQuery.get('kw');

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

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

  var subtitle = void 0;

  if (advancedSearchCondition) {
    subtitle = _react2.default.createElement(
      'div',
      { className: _Subtitle2.default.common },
      _react2.default.createElement(_AdvancedSearchBuilder2.default, {
        condition: advancedSearchCondition,
        config: config,
        inline: true,
        readOnly: true,
        recordType: recordType
      })
    );
  }

  return _react2.default.createElement(
    'header',
    { className: _SearchToRelateTitleBar2.default.common },
    _react2.default.createElement(
      'h1',
      null,
      _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.title, { values: { collectionName: collectionName, query: queryTitle } }))
    ),
    subtitle
  );
}

SearchToRelateTitleBar.propTypes = propTypes;