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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

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

var _PageSizeChooser = require('./PageSizeChooser');

var _PageSizeChooser2 = _interopRequireDefault(_PageSizeChooser);

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

var _SearchResultSummary2 = _interopRequireDefault(_SearchResultSummary);

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

var messages = (0, _reactIntl.defineMessages)({
  error: {
    'id': 'searchResultSummary.error',
    'defaultMessage': 'Error: {message}'
  },
  editSearch: {
    'id': 'searchResultSummary.editSearch',
    'defaultMessage': 'Revise search'
  }
});

var propTypes = {
  config: _propTypes2.default.object,
  listType: _propTypes2.default.string,
  searchDescriptor: _propTypes2.default.instanceOf(_immutable2.default.Map),
  searchError: _propTypes2.default.instanceOf(_immutable2.default.Map),
  searchResult: _propTypes2.default.instanceOf(_immutable2.default.Map),
  renderEditLink: _propTypes2.default.func,
  onEditSearchLinkClick: _propTypes2.default.func,
  onPageSizeChange: _propTypes2.default.func
};

var defaultProps = {
  renderEditLink: function renderEditLink(searchDescriptor, onEditSearchLinkClick) {
    var recordType = searchDescriptor.get('recordType');
    var vocabulary = searchDescriptor.get('vocabulary');
    var subresource = searchDescriptor.get('subresource');
    var 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;
    }

    var vocabularyPath = vocabulary ? '/' + vocabulary : '';
    var path = '/search/' + recordType + vocabularyPath;

    return _react2.default.createElement(
      _reactRouterDom.Link,
      { to: path, onClick: onEditSearchLinkClick },
      _react2.default.createElement(_reactIntl.FormattedMessage, messages.editSearch)
    );
  }
};

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


  if (searchError) {
    // FIXME: Make a proper error page
    var _message = searchError.get('code') || '';

    return _react2.default.createElement(
      'div',
      { className: _SearchResultSummary2.default.error },
      _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.error, { values: { message: _message } })),
      _react2.default.createElement(
        'p',
        null,
        renderEditLink(searchDescriptor, onEditSearchLinkClick)
      )
    );
  }

  var message = null;
  var pageSize = null;

  if (searchResult) {
    var listTypeConfig = config.listTypes[listType];
    var listNodeName = listTypeConfig.listNodeName;


    var list = searchResult.get(listNodeName);
    var totalItems = parseInt(list.get('totalItems'), 10);

    if (isNaN(totalItems)) {
      message = _react2.default.createElement(_reactIntl.FormattedMessage, listTypeConfig.messages.searching);
    } else {
      var pageNum = parseInt(list.get('pageNum'), 10);

      pageSize = parseInt(list.get('pageSize'), 10);

      var startNum = pageNum * pageSize + 1;
      var endNum = Math.min(pageNum * pageSize + pageSize, totalItems);

      message = _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, listTypeConfig.messages.resultCount, {
        values: {
          totalItems: totalItems,
          startNum: startNum,
          endNum: endNum
        }
      }));
    }
  }

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

  var editLink = renderEditLink(searchDescriptor, onEditSearchLinkClick);

  var content = _react2.default.createElement(
    'div',
    null,
    message,
    message && editLink ? ' | ' : '',
    editLink
  );

  var pageSizeChooser = _react2.default.createElement(_PageSizeChooser2.default, {
    pageSize: pageSize,
    onPageSizeChange: onPageSizeChange
  });

  return _react2.default.createElement(
    'div',
    { className: _SearchResultSummary2.default.normal },
    content,
    pageSizeChooser
  );
}

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