"""
  UzbekDB database
"""

def _is_correct(dbstr: str):
    try:
        db = [x for x in dbstr.split("\n") if x]
    except Exception:
        return False
    if len(db) == 0:
        return False
    else:
        return True

def _is_multiline(dbstr: str):
    if _is_correct(dbstr):
        db = [x for x in dbstr.split("\n") if x]
        if len(db) == 1:
            return False
        else:
            return True

def _return_correct_type(s):
    for t in (int, float, complex):
        try:
            return t(s)
        except ValueError:
            continue
    return s

def loads(dbstr: str):
    if _is_correct(dbstr):
        db = [x for x in dbstr.split("\n") if x]
        multiline = _is_multiline(dbstr)
        if multiline:
            dbobj = {}
            for i in db:
                dbel = i.split(" | ")
                dbelc = []
                for i in dbel:
                    dbelc.append(_return_correct_type(i))
                dbobj[dbelc[0]] = dbelc[1:]
        else:
            dbobj = []
            for i in db[0].split(" | "):
                dbobj.append(_return_correct_type(i))
        return dbobj

def dumps(dbobj):
    dbstr = ""
    if isinstance(dbobj, dict):
        for k,v in dbobj.items():
            dbstr = dbstr + f"{k} | {' | '.join(map(str, v))}\n"
    elif isinstance(dbobj, list):
        dbstr = " | ".join(map(str, dbobj))
    return dbstr
