import { createLogger } from "../../logger.js";
const log = createLogger("search");
class MarginaliaEngine {
  name = "marginalia";
  async search(query, options = {}) {
    const timeoutMs = options.timeoutMs ?? 1e4;
    const maxResults = options.maxResults ?? 10;
    const encoded = encodeURIComponent(query);
    const url = `https://api.marginalia-search.com/search/${encoded}?count=${maxResults}`;
    log.debug("querying marginalia", { query });
    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(`Marginalia 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 url = typeof item.url === "string" ? item.url : "";
      const title = typeof item.title === "string" ? item.title : "";
      if (!url || !title) continue;
      out.push({
        title,
        url,
        snippet: typeof item.description === "string" ? item.description : "",
        relevance_score: 1 - i / Math.max(items.length, 1),
        engine: "marginalia"
      });
    }
    return out;
  }
}
export {
  MarginaliaEngine
};
//# sourceMappingURL=marginalia.js.map