import { createLogger } from "../../logger.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 VQD_PATTERN = /vqd=(?:['"])(\d+-[A-Za-z0-9_-]+)(?:['"])/;
class DdgImageEngine {
  name = "ddg-image";
  async search(query, options = {}) {
    const timeoutMs = options.timeoutMs ?? 1e4;
    const maxResults = options.maxResults ?? 10;
    const ua = USER_AGENTS[Math.floor(Math.random() * USER_AGENTS.length)];
    log.debug("ddg-image: bootstrapping vqd token", { query });
    const bootstrapUrl = `https://duckduckgo.com/?q=${encodeURIComponent(query)}&iax=images&ia=images`;
    const bootstrap = await fetch(bootstrapUrl, {
      signal: AbortSignal.timeout(timeoutMs),
      headers: { "User-Agent": ua, "Accept": "text/html" }
    });
    if (!bootstrap.ok) throw new Error(`DDG image bootstrap returned ${bootstrap.status}`);
    const html = await bootstrap.text();
    const vqd = this.extractVqd(html);
    if (!vqd) throw new Error("DDG image bootstrap missing vqd token");
    const lang = (options.language ?? "en").slice(0, 2).toLowerCase();
    const region = options.country ? `${options.country.toLowerCase()}-${lang}` : "us-en";
    const ijsUrl = `https://duckduckgo.com/i.js?l=${region}&o=json&q=${encodeURIComponent(query)}&vqd=${vqd}&f=,,,,,&p=1`;
    log.debug("ddg-image: fetching i.js", { region });
    const response = await fetch(ijsUrl, {
      signal: AbortSignal.timeout(timeoutMs),
      headers: {
        "User-Agent": ua,
        "Accept": "application/json",
        // DDG sets a strict referer check on i.js; without it the endpoint
        // returns an HTML error page that breaks JSON parsing.
        "Referer": "https://duckduckgo.com/"
      }
    });
    if (!response.ok) throw new Error(`DDG image returned ${response.status}`);
    const body = await response.json();
    return this.parseResults(body, maxResults);
  }
  /**
   * Extract the vqd bootstrap token from a DDG HTML page. Returns null when
   * the page does not carry one — callers should treat that as a hard
   * failure (no token, no JSON endpoint access).
   */
  extractVqd(html) {
    const m = html.match(VQD_PATTERN);
    return m ? m[1] : null;
  }
  parseResults(body, maxResults) {
    if (!body || typeof body !== "object") return [];
    const items = body.results;
    if (!Array.isArray(items)) return [];
    const total = Math.min(items.length, maxResults);
    const out = [];
    for (let i = 0; i < total; i++) {
      const item = items[i];
      if (!item) continue;
      const image = typeof item.image === "string" ? item.image : "";
      const source = typeof item.url === "string" ? item.url : "";
      if (!image || !source) continue;
      const hasW = typeof item.width === "number" && item.width > 0;
      const hasH = typeof item.height === "number" && item.height > 0;
      const bothDims = hasW && hasH;
      const result = {
        title: item.title || item.source || image,
        url: source,
        snippet: item.source ?? "",
        relevance_score: 1 - i / Math.max(items.length, 1),
        engine: "ddg-image",
        image_url: image
      };
      if (typeof item.thumbnail === "string" && item.thumbnail.length > 0) {
        result.thumbnail_url = item.thumbnail;
      }
      if (bothDims) {
        result.width = item.width;
        result.height = item.height;
      }
      out.push(result);
    }
    return out;
  }
}
export {
  DdgImageEngine
};
//# sourceMappingURL=ddg-image.js.map