"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = SearchToSelectTitleBar;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactIntl = require("react-intl");
var _get = _interopRequireDefault(require("lodash/get"));
var _immutable = _interopRequireDefault(require("immutable"));
var _AdvancedSearchBuilder = _interopRequireDefault(require("./AdvancedSearchBuilder"));
var _SearchToSelectTitleBar = _interopRequireDefault(require("../../../styles/cspace-ui/SearchToSelectTitleBar.css"));
var _Subtitle = _interopRequireDefault(require("../../../styles/cspace-ui/Subtitle.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 messages = (0, _reactIntl.defineMessages)({
  title: {
    "id": "searchToSelectTitleBar.title",
    "defaultMessage": "Select {typeName} {query}"
  },
  keyword: {
    "id": "searchToSelectTitleBar.keyword",
    "defaultMessage": "containing \"{keyword}\""
  }
});
const propTypes = {
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  isSearchInitiated: _propTypes.default.bool,
  titleMessage: _propTypes.default.objectOf(_propTypes.default.string),
  recordType: _propTypes.default.string,
  vocabulary: _propTypes.default.string,
  searchDescriptor: _propTypes.default.instanceOf(_immutable.default.Map),
  singleSelect: _propTypes.default.bool
};
const defaultProps = {
  titleMessage: messages.title
};
function SearchToSelectTitleBar(props) {
  const {
    config,
    isSearchInitiated,
    searchDescriptor,
    singleSelect,
    titleMessage
  } = props;
  if (!isSearchInitiated) {
    const {
      recordType,
      vocabulary
    } = props;
    const recordTypeConfig = (0, _get.default)(config, ['recordTypes', recordType]);
    if (!recordTypeConfig) {
      return null;
    }
    const vocabularyConfig = vocabulary ? (0, _get.default)(recordTypeConfig, ['vocabularies', vocabulary]) : undefined;
    const nameMessages = vocabularyConfig ? vocabularyConfig.messages : recordTypeConfig.messages.record;
    const typeNameMessage = singleSelect ? nameMessages.itemName || nameMessages.name : nameMessages.collectionName;
    const typeName = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, typeNameMessage);
    return /*#__PURE__*/_react.default.createElement("header", {
      className: _SearchToSelectTitleBar.default.common
    }, /*#__PURE__*/_react.default.createElement("h1", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, titleMessage, {
      values: {
        typeName,
        query: ''
      }
    }))));
  }
  const recordType = searchDescriptor.get('recordType');
  const vocabulary = searchDescriptor.get('vocabulary');
  const recordTypeConfig = (0, _get.default)(config, ['recordTypes', recordType]);
  const vocabularyConfig = vocabulary ? (0, _get.default)(recordTypeConfig, ['vocabularies', vocabulary]) : undefined;
  const searchQuery = searchDescriptor.get('searchQuery');
  const advancedSearchCondition = searchQuery.get('as');
  const kw = searchQuery.get('kw');
  const queryTitle = kw ? /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.keyword, {
    values: {
      keyword: kw
    }
  })) : null;
  const nameMessages = vocabularyConfig ? vocabularyConfig.messages : recordTypeConfig.messages.record;
  const typeNameMessage = singleSelect ? nameMessages.itemName || nameMessages.name : nameMessages.collectionName;
  const typeName = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, typeNameMessage);
  let subtitle;
  if (advancedSearchCondition) {
    subtitle = /*#__PURE__*/_react.default.createElement("div", {
      className: _Subtitle.default.common
    }, /*#__PURE__*/_react.default.createElement(_AdvancedSearchBuilder.default, {
      condition: advancedSearchCondition,
      config: config,
      inline: true,
      readOnly: true,
      recordType: recordType
    }));
  }
  return /*#__PURE__*/_react.default.createElement("header", {
    className: _SearchToSelectTitleBar.default.common
  }, /*#__PURE__*/_react.default.createElement("h1", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, titleMessage, {
    values: {
      typeName,
      query: queryTitle
    }
  }))), subtitle);
}
SearchToSelectTitleBar.propTypes = propTypes;
SearchToSelectTitleBar.defaultProps = defaultProps;