from typing import List
import re


def split_text(text: str, max_length: int = 4096) -> List[str]:
    """Splits text by lines. If some line is too long, by spaces
    """
    chunks = text.splitlines(keepends=True)
    ans = []
    cur = ""
    while chunks:
        cur_chunk = chunks.pop(0)
        if len(cur_chunk) > max_length:
            split_chunk = re.split('(\S*\s)', cur_chunk)  # Split by whitespace, saving the delimeter
            if len(split_chunk) == 1:
                # if no spaces, split by length
                split_chunk = [
                    cur_chunk[i: i + max_length]
                    for i in range(0, len(cur_chunk), max_length)
                ]
            chunks = split_chunk + chunks
            continue
        if len(cur) + len(cur_chunk) > max_length:
            ans.append(cur)
            cur = cur_chunk
        else:
            cur += cur_chunk
    if cur:
        ans.append(cur)
    return ans