import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';
import { htmlTagRegEx } from '../../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;
  }
}

export var HIGHLIGHT_TYPE = {
  BACKGROUND: 'BACKGROUND',
  BOLD: 'BOLD'
};
export var NO_HIGHLIGHT_REGEX = /<nhl>.*?<\/nhl>/g;
export var Highlighter =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(Highlighter, _React$Component);

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

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

  var _proto = Highlighter.prototype;

  _proto.emphazis = function emphazis(part) {
    return part ? this.props.highlight(_.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(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(htmlTagRegEx);
    var tags = text.match(htmlTagRegEx);
    words = words.map(function (word) {
      return _.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.createElement("span", {
      title: title,
      dangerouslySetInnerHTML: {
        __html: highlightedText
      }
    });
  };

  return Highlighter;
}(React.Component);

_defineProperty(Highlighter, "propTypes", {
  children: PropTypes.node,
  listHighlightType: PropTypes.oneOf([HIGHLIGHT_TYPE.BACKGROUND, HIGHLIGHT_TYPE.BOLD]),
  highlightRegexp: PropTypes.object,
  filterRegexp: PropTypes.object,
  highlight: PropTypes.func,
  title: PropTypes.string
});

_defineProperty(Highlighter, "defaultProps", {
  children: null,
  listHighlightType: HIGHLIGHT_TYPE.BOLD,
  highlightRegexp: null,
  filterRegexp: null,
  title: null,
  highlight: highlight
});