# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_butil.ipynb.

# %% auto 0
__all__ = ['isbooltype', 'boolcall', 'echobool', 'ascall', 'callbool', 'asbool']

# %% ../nbs/02_butil.ipynb 3
from typing import Any, Callable

# %% ../nbs/02_butil.ipynb 4
from bflag.types import (BoolCall, BoolFunc)
from bflag.futil import (hasvargs, haskwargs, islambda, lambdastr)

# %% ../nbs/02_butil.ipynb 6
def isbooltype(b: Any) -> bool:
    '''
    Checks if a given value is of type bool.
    
    Parameters
    ----------
    b : Any
        The value to check.
        
    Returns
    -------
    bool
        True if the value is of type bool, otherwise False.
    '''
    return b == bool

def boolcall(b: Any = None) -> BoolCall:
    '''
    Normalizes a Boolean callable to either bool or Callable.
    
    Parameters
    ----------
    b : BoolCall, optional
        The value to normalize.
        
    Returns
    -------
    BoolCall
        The normalized value.
    '''
    return b if isinstance(b, (bool, Callable)) else bool

def echobool(b: bool) -> BoolFunc:
    '''
    Creates a function that echoes a given boolean value.
    
    Parameters
    ----------
    b : bool
        The boolean value to echo.
        
    Returns
    -------
    BoolFunc
        The function that echoes the given boolean value.
    '''
    assert isinstance(b, bool), 'b must be bool'
    func = lambda *args, **kwargs: b
    func.__name__ = f'Echo{b}'
    return func

def ascall(b: BoolCall) -> BoolFunc:
    '''Normalizes a BoolCall to a BoolFunc.
    
    Parameters
    ----------
    b : BoolCall
        The value to normalize.
        
    Returns
    -------
    BoolFunc
        The normalized value.
    '''
    b = boolcall(b)
    return b if isinstance(b, Callable) else echobool(b)


def callbool(v: Any, b: BoolFunc, *args, **kwargs) -> bool:
    '''Evaluates a boolean function with the given value and optional arguments.
    
    Parameters
    ----------
    v : Any
        The value to evaluate.
    b : BoolFunc
        The boolean function to use for the evaluation.
    args : tuple, optional
        Additional arguments to pass to the function.
    kwargs : dict, optional
        Additional keyword arguments to pass to the function.
        
    Returns
    -------
    bool
        The result of the evaluation.
    '''
    b = ascall(b)
    if hasvargs(b) and haskwargs(b): return b(v, *args, **kwargs)
    elif haskwargs(b): return b(v, **kwargs)
    elif hasvargs(b):  return b(v, *args)
    return b(v)

def asbool(v: Any, b: BoolCall, *args, **kwargs) -> bool:
    '''Evaluates a BoolCall with the given value and optional arguments.
    
    Parameters
    ----------
    v : Any
        The value to evaluate.
    b : BoolCall
        The boolean function or value to use for the evaluation.
    args : tuple, optional
        Additional arguments to pass to the function.
    kwargs : dict, optional
        Additional keyword arguments to pass to the function.
        
    Returns
    -------
    bool
        The result of the evaluation.
    '''
    return callbool(v, ascall(b), *args, **kwargs)
