import { normalizeUrl } from "../cache/store.js";
import { normalizeResultUrl } from "./url-unwrap.js";
function deduplicateResults(results) {
  const urlMap = /* @__PURE__ */ new Map();
  for (const raw of results) {
    const unwrappedUrl = normalizeResultUrl(raw.url);
    const result = unwrappedUrl !== raw.url ? { ...raw, url: unwrappedUrl } : raw;
    let normalized;
    try {
      normalized = normalizeUrl(result.url);
    } catch {
      normalized = result.url;
    }
    const existing = urlMap.get(normalized);
    if (existing) {
      if (result.relevance_score > existing.relevance_score) {
        existing.relevance_score = result.relevance_score;
        existing.title = result.title;
        existing.snippet = result.snippet;
      }
      if (result.published_date && !existing.published_date) {
        existing.published_date = result.published_date;
      }
      if (!existing.engines.includes(result.engine)) {
        existing.engines.push(result.engine);
      }
    } else {
      urlMap.set(normalized, {
        title: result.title,
        url: result.url,
        snippet: result.snippet,
        relevance_score: result.relevance_score,
        engines: [result.engine],
        ...result.published_date ? { published_date: result.published_date } : {}
      });
    }
  }
  return [...urlMap.values()].sort((a, b) => b.relevance_score - a.relevance_score);
}
export {
  deduplicateResults
};
//# sourceMappingURL=dedup.js.map