"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Highlighter = void 0;
var react_1 = __importDefault(require("react"));
var ramda_1 = require("ramda");
var styles_1 = require("./styles");
var Highlighter = function (_a) {
    var text = _a.text, highlight = _a.highlight, className = _a.className, _b = _a.multiTerm, multiTerm = _b === void 0 ? false : _b;
    var styles = (0, styles_1.useStyles)();
    var hasHighlights = text && highlight;
    if (!hasHighlights) {
        return react_1.default.createElement(react_1.default.Fragment, null, text);
    }
    var REG_SPACES = new RegExp(/\s+/);
    var getMultiTerms = (0, ramda_1.pipe)(ramda_1.trim, (0, ramda_1.split)(REG_SPACES), (0, ramda_1.map)(ramda_1.toLower), (0, ramda_1.sort)(ramda_1.ascend), ramda_1.reverse);
    var highlightedValues = multiTerm ? getMultiTerms(highlight) : [(0, ramda_1.toLower)(highlight)];
    var regValues = highlightedValues.map(function (item) { return item.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); });
    var parts = text.split(new RegExp("(".concat(regValues.join('|'), ")"), 'gi'));
    return (react_1.default.createElement("span", null, parts.map(function (part, i) {
        var isHighlightedText = highlightedValues.includes(part.toLocaleLowerCase());
        return isHighlightedText ? (react_1.default.createElement("span", { key: i, className: className || styles.highlight }, part)) : (part);
    })));
};
exports.Highlighter = Highlighter;
