import { __assign, __rest } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import cx from 'classnames';
import { findAll, } from './chunk';
import memoizeOne from '../utils/memorize-one';
import { hasOwnProperty } from '../utils/hasOwn';
function lowercaseProps(object) {
    var mapped = {};
    for (var key in object) {
        if (hasOwnProperty(object, key)) {
            mapped[key.toLowerCase()] = object[key];
        }
    }
    return mapped;
}
var memoizedLowercaseProps = memoizeOne(lowercaseProps);
export function TextMark(_a) {
    var textToHighlight = _a.textToHighlight, searchWords = _a.searchWords, _b = _a.highlightClassName, highlightClassName = _b === void 0 ? '' : _b, _c = _a.highlightStyle, highlightStyle = _c === void 0 ? {} : _c, _d = _a.activeIndex, activeIndex = _d === void 0 ? -1 : _d, _e = _a.activeClassName, activeClassName = _e === void 0 ? '' : _e, activeStyle = _a.activeStyle, _f = _a.unhighlightClassName, unhighlightClassName = _f === void 0 ? '' : _f, unhighlightStyle = _a.unhighlightStyle, _g = _a.highlightTag, highlightTag = _g === void 0 ? 'mark' : _g, _h = _a.autoEscape, autoEscape = _h === void 0 ? false : _h, _j = _a.caseSensitive, caseSensitive = _j === void 0 ? false : _j, sanitize = _a.sanitize, findChunks = _a.findChunks, className = _a.className, rest = __rest(_a, ["textToHighlight", "searchWords", "highlightClassName", "highlightStyle", "activeIndex", "activeClassName", "activeStyle", "unhighlightClassName", "unhighlightStyle", "highlightTag", "autoEscape", "caseSensitive", "sanitize", "findChunks", "className"]);
    var chunks = findAll({
        autoEscape: autoEscape,
        caseSensitive: caseSensitive,
        findChunks: findChunks,
        sanitize: sanitize,
        searchWords: searchWords,
        textToHighlight: textToHighlight,
    });
    var HighlightTag = highlightTag;
    var highlightIndex = -1;
    var highlightClassNames = '';
    var highlightStyles;
    return (_jsx("span", __assign({ className: className }, rest, { "data-zv": 'v10.0.11' }, { children: chunks.map(function (chunk, index) {
            var _a;
            var text = textToHighlight.substr(chunk.start, chunk.end - chunk.start);
            if (chunk.highlight) {
                highlightIndex++;
                var highlightClass = void 0;
                if (typeof highlightClassName === 'object') {
                    if (!caseSensitive) {
                        highlightClassName = memoizedLowercaseProps(highlightClassName);
                        highlightClass = highlightClassName[text.toLowerCase()];
                    }
                    else {
                        highlightClass = highlightClassName[text];
                    }
                }
                else {
                    highlightClass = highlightClassName;
                }
                var isActive = highlightIndex === +activeIndex;
                highlightClassNames = cx(highlightClass, (_a = {},
                    _a[activeClassName] = isActive,
                    _a));
                highlightStyles =
                    isActive === true && activeStyle != null
                        ? __assign(__assign({}, highlightStyle), activeStyle) : highlightStyle;
                var props = typeof HighlightTag !== 'string'
                    ? {
                        highlightIndex: highlightIndex,
                    }
                    : {};
                return (_jsx(HighlightTag, __assign({ className: highlightClassNames, style: highlightStyles }, props, { children: text }), index));
            }
            else {
                return (_jsx("span", __assign({ className: unhighlightClassName, style: unhighlightStyle, "data-zv": 'v10.0.11' }, { children: text }), index));
            }
        }) }), void 0));
}
