// packages/block-editor/src/components/inserter/search-items.js
import removeAccents from "remove-accents";
import { noCase } from "change-case";
var defaultGetName = (item) => item.name || "";
var defaultGetTitle = (item) => item.title;
var defaultGetDescription = (item) => item.description || "";
var defaultGetKeywords = (item) => item.keywords || [];
var defaultGetCategory = (item) => item.category;
var defaultGetCollection = () => null;
var splitRegexp = [
  /([\p{Ll}\p{Lo}\p{N}])([\p{Lu}\p{Lt}])/gu,
  // One lowercase or digit, followed by one uppercase.
  /([\p{Lu}\p{Lt}])([\p{Lu}\p{Lt}][\p{Ll}\p{Lo}])/gu
  // One uppercase followed by one uppercase and one lowercase.
];
var stripRegexp = new RegExp("(\\p{C}|\\p{P}|\\p{S})+", "giu");
var extractedWords = /* @__PURE__ */ new Map();
var normalizedStrings = /* @__PURE__ */ new Map();
function extractWords(input = "") {
  if (extractedWords.has(input)) {
    return extractedWords.get(input);
  }
  const result = noCase(input, {
    splitRegexp,
    stripRegexp
  }).split(" ").filter(Boolean);
  extractedWords.set(input, result);
  return result;
}
function normalizeString(input = "") {
  if (normalizedStrings.has(input)) {
    return normalizedStrings.get(input);
  }
  let result = removeAccents(input);
  result = result.replace(/^\//, "");
  result = result.toLowerCase();
  normalizedStrings.set(input, result);
  return result;
}
var getNormalizedSearchTerms = (input = "") => {
  return extractWords(normalizeString(input));
};
var removeMatchingTerms = (unmatchedTerms, unprocessedTerms) => {
  return unmatchedTerms.filter(
    (term) => !getNormalizedSearchTerms(unprocessedTerms).some(
      (unprocessedTerm) => unprocessedTerm.includes(term)
    )
  );
};
var searchBlockItems = (items, categories, collections, searchInput) => {
  const normalizedSearchTerms = getNormalizedSearchTerms(searchInput);
  if (normalizedSearchTerms.length === 0) {
    return items;
  }
  const config = {
    getCategory: (item) => categories.find(({ slug }) => slug === item.category)?.title,
    getCollection: (item) => collections[item.name.split("/")[0]]?.title
  };
  return searchItems(items, searchInput, config);
};
var searchItems = (items = [], searchInput = "", config = {}) => {
  const normalizedSearchTerms = getNormalizedSearchTerms(searchInput);
  if (normalizedSearchTerms.length === 0) {
    return items;
  }
  const rankedItems = items.map((item) => {
    return [item, getItemSearchRank(item, searchInput, config)];
  }).filter(([, rank]) => rank > 0);
  rankedItems.sort(([, rank1], [, rank2]) => rank2 - rank1);
  return rankedItems.map(([item]) => item);
};
function getItemSearchRank(item, searchTerm, config = {}) {
  const {
    getName = defaultGetName,
    getTitle = defaultGetTitle,
    getDescription = defaultGetDescription,
    getKeywords = defaultGetKeywords,
    getCategory = defaultGetCategory,
    getCollection = defaultGetCollection
  } = config;
  const name = getName(item);
  const title = getTitle(item);
  const description = getDescription(item);
  const keywords = getKeywords(item);
  const category = getCategory(item);
  const collection = getCollection(item);
  const normalizedSearchInput = normalizeString(searchTerm);
  const normalizedTitle = normalizeString(title);
  let rank = 0;
  if (normalizedSearchInput === normalizedTitle) {
    rank += 30;
  } else if (normalizedTitle.startsWith(normalizedSearchInput)) {
    rank += 20;
  } else {
    const terms = [
      name,
      title,
      description,
      ...keywords,
      category,
      collection
    ].join(" ");
    const normalizedSearchTerms = extractWords(normalizedSearchInput);
    const unmatchedTerms = removeMatchingTerms(
      normalizedSearchTerms,
      terms
    );
    if (unmatchedTerms.length === 0) {
      rank += 10;
    }
  }
  if (rank !== 0 && name.startsWith("core/")) {
    const isCoreBlockVariation = name !== item.id;
    rank += isCoreBlockVariation ? 1 : 2;
  }
  return rank;
}
export {
  extractWords,
  getItemSearchRank,
  getNormalizedSearchTerms,
  normalizeString,
  searchBlockItems,
  searchItems
};
//# sourceMappingURL=search-items.js.map
