from types import SimpleNamespace
from sdsvkie.utils.io_file import yaml_load
from pathlib import Path
from copy import copy, deepcopy

def load_cfg(cfg, args=None):
    """
    Convert a configuration object to a dictionary, whether it is a file path, a string, or a SimpleNamespace object.
    Inputs:
        cfg (str) or (Path) or (SimpleNamespace): Configuration object to be converted to a dictionary.
    Returns:
        cfg (dict): Configuration object in dictionary format.
    """
    if isinstance(cfg, (str, Path)):
        cfg = yaml_load(cfg)  # load dict
    elif isinstance(cfg, SimpleNamespace):
        cfg = vars(cfg)  # convert to dict

    
    if args is not None:
        _args = deepcopy(args)
        for k, v in args.items():
            if v is None or v == "cfg" :
                _args.pop(k)

            if v is not None and k == "weights":
                _args['inference_weights'] = v
                _args.pop(k)
    
        cfg.update(_args)
    return cfg


