from typing import List
import logging

from .propagated_sentence import PropagatedSentence
from .propagated_sentence_builder import build_propagated_sentence
from .json_sentence_parser import is_json_format
from .pipeline.message_parsing_pipeline import MessageParsingPipeline


logger = logging.getLogger(__name__)


class MessageParser:
    def parse(self, text: str) -> List[PropagatedSentence]:
        if not text or not text.strip():
            return []
        payload_text = text.strip()
        if not is_json_format(payload_text):
            return []
        
        logger.debug(f"[1] Parsing JSON: {payload_text[:100]}...")
        parsed_sentence_list = MessageParsingPipeline().parse(payload_text)
        logger.debug(f"[2] Extracted {len(parsed_sentence_list)} sentences: {parsed_sentence_list}")
        
        propagated_sentences = [build_propagated_sentence(s) for s in parsed_sentence_list]
        logger.debug(f"[3] Enum propagated: {propagated_sentences}")
        
        return propagated_sentences
