from io import BytesIO

from phylodata.data_types import EvolutionaryModel, EvolutionaryModelComponent
from phylodata.parsers.parse_beast2_packages import BEAST2_PACKAGE_PARSERS
from phylodata.utils.bytesio_utils import get_xml_from_bytesio


def parse_evolutionary_model(beast2_config: BytesIO) -> EvolutionaryModel:
    xml = get_xml_from_bytesio(beast2_config)

    models = []

    for parser in BEAST2_PACKAGE_PARSERS:
        if parser.is_used(xml):
            model = EvolutionaryModelComponent(
                name=parser.get_name(),
                description=parser.get_description(),
                parameters=parser.get_parameters(xml),
                documentation_url=parser.get_documentation_url(),
                type=parser.get_type(),
            )
            models.append(model)

    return EvolutionaryModel(models=models)
