const MAX_ENTRIES = 50;
const MAX_SOURCES_PER_CLAIM = 3;
const JACCARD_THRESHOLD = 0.2;
const MIN_SENTENCE_LEN = 10;
const MIN_TOKEN_LEN = 4;
const STOPWORDS = /* @__PURE__ */ new Set([
  "a",
  "an",
  "the",
  "is",
  "are",
  "was",
  "were",
  "and",
  "or",
  "but",
  "in",
  "on",
  "at",
  "to",
  "for",
  "of",
  "with",
  "by",
  "from",
  "as",
  "that",
  "this",
  "it",
  "its",
  "be",
  "been",
  "has",
  "have",
  "had"
]);
function buildCitationGraph(synthesisText, sources) {
  if (!synthesisText || synthesisText.trim().length === 0) return [];
  const sentenceMatches = synthesisText.match(/[^.!?]+[.!?]+(?:\s|$)/g);
  const sentences = sentenceMatches && sentenceMatches.length > 0 ? sentenceMatches.map((s) => s.trim()) : [synthesisText.trim()];
  const sourceTokens = sources.map((s) => tokenize(s.markdown));
  const entries = [];
  for (const sentence of sentences) {
    if (entries.length >= MAX_ENTRIES) break;
    if (sentence.length < MIN_SENTENCE_LEN) continue;
    const markerIndices = extractMarkers(sentence, sources.length);
    if (markerIndices.length > 0) {
      entries.push({
        claim: sentence,
        source_indices: markerIndices,
        confidence: "high"
      });
      continue;
    }
    const sentenceTokens = tokenize(sentence);
    if (sentenceTokens.size === 0 || sourceTokens.length === 0) {
      entries.push({ claim: sentence, source_indices: [], confidence: "low" });
      continue;
    }
    const scored = [];
    for (let i = 0; i < sourceTokens.length; i++) {
      const score = jaccard(sentenceTokens, sourceTokens[i]);
      if (score >= JACCARD_THRESHOLD) scored.push({ idx: i, score });
    }
    scored.sort((a, b) => b.score - a.score);
    if (scored.length === 0) {
      entries.push({ claim: sentence, source_indices: [], confidence: "low" });
    } else {
      entries.push({
        claim: sentence,
        source_indices: scored.slice(0, MAX_SOURCES_PER_CLAIM).map((s) => s.idx),
        confidence: "medium"
      });
    }
  }
  return entries;
}
function extractMarkers(sentence, sourceCount) {
  const matches = sentence.match(/\[(\d+)\]/g);
  if (!matches) return [];
  const indices = [];
  const seen = /* @__PURE__ */ new Set();
  for (const m of matches) {
    const n = Number(m.slice(1, -1));
    if (!Number.isFinite(n) || n < 1) continue;
    const idx = n - 1;
    if (idx >= sourceCount) continue;
    if (seen.has(idx)) continue;
    seen.add(idx);
    indices.push(idx);
  }
  return indices;
}
function tokenize(text) {
  const tokens = /* @__PURE__ */ new Set();
  const words = text.toLowerCase().replace(/[^a-z0-9\s]/g, " ").split(/\s+/);
  for (const w of words) {
    if (w.length < MIN_TOKEN_LEN) continue;
    if (STOPWORDS.has(w)) continue;
    tokens.add(w);
  }
  return tokens;
}
function jaccard(a, b) {
  if (a.size === 0 || b.size === 0) return 0;
  let intersection = 0;
  for (const t of a) if (b.has(t)) intersection++;
  const union = a.size + b.size - intersection;
  return union === 0 ? 0 : intersection / union;
}
export {
  buildCitationGraph
};
//# sourceMappingURL=citation-graph.js.map