import { createLogger } from "../../logger.js";
const log = createLogger("search");
const SNIPPET_LIMIT = 200;
function toEpoch(dateStr) {
  const d = new Date(dateStr);
  const t = d.getTime();
  if (isNaN(t)) return 0;
  return Math.floor(t / 1e3);
}
function asString(v) {
  return typeof v === "string" && v.length > 0 ? v : void 0;
}
function asNumber(v) {
  return typeof v === "number" && isFinite(v) ? v : void 0;
}
class HnAlgoliaEngine {
  name = "hn-algolia";
  async search(query, options = {}) {
    const timeoutMs = options.timeoutMs ?? 1e4;
    const maxResults = options.maxResults ?? 10;
    const params = new URLSearchParams({
      query,
      hitsPerPage: String(maxResults),
      tags: "story"
    });
    if (options.fromDate || options.toDate) {
      const filters = [];
      if (options.fromDate) {
        const epoch = toEpoch(options.fromDate);
        if (epoch > 0) filters.push(`created_at_i>${epoch}`);
      }
      if (options.toDate) {
        const epoch = toEpoch(options.toDate);
        if (epoch > 0) filters.push(`created_at_i<${epoch}`);
      }
      if (filters.length) params.set("numericFilters", filters.join(","));
    }
    const url = `https://hn.algolia.com/api/v1/search?${params}`;
    log.debug("hn algolia search", { query });
    const response = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) });
    if (!response.ok) throw new Error(`HN Algolia returned ${response.status}`);
    const data = await response.json();
    return this.parseHits(data.hits ?? []);
  }
  parseHits(hits) {
    const results = [];
    const total = hits.length;
    for (let i = 0; i < total; i++) {
      const hit = hits[i];
      const title = asString(hit.title);
      if (!title) continue;
      const objectID = asString(hit.objectID);
      const url = asString(hit.url) ?? (objectID ? `https://news.ycombinator.com/item?id=${objectID}` : void 0);
      if (!url) continue;
      const storyText = asString(hit.story_text);
      const points = asNumber(hit.points) ?? 0;
      const comments = asNumber(hit.num_comments) ?? 0;
      const snippet = storyText ? storyText.slice(0, SNIPPET_LIMIT) : `${points} points \xB7 ${comments} comments`;
      const createdAt = asNumber(hit.created_at_i);
      const published_date = createdAt ? new Date(createdAt * 1e3).toISOString() : void 0;
      results.push({
        title,
        url,
        snippet,
        relevance_score: 1 - i / Math.max(total, 1),
        engine: "hn-algolia",
        ...published_date ? { published_date } : {}
      });
    }
    return results;
  }
}
export {
  HnAlgoliaEngine
};
//# sourceMappingURL=hn-algolia.js.map