from contoml.errors import InvalidValueError
from contoml.file.file import TOMLFile


def new():
    """
    Constructs a fresh empty TOML data structure.
    """
    from contoml.file.file import TOMLFile
    return TOMLFile([])


def loads(text):
    """
    Parses TOML text into a dict-like object and returns it.
    """
    from .parser import parse_token_stream
    from .lexer import tokenize as lexer
    from .parser.tokenstream import TokenStream
    from .file import TOMLFile

    tokens = lexer(text)
    elements = parse_token_stream(TokenStream(tokens))
    return TOMLFile(elements)


def load(file_path):
    """
    Parses a TOML file into a dict-like object and returns it.
    """
    return loads(open(file_path).read())


def dumps(value):
    """
    Dumps a data structure to TOML source code.

    The given value must be either a dict of dict values, a dict, or a TOML file constructed by this module.
    """

    if isinstance(value, TOMLFile):
        return value.dumps()

    if not isinstance(value, dict):
        raise InvalidValueError('Input must be a dict of dicts, or just a dict!')

    f = new()

    if all(isinstance(child, dict) for child in value.values()):
        for k, v in value.items():
            f[k] = v
    else:
        f[''] = value

    return f.dumps()


def dump(obj, file_path):
    """
    Dumps a data structure to the filesystem as TOML.

    The given value must be either a dict of dict values, a dict, or a TOML file constructed by this module.
    """
    with open(file_path, 'w') as fp:
        fp.write(dumps(obj))
