'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _SearchResultItemLink = require('./SearchResultItemLink');

var _SearchResultItemLink2 = _interopRequireDefault(_SearchResultItemLink);

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

const messages = (0, _reactIntl.defineMessages)({
  pending: {
    'id': 'searchResultLink.pending',
    'defaultMessage': '...'
  },
  error: {
    'id': 'searchResultLink.error',
    'defaultMessage': '...'
  },
  notFound: {
    'id': 'searchResultLink.notFound',
    'defaultMessage': '[record not found]'
  },
  label: {
    'id': 'searchResultLink.label',
    'defaultMessage': '{recordNumber}'
  }
});

const propTypes = {
  config: _propTypes2.default.object,
  listType: _propTypes2.default.string,
  search: _propTypes2.default.func,
  searchName: _propTypes2.default.string,
  searchDescriptor: _propTypes2.default.instanceOf(_immutable2.default.Map),
  itemFilter: _propTypes2.default.func,
  isSearchPending: _propTypes2.default.bool,
  searchError: _propTypes2.default.instanceOf(_immutable2.default.Map),
  searchResult: _propTypes2.default.instanceOf(_immutable2.default.Map)
};

const defaultProps = {
  listType: 'common',
  itemFilter: () => true
};

class SearchResultLink extends _react.Component {
  componentDidMount() {
    this.search();
  }

  componentDidUpdate(prevProps) {
    var _props = this.props;
    const searchName = _props.searchName,
          searchDescriptor = _props.searchDescriptor;
    const prevSearchName = prevProps.searchName,
          prevSearchDescriptor = prevProps.searchDescriptor;


    if (searchName !== prevSearchName || !_immutable2.default.is(searchDescriptor, prevSearchDescriptor)) {
      this.search();
    }
  }

  getResultItem() {
    var _props2 = this.props;
    const config = _props2.config,
          searchResult = _props2.searchResult,
          itemFilter = _props2.itemFilter,
          listType = _props2.listType;
    var _config$listTypes$lis = config.listTypes[listType];
    const listNodeName = _config$listTypes$lis.listNodeName,
          itemNodeName = _config$listTypes$lis.itemNodeName;


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

    if (isNaN(totalItems) || totalItems === 0) {
      return null;
    }

    let items = list.get(itemNodeName);

    if (!_immutable2.default.List.isList(items)) {
      items = _immutable2.default.List.of(items);
    }

    return items.find(itemFilter);
  }

  search() {
    var _props3 = this.props;
    const config = _props3.config,
          search = _props3.search,
          searchName = _props3.searchName,
          searchDescriptor = _props3.searchDescriptor;


    if (search) {
      search(config, searchName, searchDescriptor);
    }
  }

  render() {
    var _props4 = this.props;
    const config = _props4.config,
          isSearchPending = _props4.isSearchPending,
          searchError = _props4.searchError,
          searchResult = _props4.searchResult;


    if (isSearchPending) {
      return _react2.default.createElement(_reactIntl.FormattedMessage, messages.pending);
    }

    if (searchResult) {
      const item = this.getResultItem();

      if (item) {
        return _react2.default.createElement(_SearchResultItemLink2.default, {
          config: config,
          item: item,
          message: messages.label
        });
      }

      return _react2.default.createElement(_reactIntl.FormattedMessage, messages.notFound);
    }

    if (searchError) {
      return _react2.default.createElement(_reactIntl.FormattedMessage, messages.error);
    }

    return null;
  }
}

exports.default = SearchResultLink;
SearchResultLink.propTypes = propTypes;
SearchResultLink.defaultProps = defaultProps;