import { n as uuid_default } from "./uuid.js";
import { t as isComponent } from "./component.js";
import { r as DuolistGroup } from "./Duolist.js";
import React from "react";
import { Fragment, jsx } from "react/jsx-runtime";
import styles from "./components/Highlighter/styles.module.scss";
const Highlighter = ({ children, searchText }) => {
	if (!searchText) return /* @__PURE__ */ jsx(Fragment, { children });
	const highlightRegex = new RegExp(`(${searchText})`, "i");
	const replaceWithMarkTag = (text) => {
		return text.split(highlightRegex).map((part) => {
			if (highlightRegex.test(part)) {
				const id = uuid_default();
				return /* @__PURE__ */ jsx("mark", {
					className: styles.highlight,
					children: part
				}, id);
			}
			return part;
		});
	};
	const recursivelyHighlight = (node) => {
		if (typeof node === "string") return replaceWithMarkTag(node);
		if (React.isValidElement(node)) {
			const { children: nodeChildren, ...props } = node.props;
			if (isComponent(node, DuolistGroup)) {
				const term = replaceWithMarkTag(node.props.term);
				const description = replaceWithMarkTag(node.props.description);
				return React.cloneElement(node, {
					term,
					description
				});
			}
			if (!nodeChildren) return node;
			const highlightedChildren = React.Children.toArray(nodeChildren).map((child) => recursivelyHighlight(child));
			return React.cloneElement(node, props, highlightedChildren);
		}
		return node;
	};
	return /* @__PURE__ */ jsx(Fragment, { children: React.Children.map(children, recursivelyHighlight) });
};
var Highlighter_default$1 = Highlighter;
export { Highlighter as n, Highlighter_default$1 as t };

//# sourceMappingURL=Highlighter.js.map