"use strict";

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

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

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

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

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

var _SearchResultItemLink = _interopRequireDefault(require("./SearchResultItemLink"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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: _propTypes.default.object,
  listType: _propTypes.default.string,
  search: _propTypes.default.func,
  searchName: _propTypes.default.string,
  searchDescriptor: _propTypes.default.instanceOf(_immutable.default.Map),
  itemFilter: _propTypes.default.func,
  isSearchPending: _propTypes.default.bool,
  searchError: _propTypes.default.instanceOf(_immutable.default.Map),
  searchResult: _propTypes.default.instanceOf(_immutable.default.Map)
};
const defaultProps = {
  listType: 'common',
  itemFilter: () => true
};

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

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

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

  getResultItem() {
    const {
      config,
      searchResult,
      itemFilter,
      listType
    } = this.props;
    const {
      listNodeName,
      itemNodeName
    } = config.listTypes[listType];
    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 (!_immutable.default.List.isList(items)) {
      items = _immutable.default.List.of(items);
    }

    return items.find(itemFilter);
  }

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

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

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

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

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

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

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

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

    return null;
  }

}

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