"use strict";

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

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 _CsidLink = _interopRequireDefault(require("../navigation/CsidLink"));

var _TitleBar = _interopRequireDefault(require("../sections/TitleBar"));

var _AdvancedSearchBuilder = _interopRequireDefault(require("./AdvancedSearchBuilder"));

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

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } 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: _propTypes.default.object,
  searchDescriptor: _propTypes.default.instanceOf(_immutable.default.Map),
  searchName: _propTypes.default.string
};

function SearchResultTitleBar(props) {
  const {
    config,
    searchDescriptor,
    searchName
  } = props,
        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, _get.default)(config, ['recordTypes', recordType]);
  const vocabularyConfig = vocabulary ? (0, _get.default)(recordTypeConfig, ['vocabularies', vocabulary]) : undefined;
  const subresourceConfig = subresource ? (0, _get.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 = _react.default.createElement(_CsidLink.default, {
      config: config,
      searchName: "".concat(searchName, ".rel"),
      csid: rel
    });

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

  let collectionName;

  if (subresourceConfig) {
    const recordLink = _react.default.createElement(_CsidLink.default, {
      config: config,
      searchName: "".concat(searchName, ".csid"),
      csid: csid
    });

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

  let advancedTitle;

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

  const title = _react.default.createElement("div", null, collectionName, " ", queryTitle);

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

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

SearchResultTitleBar.propTypes = propTypes;