# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/05_sint.ipynb.

# %% auto 0
__all__ = ['sint']

# %% ../nbs/05_sint.ipynb 3
from typing import Optional
from numbers import Number

# %% ../nbs/05_sint.ipynb 4
from .cons import P, Z, N, POS, NIL, NEG, POSSYM, NILSYM, NEGSYM
from .types import NumStr
from .utils import numstr2sint, isnumstr

# %% ../nbs/05_sint.ipynb 5
class sint(int):
    def __new__(cls, val: NumStr):
        return super().__new__(cls, numstr2sint(val))

    def eval(self, pos=POS, neg=NEG, nil=NIL):
        match self:
            case  1: return pos
            case -1: return neg
            case _:  return nil

    @classmethod
    def match(cls, s: NumStr, pos=POS, neg=NEG, nil=NIL):
        return cls(s).eval(pos, neg, nil)

    @classmethod
    def tosym(cls, s: NumStr) -> str:
        return cls.match(s, POSSYM, NEGSYM, NILSYM)
    
    @classmethod
    def tostr(cls, s: NumStr) -> str:
        return cls.match(s, P, N, Z)
    
    @classmethod
    def topow(cls, base: int, expo: int, sign: Optional[NumStr] = None) -> Number:
        return base ** (expo * (cls.to(sign or expo)))
    
    @property
    def sym(self) -> str: return self.tosym(self)
    
    @property
    def str(self) -> str: return self.tostr(self)

    def pow(self, base: int, expo: int) -> Number:
        return self.topow(base, expo, self)

    @classmethod
    def to(cls, s: NumStr) -> Optional['sint']:
        return cls(s) if isnumstr(s) else None
    
