/** @internal */ const identity = (name)=>(item)=>{
        if (typeof item === "string") {
            return item;
        }
        throw new Error(`A \`TextExtractor\` must be provided to \`${name}\` for lists that do not contain strings`);
    };
/**
 * @internal
 * @remarks \@since 6.0.0
 */ const toSearchString = (s, whitespace)=>{
    let q = s.toLowerCase();
    if (whitespace === "ignore") {
        q = q.replace(/\s/g, "");
    } else if (whitespace === "trim") {
        q = q.trim();
    }
    return q;
};
/**
 * @internal
 * @remarks \@since 6.0.0
 */ function filter(options) {
    const { list, query, filter, extractor, whitespace = "keep" } = options;
    if (!list.length) {
        return list;
    }
    const q = toSearchString(query, whitespace);
    if (!q) {
        return list;
    }
    return list.filter((item)=>filter(q, toSearchString(extractor(item), whitespace)));
}
export function caseInsensitiveFilter(options) {
    const { list, query, extractor = identity("caseInsensitiveFilter"), startsWith, whitespace } = options;
    return filter({
        list,
        query,
        extractor,
        whitespace,
        filter (q, value) {
            const matchIndex = value.indexOf(q);
            if (startsWith) {
                return matchIndex === 0;
            }
            return matchIndex !== -1;
        }
    });
}
export function fuzzyFilter(options) {
    const { list, query, extractor = identity("fuzzyFilter"), whitespace } = options;
    let regexp;
    return filter({
        list,
        query,
        extractor,
        whitespace,
        filter (query, value) {
            if (!regexp) {
                regexp = 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");
            }
            return value.length > 0 && regexp.test(value);
        }
    });
}

//# sourceMappingURL=filters.js.map