/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-127afd4
 * Released under the MIT license.
 */

import { jsx as _jsx } from "react/jsx-runtime";
import { Text } from './Text.js';
/**
 * Highlights occurrences of a search string within text.
 * Returns an array of ReactNodes with highlighted portions wrapped in Text.Highlight.
 */
export function highlightText(text, highlight, caseSensitive, highlightStyles) {
    if (!highlight) {
        return [text];
    }
    const flags = caseSensitive ? 'g' : 'gi';
    const escapedHighlight = highlight.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    const regex = new RegExp(`(${escapedHighlight})`, flags);
    const parts = text.split(regex);
    return parts.map((part, index) => {
        const isMatch = caseSensitive
            ? part === highlight
            : part.toLowerCase() === highlight.toLowerCase();
        if (isMatch) {
            return (_jsx(Text.Highlight, { styles: highlightStyles, children: part }, index));
        }
        return part;
    });
}

        