"use strict";
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 __());
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var lodash_1 = __importDefault(require("lodash"));
var constants_1 = require("../../utils/constants");
function highlight(matchedText, listHighlightType) {
    switch (listHighlightType) {
        case exports.HIGHLIGHT_TYPE.BACKGROUND:
            return "<span class=\"rc-highlight\">" + matchedText + "</span>";
        case exports.HIGHLIGHT_TYPE.BOLD:
            return "<strong>" + matchedText + "</strong>";
        default:
            return matchedText;
    }
}
exports.HIGHLIGHT_TYPE = {
    BACKGROUND: 'BACKGROUND',
    BOLD: 'BOLD'
};
exports.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 lodash_1.default.zipWith(arr1, arr2, function (item1, item2) {
            return (item1 || '') + (item2 || '');
        }).join('');
    };
    Highlighter.prototype.emphazis = function (part) {
        return part ? this.props.highlight(lodash_1.default.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(constants_1.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()) {
            exports.NO_HIGHLIGHT_REGEX.lastIndex = 0;
            var wordsToHL = childrenString.split(exports.NO_HIGHLIGHT_REGEX);
            var wordsNotToHL = childrenString.match(exports.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(constants_1.htmlTagRegEx);
        var tags = text.match(constants_1.htmlTagRegEx);
        words = words.map(function (word) {
            return lodash_1.default.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_1.default.createElement("span", { title: title, dangerouslySetInnerHTML: { __html: highlightedText } });
    };
    Highlighter.propTypes = {
        children: prop_types_1.default.node,
        listHighlightType: prop_types_1.default.oneOf([exports.HIGHLIGHT_TYPE.BACKGROUND, exports.HIGHLIGHT_TYPE.BOLD]),
        highlightRegexp: prop_types_1.default.object,
        filterRegexp: prop_types_1.default.object,
        highlight: prop_types_1.default.func,
        title: prop_types_1.default.string
    };
    Highlighter.defaultProps = {
        children: null,
        listHighlightType: exports.HIGHLIGHT_TYPE.BOLD,
        highlightRegexp: null,
        filterRegexp: null,
        title: null,
        highlight: highlight
    };
    return Highlighter;
}(react_1.default.Component));
exports.Highlighter = Highlighter;
//# sourceMappingURL=Highlighter.js.map