// packages/block-library/src/utils/search-patterns.js
import removeAccents from "remove-accents";
function normalizeSearchInput(input = "") {
  input = removeAccents(input);
  input = input.trim().toLowerCase();
  return input;
}
function getPatternSearchRank(pattern, searchValue) {
  const normalizedSearchValue = normalizeSearchInput(searchValue);
  const normalizedTitle = normalizeSearchInput(pattern.title);
  let rank = 0;
  if (normalizedSearchValue === normalizedTitle) {
    rank += 30;
  } else if (normalizedTitle.startsWith(normalizedSearchValue)) {
    rank += 20;
  } else {
    const searchTerms = normalizedSearchValue.split(" ");
    const hasMatchedTerms = searchTerms.every(
      (searchTerm) => normalizedTitle.includes(searchTerm)
    );
    if (hasMatchedTerms) {
      rank += 10;
    }
  }
  return rank;
}
function searchPatterns(patterns = [], searchValue = "") {
  if (!searchValue) {
    return patterns;
  }
  const rankedPatterns = patterns.map((pattern) => {
    return [pattern, getPatternSearchRank(pattern, searchValue)];
  }).filter(([, rank]) => rank > 0);
  rankedPatterns.sort(([, rank1], [, rank2]) => rank2 - rank1);
  return rankedPatterns.map(([pattern]) => pattern);
}
export {
  getPatternSearchRank,
  normalizeSearchInput,
  searchPatterns
};
//# sourceMappingURL=search-patterns.js.map
