/**
 * Case-insensitive substring matching that ignores spaces, hyphens, and underscores
 * so "co-op", "co op", and "coop" all match the same token.
 */
export function normalizeForKeywordMatch(value: string): string {
  return value
    .toLowerCase()
    .replace(/[\s\-_]+/g, "")
    .trim();
}

export function textMatchesKeyword(
  haystack: string | null | undefined,
  keyword: string,
): boolean {
  const normalizedHaystack = normalizeForKeywordMatch(haystack ?? "");
  const normalizedKeyword = normalizeForKeywordMatch(keyword);
  if (!normalizedKeyword) return false;
  return normalizedHaystack.includes(normalizedKeyword);
}

export function textMatchesAnyKeyword(
  haystack: string | null | undefined,
  keywords: readonly string[],
): boolean {
  return keywords.some((keyword) => textMatchesKeyword(haystack, keyword));
}
