from typing import TextIO
from typing import Dict


def parse_percepta_txt_output(
    result_file: TextIO, offset: int = 0
) -> Dict[str, Dict[str, str]]:

    """
    Parses text output file from perceptabat_cv.
    Returns a nested dictionary {compound ID: {property name: value}}.
    """

    parsed_output = {}

    for line in result_file:
        if line.split()[0].isdigit() and line.split()[1] != "ID:":
            cp_id = str(int(line.split()[0]) + offset)
            col_name = line.split()[1].rstrip(":").lower()
            value = line.split(": ")[1].rstrip("\n")
            if not cp_id in parsed_output:
                parsed_output[cp_id] = {}
            parsed_output[cp_id][col_name] = value
        else:
            continue

    return parsed_output