import { createLogger } from "../../logger.js";
import { getConfig } from "../../config.js";
const log = createLogger("search");
class BraveImageEngine {
  name = "brave-image";
  async search(query, options = {}) {
    const apiKey = getConfig().braveApiKey;
    if (!apiKey) {
      throw new Error(
        "BRAVE_API_KEY not set \u2014 set the env var to enable Brave image search"
      );
    }
    const timeoutMs = options.timeoutMs ?? 1e4;
    const maxResults = Math.min(options.maxResults ?? 10, 100);
    const params = new URLSearchParams({
      q: query,
      count: String(maxResults),
      safesearch: "moderate"
    });
    const lang = (options.language ?? "").slice(0, 2).toLowerCase();
    if (lang) params.set("search_lang", lang);
    if (options.country) params.set("country", options.country.toUpperCase());
    const url = `https://api.search.brave.com/res/v1/images/search?${params}`;
    log.debug("brave-image: querying api", { query });
    const response = await fetch(url, {
      signal: AbortSignal.timeout(timeoutMs),
      headers: {
        "X-Subscription-Token": apiKey,
        "Accept": "application/json"
      }
    });
    if (!response.ok) throw new Error(`Brave image returned ${response.status}`);
    const body = await response.json();
    return this.parseResults(body, maxResults);
  }
  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 source = typeof item.url === "string" ? item.url : "";
      const imageUrl = typeof item.properties?.url === "string" ? item.properties.url : "";
      if (!source || !imageUrl) continue;
      const thumb = item.thumbnail?.src ?? item.thumbnail?.original;
      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 || imageUrl,
        url: source,
        snippet: item.source ?? "",
        relevance_score: 1 - i / Math.max(items.length, 1),
        engine: "brave-image",
        image_url: imageUrl
      };
      if (typeof thumb === "string" && thumb.length > 0) {
        result.thumbnail_url = thumb;
      }
      if (bothDims) {
        result.width = item.width;
        result.height = item.height;
      }
      out.push(result);
    }
    return out;
  }
}
export {
  BraveImageEngine
};
//# sourceMappingURL=brave-image.js.map