"""Text chunking utilities"""
import re

def chunk_text(text: str, chunk_size: int = 3000):
    """Split text into fixed-size chunks."""
    if not text:
        return []
    if len(text) <= chunk_size:
        return [text]
    return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]

def chunk_text_by_sentences(text: str, max_chunk_size: int = 3000):
    """Split text into chunks respecting sentence boundaries."""
    if not text:
        return []
    sentences = re.split(r"(?<=[.!?])\s+", text)
    sentences = [s.strip() for s in sentences if s.strip()]
    if not sentences:
        return [text]
    chunks = []
    current_chunk = ""
    for sentence in sentences:
        candidate = (current_chunk + " " + sentence) if current_chunk else sentence
        if len(candidate) <= max_chunk_size:
            current_chunk = candidate
        else:
            if current_chunk:
                chunks.append(current_chunk)
            current_chunk = sentence
    if current_chunk:
        chunks.append(current_chunk)
    return chunks
