import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { Children, useMemo } from "react";
import { remove as removeAccents } from "remove-accents";
import { HighlightTextMark } from "./HighlightTextMark.js";
// https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Using_special_characters
const SPECIAL_CHARACTERS_REGEXP = /[.*+?^${}()|[\]\\]/g;
/**
 * The `HighlightText` component can be used to highlight text matching the
 * provided query ignoring case.
 *
 * @example Simple Example
 * ```tsx
 * import { HighlightText } from "@react-md/core/typography/HighlightText";
 * import { Typography } from "@react-md/core/typography/Typography";
 * import { type ReactElement } from "react";
 *
 * export default function SimpleExample(): ReactElement {
 *   return (
 *     <Typography>
 *       <HighlightText query="lorem ipsum">
 *         Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget metus
 *         ut mi mattis dapibus. Praesent interdum sapien ut posuere convallis.
 *         Donec et tristique ex. Aliquam erat volutpat. Donec sit amet dui
 *         egestas, tempus quam id, hendrerit risus. Nullam tincidunt quam ut dui
 *         aliquet ultricies. Lorem ipsum dolor sit amet, consectetur adipiscing
 *         elit. Sed rhoncus, purus vitae tincidunt placerat, quam diam lobortis
 *         magna, et aliquam ante diam id nunc. Cras blandit leo eu nisi
 *         elementum, nec gravida ligula pharetra. Etiam molestie luctus orci,
 *         vel hendrerit lacus eleifend ut. Nullam placerat dolor ac mi congue,
 *         non auctor metus consectetur. Ut pretium mollis vulputate.
 *       </HighlightText>
 *     </Typography>
 *   );
 * }
 *
 * ```
 * @see {@link https://react-md.dev/components/highlight-text | HighlightText Demos}
 * @since 6.0.0
 */ export function HighlightText(props) {
    const { query, children, highlight: Highlight = HighlightTextMark, firstMatchOnly } = props;
    return /*#__PURE__*/ _jsx(_Fragment, {
        children: useMemo(()=>Children.map(children, (child)=>{
                if (!query || !child || typeof child !== "string") {
                    return child;
                }
                const cleanQuery = removeAccents(query).trim();
                if (!cleanQuery) {
                    return /*#__PURE__*/ _jsx(_Fragment, {
                        children: child
                    });
                }
                const parts = [];
                const text = removeAccents(child).trim();
                const regex = new RegExp("(" + cleanQuery.replace(SPECIAL_CHARACTERS_REGEXP, "\\$&") + ")", "gi");
                let match;
                let lastIndex = 0;
                while((match = regex.exec(text)) !== null){
                    const { index } = match;
                    if (index > lastIndex) {
                        parts.push(child.slice(lastIndex, index));
                    }
                    ({ lastIndex } = regex);
                    parts.push(/*#__PURE__*/ _jsx(Highlight, {
                        match: {
                            index,
                            lastIndex,
                            query: cleanQuery,
                            rawQuery: query,
                            rawText: child
                        },
                        children: child.slice(index, lastIndex)
                    }, index));
                    if (firstMatchOnly) {
                        break;
                    }
                }
                if (lastIndex < child.length) {
                    parts.push(child.slice(lastIndex));
                }
                return parts;
            }), [
            Highlight,
            children,
            firstMatchOnly,
            query
        ])
    });
}

//# sourceMappingURL=HighlightText.js.map