const VERTICALS = [
  "general",
  "news",
  "code",
  "docs",
  "papers",
  "images"
];
const PAPERS_RE = /\b(arxiv|paper|cite|citation|doi|preprint|whitepaper|journal|pubmed|proceedings)\b/i;
const CODE_HARD_RE = /\b(github|pull request|pr #|commit|stack overflow|stackoverflow|compile error|typeerror|traceback|exception)\b/i;
const LANG_TOKEN_RE = /\b(python|typescript|javascript|rust|go|c\+\+|npm|cargo|pip|regex|sql|bash)\b/i;
const HOWTO_VERB_RE = /\b(error|fix|debug|compile)\b/i;
const DOCS_PHRASE_RE = /(\bhow to\b|\btutorial\b|\breference\b|\bapi\b|\bdocumentation\b|\bdocs for\b|\bmdn\b|\bdevdocs\b|\bguide\b|\bgetting started\b)/i;
const NEWS_RE = /\b(latest|today|yesterday|this week|news|breaking|recent|update|announcement)\b/i;
const MIN_YEAR = 1990;
const MAX_YEAR = 2099;
const MS_PER_DAY = 864e5;
function isoDate(d) {
  return d.toISOString().slice(0, 10);
}
function shiftDays(now, days) {
  return isoDate(new Date(now.getTime() - days * MS_PER_DAY));
}
function validYear(y) {
  return y >= MIN_YEAR && y <= MAX_YEAR;
}
function withUnit(now, n, unit) {
  if (!Number.isFinite(n) || n <= 0) return void 0;
  const u = unit.toLowerCase();
  if (u.startsWith("day")) return { fromDate: shiftDays(now, n) };
  if (u.startsWith("week")) return { fromDate: shiftDays(now, 7 * n) };
  if (u.startsWith("month")) return { fromDate: shiftDays(now, 30 * n) };
  if (u.startsWith("year")) return { fromDate: shiftDays(now, 365 * n) };
  return void 0;
}
function parseDateHint(query, now = /* @__PURE__ */ new Date()) {
  if (!query) return void 0;
  const q = query;
  const between = q.match(/\bbetween\s+(\d{4})\s+and\s+(\d{4})\b/i);
  if (between) {
    const y1 = Number(between[1]);
    const y2 = Number(between[2]);
    if (validYear(y1) && validYear(y2) && y1 <= y2) {
      return { fromDate: `${y1}-01-01`, toDate: `${y2}-12-31` };
    }
    return void 0;
  }
  const fromTo = q.match(/\bfrom\s+(\d{4})\s+to\s+(\d{4})\b/i);
  if (fromTo) {
    const y1 = Number(fromTo[1]);
    const y2 = Number(fromTo[2]);
    if (validYear(y1) && validYear(y2) && y1 <= y2) {
      return { fromDate: `${y1}-01-01`, toDate: `${y2}-12-31` };
    }
    return void 0;
  }
  const since = q.match(/\bsince\s+(\d{4})\b/i);
  if (since) {
    const y = Number(since[1]);
    if (validYear(y)) return { fromDate: `${y}-01-01` };
    return void 0;
  }
  const inAfter = q.match(/\b(?:in|after|starting)\s+(\d{4})\b/i);
  if (inAfter) {
    const y = Number(inAfter[1]);
    if (validYear(y)) return { fromDate: `${y}-01-01` };
    return void 0;
  }
  const before = q.match(/\bbefore\s+(\d{4})\b/i);
  if (before) {
    const y = Number(before[1]);
    if (validYear(y)) return { toDate: `${y - 1}-12-31` };
    return void 0;
  }
  const last = q.match(/\blast\s+(\d+)\s+(days?|weeks?|months?|years?)\b/i);
  if (last) {
    const hint = withUnit(now, Number(last[1]), last[2]);
    if (hint) return hint;
  }
  const past = q.match(/\bpast\s+(\d+)\s+(days?|weeks?|months?|years?)\b/i);
  if (past) {
    const hint = withUnit(now, Number(past[1]), past[2]);
    if (hint) return hint;
  }
  if (/\btoday\b/i.test(q)) {
    const d = shiftDays(now, 0);
    return { fromDate: d, toDate: d };
  }
  if (/\byesterday\b/i.test(q)) {
    const d = shiftDays(now, 1);
    return { fromDate: d, toDate: d };
  }
  if (/\bthis\s+week\b/i.test(q)) {
    return { fromDate: shiftDays(now, 7) };
  }
  if (/\bthis\s+month\b/i.test(q)) {
    return { fromDate: shiftDays(now, 30) };
  }
  if (/\bthis\s+year\b/i.test(q)) {
    return { fromDate: `${now.getUTCFullYear()}-01-01` };
  }
  return void 0;
}
function classifyIntentDetailed(query, opts) {
  const q = query ?? "";
  const dateHint = parseDateHint(q, opts?.now);
  if (opts?.hint) {
    return dateHint ? { vertical: opts.hint, dateHint } : { vertical: opts.hint };
  }
  const trimmed = q.trim();
  if (trimmed.length === 0) {
    return { vertical: "general" };
  }
  let vertical;
  if (PAPERS_RE.test(trimmed)) {
    vertical = "papers";
  } else if (CODE_HARD_RE.test(trimmed)) {
    vertical = "code";
  } else if (LANG_TOKEN_RE.test(trimmed) && HOWTO_VERB_RE.test(trimmed)) {
    vertical = "code";
  } else if (DOCS_PHRASE_RE.test(trimmed) || /\blearn\b/i.test(trimmed)) {
    vertical = "docs";
  } else if (opts?.hasDateBound || NEWS_RE.test(trimmed) || !!dateHint) {
    vertical = "news";
  } else {
    vertical = "general";
  }
  return dateHint ? { vertical, dateHint } : { vertical };
}
function classifyIntent(query, opts) {
  return classifyIntentDetailed(query, opts).vertical;
}
export {
  VERTICALS,
  classifyIntent,
  classifyIntentDetailed,
  parseDateHint
};
//# sourceMappingURL=intent-router.js.map