import { parseHTML } from "linkedom";
import { createLogger } from "../../logger.js";
const log = createLogger("search");
const USER_AGENT = "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";
class MojeekEngine {
  name = "mojeek";
  async search(query, options = {}) {
    const timeoutMs = options.timeoutMs ?? 1e4;
    const maxResults = options.maxResults ?? 10;
    const params = new URLSearchParams({ q: query, fmt: "html" });
    const url = `https://www.mojeek.com/search?${params}`;
    log.debug("scraping mojeek", { query });
    const response = await fetch(url, {
      signal: AbortSignal.timeout(timeoutMs),
      headers: { "User-Agent": USER_AGENT, Accept: "text/html" }
    });
    if (!response.ok) throw new Error(`Mojeek returned ${response.status}`);
    const html = await response.text();
    return this.parseResults(html, maxResults);
  }
  parseResults(html, maxResults) {
    if (!html || html.length === 0) return [];
    const { document } = parseHTML(html);
    const items = document.querySelectorAll("ul.results-standard li, .results .result");
    const results = [];
    const total = Math.min(items.length, maxResults);
    for (let i = 0; i < total; i++) {
      const item = items[i];
      const titleAnchor = item.querySelector("a.title, h2 a.title, h2 a") ?? item.querySelector("a");
      const snippetEl = item.querySelector("p.s, .description");
      const href = titleAnchor?.getAttribute("href") ?? "";
      const title = titleAnchor?.textContent?.trim() ?? "";
      if (!href || !title) continue;
      results.push({
        title,
        url: href,
        snippet: snippetEl?.textContent?.trim() ?? "",
        relevance_score: 1 - i / Math.max(items.length, 1),
        engine: "mojeek"
      });
    }
    return results;
  }
}
export {
  MojeekEngine
};
//# sourceMappingURL=mojeek.js.map