import { parseHTML } from "linkedom";
import { createLogger } from "../../logger.js";
import { normalizeResultUrl } from "../url-unwrap.js";
const log = createLogger("search");
const USER_AGENTS = [
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
  "Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0"
];
const DATE_SNIPPET_PATTERN = /^(\w{3}\s+\d{1,2},\s+\d{4}|\d{4}-\d{2}-\d{2})\s*[·—–-]/;
function parseDateFromSnippet(snippet) {
  const match = snippet.trim().match(DATE_SNIPPET_PATTERN);
  if (!match) return void 0;
  const d = new Date(match[1]);
  return isNaN(d.getTime()) ? void 0 : d.toISOString();
}
class DuckDuckGoEngine {
  name = "duckduckgo";
  async search(query, options = {}) {
    const timeoutMs = options.timeoutMs ?? 1e4;
    const maxResults = options.maxResults ?? 10;
    const params = new URLSearchParams({ q: query });
    if (options.country) {
      const lang = (options.language ?? "en").slice(0, 2).toLowerCase();
      params.set("kl", `${options.country.toLowerCase()}-${lang}`);
    }
    const url = `https://lite.duckduckgo.com/lite/?${params}`;
    log.debug("scraping duckduckgo", { query });
    const response = await fetch(url, {
      signal: AbortSignal.timeout(timeoutMs),
      headers: { "User-Agent": USER_AGENTS[Math.floor(Math.random() * USER_AGENTS.length)] }
    });
    if (!response.ok) throw new Error(`DDG returned ${response.status}`);
    const html = await response.text();
    return this.parseResults(html, maxResults);
  }
  parseResults(html, maxResults) {
    const { document } = parseHTML(html);
    const results = [];
    const links = document.querySelectorAll("a.result-link");
    const snippets = document.querySelectorAll(".result-snippet");
    const total = Math.min(links.length, maxResults);
    for (let i = 0; i < total; i++) {
      const link = links[i];
      const snippet = snippets[i];
      const href = link?.getAttribute("href");
      const title = link?.textContent?.trim();
      if (href && title) {
        const snippetText = snippet?.textContent?.trim() ?? "";
        const published_date = parseDateFromSnippet(snippetText);
        results.push({
          title,
          url: normalizeResultUrl(href),
          snippet: snippetText,
          relevance_score: 1 - i / Math.max(links.length, 1),
          engine: "duckduckgo",
          ...published_date ? { published_date } : {}
        });
      }
    }
    return results;
  }
}
export {
  DuckDuckGoEngine
};
//# sourceMappingURL=duckduckgo.js.map