import { defaultExtractor, search } from "./utils.js";
/**
 * @example
 * ```tsx
 * import { createFuzzyRegExp, toSearchQuery } from "@react-md/core":
 * import { useDeferredValue, useMemo, useState, type ReactElement } from "react";
 *
 * function Example(): ReactElement {
 *   const [value, setValue] = useState("");
 *   const deferredValue = useDeferredValue(value)
 *
 *   const match = useMemo(() => {
 *     const query = toSearchQuery(deferredValue);
 *     if (!query) {
 *       return;
 *     }
 *
 *     const fuzzyRegExp = createFuzzyRegExp(query);
 *     return options.find(option => fuzzyRegExp.test(option.label)):
 *   }, [options])
 *
 *   if (match) {
 *     // do something
 *   }
 * }
 *
 * ```
 *
 * @since 6.0.0
 * @see {@link fuzzySearch}
 */ export function createFuzzyRegExp(query) {
    return new RegExp(query.split("").join("\\w*").replace(/(\(|\||\)|\\(?!w\*)|\[|\|-|\.|\^|\+|\$|\?|^(?!w)\*)/g, "\\$1")// Couldn't get the matching of two '*' working, so replace them here..
    .replace(/\*\*/g, "*\\*"), "i");
}
export function fuzzySearch(options) {
    const { list, type = "filter", query, extractor = defaultExtractor("fuzzySearch"), whitespace } = options;
    // lazy initialize the RegExp since the base `filter` function will modify the
    // query and never call the filter function if:
    // - there is no query
    // - the list is empty
    let regexp;
    return search({
        type,
        list,
        query,
        extractor,
        whitespace,
        filter (query, value) {
            return value.length > 0 && (regexp ??= createFuzzyRegExp(query)).test(value);
        }
    });
}

//# sourceMappingURL=fuzzy.js.map