// src/chunking.ts
var DEFAULT_CHUNKING_CONFIG = {
  targetTokens: 200,
  minTokens: 150,
  overlapSentences: 2
};
function estimateTokens(text) {
  return Math.ceil(text.length / 4);
}
function splitSentences(text) {
  const sentences = [];
  const sentenceRegex = /[^.!?]*[.!?]+(?:\s+|$)/g;
  let match;
  let lastIndex = 0;
  while ((match = sentenceRegex.exec(text)) !== null) {
    sentences.push(match[0].trim());
    lastIndex = sentenceRegex.lastIndex;
  }
  if (lastIndex < text.length) {
    const remaining = text.slice(lastIndex).trim();
    if (remaining) {
      sentences.push(remaining);
    }
  }
  return sentences.filter((s) => s.length > 0);
}
function chunkContent(content, config = DEFAULT_CHUNKING_CONFIG) {
  const totalTokens = estimateTokens(content);
  if (totalTokens < config.minTokens) {
    return {
      chunked: false,
      chunks: [{
        content,
        index: 0,
        tokenCount: totalTokens
      }]
    };
  }
  const sentences = splitSentences(content);
  if (sentences.length <= 1) {
    return {
      chunked: false,
      chunks: [{
        content,
        index: 0,
        tokenCount: totalTokens
      }]
    };
  }
  const chunks = [];
  let currentChunkSentences = [];
  let currentTokens = 0;
  let chunkIndex = 0;
  for (let i = 0; i < sentences.length; i++) {
    const sentence = sentences[i];
    const sentenceTokens = estimateTokens(sentence);
    currentChunkSentences.push(sentence);
    currentTokens += sentenceTokens;
    const atTarget = currentTokens >= config.targetTokens;
    const isLastSentence = i === sentences.length - 1;
    if (atTarget || isLastSentence) {
      const chunkContent2 = currentChunkSentences.join(" ");
      chunks.push({
        content: chunkContent2,
        index: chunkIndex,
        tokenCount: estimateTokens(chunkContent2)
      });
      chunkIndex++;
      if (!isLastSentence) {
        const overlapCount = Math.min(config.overlapSentences, currentChunkSentences.length);
        if (overlapCount <= 0) {
          currentChunkSentences = [];
          currentTokens = 0;
        } else {
          currentChunkSentences = currentChunkSentences.slice(-overlapCount);
          currentTokens = currentChunkSentences.reduce((sum, s) => sum + estimateTokens(s), 0);
        }
      }
    }
  }
  return {
    chunked: chunks.length > 1,
    chunks
  };
}
function reassembleChunks(chunks) {
  if (chunks.length === 0) return "";
  if (chunks.length === 1) return chunks[0];
  const result = [chunks[0]];
  for (let i = 1; i < chunks.length; i++) {
    const prevChunk = chunks[i - 1];
    const currChunk = chunks[i];
    const prevSentences = splitSentences(prevChunk);
    const currSentences = splitSentences(currChunk);
    let overlapCount = 0;
    for (let j = 0; j < Math.min(prevSentences.length, currSentences.length); j++) {
      const prevEnd = prevSentences.slice(-(j + 1));
      const currStart = currSentences.slice(0, j + 1);
      if (prevEnd.join(" ") === currStart.join(" ")) {
        overlapCount = j + 1;
      }
    }
    if (overlapCount > 0 && overlapCount < currSentences.length) {
      result.push(currSentences.slice(overlapCount).join(" "));
    } else if (overlapCount === 0) {
      result.push(currChunk);
    }
  }
  return result.join(" ");
}

export {
  DEFAULT_CHUNKING_CONFIG,
  chunkContent,
  reassembleChunks
};
//# sourceMappingURL=chunk-4WMCPJWX.js.map