"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.Highlighter = exports.NO_HIGHLIGHT_REGEX = exports.HIGHLIGHT_TYPE = void 0;

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

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

var _lodash = _interopRequireDefault(require("lodash"));

var _constants = require("../../utils/constants");

function highlight(matchedText, listHighlightType) {
  switch (listHighlightType) {
    case HIGHLIGHT_TYPE.BACKGROUND:
      return "<span class=\"rc-highlight\">" + matchedText + "</span>";

    case HIGHLIGHT_TYPE.BOLD:
      return "<strong>" + matchedText + "</strong>";

    default:
      return matchedText;
  }
}

var HIGHLIGHT_TYPE = {
  BACKGROUND: 'BACKGROUND',
  BOLD: 'BOLD'
};
exports.HIGHLIGHT_TYPE = HIGHLIGHT_TYPE;
var NO_HIGHLIGHT_REGEX = /<nhl>.*?<\/nhl>/g;
exports.NO_HIGHLIGHT_REGEX = NO_HIGHLIGHT_REGEX;

var Highlighter =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(Highlighter, _React$Component);

  function Highlighter() {
    return _React$Component.apply(this, arguments) || this;
  }

  Highlighter.arraysToString = function arraysToString(arr1, arr2) {
    return _lodash.default.zipWith(arr1, arr2, function (item1, item2) {
      return (item1 || '') + (item2 || '');
    }).join('');
  };

  var _proto = Highlighter.prototype;

  _proto.emphazis = function emphazis(part) {
    return part ? this.props.highlight(_lodash.default.escape(part), this.props.listHighlightType) : part;
  };

  _proto.shouldProceedWithHighlight = function shouldProceedWithHighlight() {
    var isFiltred = true;
    var _this$props = this.props,
        children = _this$props.children,
        highlightRegexp = _this$props.highlightRegexp,
        filterRegexp = _this$props.filterRegexp;
    var childrenString = String(children).replace(_constants.htmlTagRegEx, '');

    if (filterRegexp) {
      filterRegexp.lastIndex = 0;
      isFiltred = filterRegexp.test(childrenString);
    }

    return highlightRegexp && isFiltred;
  };

  _proto.getHighlightedChild = function getHighlightedChild() {
    var _this = this;

    var children = this.props.children;
    var childrenString = String(children);
    var string = '';

    if (this.shouldProceedWithHighlight()) {
      NO_HIGHLIGHT_REGEX.lastIndex = 0;
      var wordsToHL = childrenString.split(NO_HIGHLIGHT_REGEX);
      var wordsNotToHL = childrenString.match(NO_HIGHLIGHT_REGEX);
      wordsToHL = wordsToHL.map(function (wordToHL) {
        return _this.highlightPart(wordToHL);
      });
      string = Highlighter.arraysToString(wordsToHL, wordsNotToHL);
    }

    return string || childrenString;
  };

  _proto.highlightPart = function highlightPart(text) {
    var _this2 = this;

    var highlightRegexp = this.props.highlightRegexp;
    var words = text.split(_constants.htmlTagRegEx);
    var tags = text.match(_constants.htmlTagRegEx);
    words = words.map(function (word) {
      return _lodash.default.unescape(word).replace(highlightRegexp, function (matchedText) {
        return _this2.emphazis(matchedText);
      });
    });
    return Highlighter.arraysToString(words, tags);
  };

  _proto.render = function render() {
    var title = this.props.title;
    var highlightedText = this.getHighlightedChild();
    return _react.default.createElement("span", {
      title: title,
      dangerouslySetInnerHTML: {
        __html: highlightedText
      }
    });
  };

  return Highlighter;
}(_react.default.Component);

exports.Highlighter = Highlighter;
(0, _defineProperty2.default)(Highlighter, "propTypes", {
  children: _propTypes.default.node,
  listHighlightType: _propTypes.default.oneOf([HIGHLIGHT_TYPE.BACKGROUND, HIGHLIGHT_TYPE.BOLD]),
  highlightRegexp: _propTypes.default.object,
  filterRegexp: _propTypes.default.object,
  highlight: _propTypes.default.func,
  title: _propTypes.default.string
});
(0, _defineProperty2.default)(Highlighter, "defaultProps", {
  children: null,
  listHighlightType: HIGHLIGHT_TYPE.BOLD,
  highlightRegexp: null,
  filterRegexp: null,
  title: null,
  highlight: highlight
});