var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
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;
var Highlighter = /** @class */ (function (_super) {
    __extends(Highlighter, _super);
    function Highlighter() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Highlighter.arraysToString = function (arr1, arr2) {
        return _.zipWith(arr1, arr2, function (item1, item2) {
            return (item1 || '') + (item2 || '');
        }).join('');
    };
    Highlighter.prototype.emphazis = function (part) {
        return part ? this.props.highlight(_.escape(part), this.props.listHighlightType) : part;
    };
    Highlighter.prototype.shouldProceedWithHighlight = function () {
        var isFiltred = true;
        var _a = this.props, children = _a.children, highlightRegexp = _a.highlightRegexp, filterRegexp = _a.filterRegexp;
        var childrenString = String(children).replace(htmlTagRegEx, '');
        if (filterRegexp) {
            filterRegexp.lastIndex = 0;
            isFiltred = filterRegexp.test(childrenString);
        }
        return highlightRegexp && isFiltred;
    };
    Highlighter.prototype.getHighlightedChild = function () {
        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;
    };
    Highlighter.prototype.highlightPart = function (text) {
        var _this = 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 _this.emphazis(matchedText);
            });
        });
        return Highlighter.arraysToString(words, tags);
    };
    Highlighter.prototype.render = function () {
        var title = this.props.title;
        var highlightedText = this.getHighlightedChild();
        return React.createElement("span", { title: title, dangerouslySetInnerHTML: { __html: highlightedText } });
    };
    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
    };
    Highlighter.defaultProps = {
        children: null,
        listHighlightType: HIGHLIGHT_TYPE.BOLD,
        highlightRegexp: null,
        filterRegexp: null,
        title: null,
        highlight: highlight
    };
    return Highlighter;
}(React.Component));
export { Highlighter };
//# sourceMappingURL=Highlighter.js.map