import math
import numpy as np


def bsss_writer(*args):
    """
    Just needed the writer to not write to terminal, defalut none is write to terminal
    """
    # full_bsss_list.append(args[0])


def convert_numpy_types(obj):
    """
    Recursively converts NumPy types within a nested dictionary, list, or tuple
    to their corresponding standard Python types.

    Args:
        obj: The input object which may contain NumPy types.

    Returns:
        The converted object with all NumPy types replaced by Python native types.
    """
    if isinstance(obj, dict):
        return {key: convert_numpy_types(value) for key, value in obj.items()}
    elif isinstance(obj, list):
        return [convert_numpy_types(item) for item in obj]
    elif isinstance(obj, tuple):
        return tuple(convert_numpy_types(item) for item in obj)
    elif isinstance(obj, np.ndarray):
        return obj.tolist()
    elif isinstance(obj, (np.integer, np.floating, np.bool_)):
        return obj.item()
    else:
        return obj


def to_str_dests(dictionary, patt_sel):
    """
    swapps a dictionary from using the string destinations to the int destinations
    """
    if dictionary is None:
        return None
    new_dict = {}
    for key in dictionary:
        if type(key) is int:
            new_dict[patt_sel.globals.DEST_NAMES[key]] = dictionary[key]
        else:
            new_dict[key] = dictionary[key]
    return new_dict


def to_int_dests(dictionary, patt_sel):
    """
    swapps a dictionary from using int destinations to the str destinations
    """
    if dictionary is None:
        return None
    new_dict = {}
    for key in dictionary:
        if type(key) is str:
            new_dict[patt_sel.globals.DEST_NAMES.index(key)] = dictionary[key]
        else:
            new_dict[key] = dictionary[key]
    return new_dict


def cagetToList(value):
    """
    caget does not return consistant datatypes
    if a waveform only has one element it returns a float or int instead of a np.ndarray
    Want to work with lists, so covert the output of a caget value to a list
    """
    if type(value) == np.ndarray:
        return [0 if math.isnan(i) else int(i) for i in value.tolist()]

    if type(value) == float:
        return [0 if math.isnan(value) else int(value)]

    if type(value) == int:
        return [value]

    print(f"cagetToList invalide type: {type(value)}")
