"use strict";

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

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _immutable = _interopRequireDefault(require("immutable"));

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

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

var _get = _interopRequireDefault(require("lodash/get"));

var _PageSizeChooser = _interopRequireDefault(require("./PageSizeChooser"));

var _errorCodes = require("../../constants/errorCodes");

var _SearchResultSummary = _interopRequireDefault(require("../../../styles/cspace-ui/SearchResultSummary.css"));

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

const messages = (0, _reactIntl.defineMessages)({
  error: {
    "id": "searchResultSummary.error",
    "defaultMessage": "Error: {code}"
  },
  [_errorCodes.ERR_NOT_ALLOWED]: {
    "id": "searchResultSummary.ERR_NOT_ALLOWED",
    "defaultMessage": "You're not allowed to perform this search."
  },
  editSearch: {
    "id": "searchResultSummary.editSearch",
    "defaultMessage": "Revise search"
  }
});
const propTypes = {
  config: _propTypes.default.shape({
    listTypes: _propTypes.default.object
  }),
  listType: _propTypes.default.string,
  searchDescriptor: _propTypes.default.instanceOf(_immutable.default.Map),
  searchError: _propTypes.default.instanceOf(_immutable.default.Map),
  searchResult: _propTypes.default.instanceOf(_immutable.default.Map),
  renderEditLink: _propTypes.default.func,
  onEditSearchLinkClick: _propTypes.default.func,
  onPageSizeChange: _propTypes.default.func
};
const defaultProps = {
  renderEditLink: (searchDescriptor, onEditSearchLinkClick) => {
    const recordType = searchDescriptor.get('recordType');
    const vocabulary = searchDescriptor.get('vocabulary');
    const subresource = searchDescriptor.get('subresource');
    const searchQuery = searchDescriptor.get('searchQuery');

    if (subresource || searchQuery.get('rel')) {
      // Services layer does not allow combining related record searches or subresource
      // (terms/refs) searches with keywords or advanced search conditions, so don't render an edit
      // search link.
      return null;
    }

    const vocabularyPath = vocabulary ? `/${vocabulary}` : '';
    const path = `/search/${recordType}${vocabularyPath}`;
    return /*#__PURE__*/_react.default.createElement(_reactRouterDom.Link, {
      to: path,
      onClick: onEditSearchLinkClick
    }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.editSearch));
  }
};

function SearchResultSummary(props) {
  const {
    config,
    listType,
    searchDescriptor,
    searchError,
    searchResult,
    renderEditLink,
    onEditSearchLinkClick,
    onPageSizeChange
  } = props;

  if (searchError) {
    const error = searchError.toJS();
    let {
      code
    } = error;

    if (code === _errorCodes.ERR_API) {
      const status = (0, _get.default)(error, ['error', 'response', 'status']);

      if (status === 401) {
        // Convert 401 to ERR_NOT_ALLOWED.
        code = _errorCodes.ERR_NOT_ALLOWED;
      }
    }

    const message = messages[code] || messages.error;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _SearchResultSummary.default.error
    }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, message, {
      values: error
    })), /*#__PURE__*/_react.default.createElement("p", null, renderEditLink(searchDescriptor, onEditSearchLinkClick)));
  }

  let isSearching = false;
  let message = null;
  let pageSize = null;

  if (searchResult) {
    const listTypeConfig = config.listTypes[listType];
    const {
      listNodeName
    } = listTypeConfig;
    const list = searchResult.get(listNodeName);
    const totalItems = parseInt(list.get('totalItems'), 10);

    if (Number.isNaN(totalItems)) {
      isSearching = true;
      message = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, listTypeConfig.messages.searching);
    } else {
      const pageNum = parseInt(list.get('pageNum'), 10);
      pageSize = parseInt(list.get('pageSize'), 10);
      const startNum = pageNum * pageSize + 1;
      const endNum = Math.min(pageNum * pageSize + pageSize, totalItems);
      message = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, listTypeConfig.messages.resultCount, {
        values: {
          totalItems,
          startNum,
          endNum
        }
      }));
    }
  }

  if (pageSize === null) {
    pageSize = searchDescriptor.getIn(['searchQuery', 'size']);
  }

  const editLink = renderEditLink(searchDescriptor, onEditSearchLinkClick);

  const content = /*#__PURE__*/_react.default.createElement("div", null, message, message && editLink ? ' | ' : '', editLink);

  const pageSizeChooser = /*#__PURE__*/_react.default.createElement(_PageSizeChooser.default, {
    pageSize: pageSize,
    onPageSizeChange: onPageSizeChange
  });

  const className = isSearching ? _SearchResultSummary.default.searching : _SearchResultSummary.default.normal;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: className
  }, content, pageSizeChooser);
}

SearchResultSummary.propTypes = propTypes;
SearchResultSummary.defaultProps = defaultProps;