function highlightSearchMatches(text, searchQuery) {
  if (!text || !searchQuery.trim()) {
    return text;
  }
  const query = searchQuery.trim().toLowerCase();
  const lowerText = text.toLowerCase();
  const matches = [];
  let startIndex = 0;
  while (true) {
    const matchIndex = lowerText.indexOf(query, startIndex);
    if (matchIndex === -1) break;
    matches.push({
      start: matchIndex,
      end: matchIndex + query.length
    });
    startIndex = matchIndex + 1;
  }
  if (matches.length === 0) {
    return text;
  }
  let result = "";
  let lastEnd = 0;
  for (const match of matches) {
    result += text.slice(lastEnd, match.start);
    result += `<mark class="bg-yellow-200 dark:bg-yellow-800/50 px-0.5 rounded">${text.slice(match.start, match.end)}</mark>`;
    lastEnd = match.end;
  }
  result += text.slice(lastEnd);
  return result;
}
function highlightMultipleTerms(text, searchTerms) {
  if (!text || !searchTerms.length) {
    return text;
  }
  let result = text;
  const sortedTerms = searchTerms.filter((term) => term.trim().length > 0).sort((a, b) => b.length - a.length);
  for (const term of sortedTerms) {
    result = highlightSearchMatches(result, term);
  }
  return result;
}
function getSearchSnippet(text, searchQuery, maxLength = 150) {
  if (!text || !searchQuery.trim()) {
    return text.slice(0, maxLength) + (text.length > maxLength ? "..." : "");
  }
  const query = searchQuery.trim().toLowerCase();
  const lowerText = text.toLowerCase();
  const matchIndex = lowerText.indexOf(query);
  if (matchIndex === -1) {
    return text.slice(0, maxLength) + (text.length > maxLength ? "..." : "");
  }
  const contextLength = Math.floor((maxLength - query.length) / 2);
  const start = Math.max(0, matchIndex - contextLength);
  const end = Math.min(text.length, matchIndex + query.length + contextLength);
  let snippet = text.slice(start, end);
  if (start > 0) {
    snippet = "..." + snippet;
  }
  if (end < text.length) {
    snippet = snippet + "...";
  }
  return snippet;
}
function highlightSearchMatchesSafe(text, searchQuery) {
  const escapedText = text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
  return highlightSearchMatches(escapedText, searchQuery);
}
function getMatchCount(text, searchQuery) {
  if (!text || !searchQuery.trim()) {
    return 0;
  }
  const query = searchQuery.trim().toLowerCase();
  const lowerText = text.toLowerCase();
  let count = 0;
  let startIndex = 0;
  while (true) {
    const matchIndex = lowerText.indexOf(query, startIndex);
    if (matchIndex === -1) break;
    count++;
    startIndex = matchIndex + 1;
  }
  return count;
}
function calculateRelevanceScore(title, description, searchQuery) {
  if (!searchQuery.trim()) {
    return 0;
  }
  let score = 0;
  const query = searchQuery.trim().toLowerCase();
  const titleLower = title.toLowerCase();
  if (titleLower === query) {
    score += 100;
  } else if (titleLower.startsWith(query)) {
    score += 75;
  } else if (titleLower.includes(query)) {
    score += 50;
  }
  if (description) {
    const descLower = description.toLowerCase();
    if (descLower.includes(query)) {
      score += 25;
    }
  }
  const titleWords = titleLower.split(/\s+/);
  const queryWords = query.split(/\s+/);
  for (const queryWord of queryWords) {
    if (titleWords.includes(queryWord)) {
      score += 10;
    }
  }
  return score;
}
function formatSearchResult(title, description, searchQuery, maxDescLength = 100) {
  const highlightedTitle = highlightSearchMatchesSafe(title, searchQuery);
  let highlightedDescription;
  let snippet;
  if (description) {
    if (description.length > maxDescLength) {
      snippet = getSearchSnippet(description, searchQuery, maxDescLength);
      highlightedDescription = highlightSearchMatchesSafe(snippet, searchQuery);
    } else {
      highlightedDescription = highlightSearchMatchesSafe(description, searchQuery);
    }
  }
  return {
    highlightedTitle,
    highlightedDescription,
    snippet
  };
}
export {
  calculateRelevanceScore,
  formatSearchResult,
  getMatchCount,
  getSearchSnippet,
  highlightMultipleTerms,
  highlightSearchMatches,
  highlightSearchMatchesSafe
};
