from typing import List, Union, Any, Literal
from swarmauri_base.chunkers.ChunkerBase import ChunkerBase
from swarmauri_core.ComponentBase import ComponentBase

@ComponentBase.register_type(ChunkerBase, 'FixedLengthChunker')
class FixedLengthChunker(ChunkerBase):
    """
    Concrete implementation of ChunkerBase that divides text into fixed-length chunks.
    
    This chunker breaks the input text into chunks of a specified size, making sure 
    that each chunk has exactly the number of characters specified by the chunk size, 
    except for possibly the last chunk.
    """
    chunk_size: int = 256
    type: Literal['FixedLengthChunker'] = 'FixedLengthChunker'
    
    def chunk_text(self, text: Union[str, Any], *args, **kwargs) -> List[str]:
        """
        Splits the input text into fixed-length chunks.

        Parameters:
        - text (Union[str, Any]): The input text to be chunked.
        
        Returns:
        - List[str]: A list of text chunks, each of a specified fixed length.
        """
        # Check if the input is a string, if not, attempt to convert to a string.
        if not isinstance(text, str):
            text = str(text)
        
        # Using list comprehension to split text into chunks of fixed size
        chunks = [text[i:i+self.chunk_size] for i in range(0, len(text), self.chunk_size)]
        
        return chunks