import { createLogger } from "../../logger.js";
const log = createLogger("search");
class WikipediaEngine {
  name = "wikipedia";
  async search(query, options = {}) {
    const timeoutMs = options.timeoutMs ?? 1e4;
    const maxResults = options.maxResults ?? 10;
    const language = (options.language ?? "en").slice(0, 2).toLowerCase();
    const params = new URLSearchParams({
      action: "opensearch",
      format: "json",
      search: query,
      limit: String(Math.min(Math.max(maxResults, 1), 20)),
      namespace: "0"
    });
    const url = `https://${language}.wikipedia.org/w/api.php?${params}`;
    log.debug("querying wikipedia opensearch", { query, language });
    const response = await fetch(url, {
      signal: AbortSignal.timeout(timeoutMs),
      headers: {
        "User-Agent": "wigolo/0.1 (https://github.com/staticn0va/wigolo)",
        "Accept": "application/json"
      }
    });
    if (!response.ok) throw new Error(`Wikipedia returned ${response.status}`);
    const body = await response.json();
    return this.parseResults(body, maxResults);
  }
  parseResults(body, maxResults) {
    if (!Array.isArray(body) || body.length < 4) return [];
    const titles = Array.isArray(body[1]) ? body[1] : [];
    const snippets = Array.isArray(body[2]) ? body[2] : [];
    const urls = Array.isArray(body[3]) ? body[3] : [];
    const total = Math.min(titles.length, urls.length, maxResults);
    const results = [];
    for (let i = 0; i < total; i++) {
      const title = titles[i];
      const url = urls[i];
      if (!title || !url) continue;
      results.push({
        title,
        url,
        snippet: snippets[i] ?? "",
        relevance_score: 1 - i / Math.max(titles.length, 1),
        engine: "wikipedia"
      });
    }
    return results;
  }
}
export {
  WikipediaEngine
};
//# sourceMappingURL=wikipedia.js.map