# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/04_utils.ipynb.

# %% auto 0
__all__ = ['tern2x', 'posstr', 'negstr', 'any2sint', 'int2sint', 'str2sint', 'numstr2sint', 'isnumstr', 'tosint']

# %% ../nbs/04_utils.ipynb 3
from numbers import Number
from typing import Any, Optional

# %% ../nbs/04_utils.ipynb 4
from .cons import POS, NIL, NEG, POSSTRS, NEGSTRS
from .types import SInt, NumStr

# %% ../nbs/04_utils.ipynb 5
from bflag.bflag import bflag
from bflag.types import BoolCall
from bflag.butil import asbool
from bflag.flags import fpos, fneg, fstr, fint

# %% ../nbs/04_utils.ipynb 7
def tern2x(
    val: Any, boola: BoolCall = None, boolb: BoolCall = None, 
    yesa: Any = None, yesb: Any = None, noab: Any = None
) -> bool: 
    return yesa if asbool(val, boola) else yesb if asbool(val, boolb) else noab

def posstr(n: str) -> bool: 
    return n in POSSTRS

def negstr(n: str) -> bool: 
    return n in NEGSTRS

def any2sint(val: Any, gtz: bflag = fpos, ltz: bflag = fneg) -> SInt:
    return tern2x(val, gtz, ltz, POS, NEG, NIL)

def int2sint(num: int) -> SInt:
    return any2sint(num, fpos, fneg)

def str2sint(n: NumStr) -> SInt:
    try:
        return int2sint(int(n))
    except:
        return any2sint(n, posstr, negstr)
    
def numstr2sint(v: Optional[NumStr]) -> SInt:
    return str2sint(v) if fstr(v) else int2sint(v) if fint(v) else 0

def isnumstr(v: Any) -> bool:
    return any(fstr(v) or fint(v))

def tosint(s: NumStr) -> Optional[SInt]:
    return numstr2sint(s) if isnumstr(s) else None
