# Copyright (C) 2022-2023 Exaloop Inc. <https://exaloop.io>

@__internal__
class __internal__:
    pass
@__internal__
class __magic__:
    pass

@tuple
@__internal__
@__notuple__
class bool:
    pass

@tuple
@__internal__
@__notuple__
class byte:
    pass

@tuple
@__internal__
@__notuple__
class int:
    MAX = 9223372036854775807
    pass

@tuple
@__internal__
@__notuple__
class float:
    MIN_10_EXP = -307
    pass

@tuple
@__internal__
@__notuple__
class float32:
    MIN_10_EXP = -37
    pass

@tuple
@__internal__
class NoneType:
    pass

@tuple
@__internal__
class type:
    pass

@tuple
@__internal__
@__notuple__
class Function[T, TR]:
    pass

@tuple
@__internal__
class Callable[T, TR]:
    pass

@tuple
@__internal__
@__notuple__
class Ptr[T]:
    pass
cobj = Ptr[byte]

@tuple
@__internal__
@__notuple__
class Generator[T]:
    pass

@tuple
@__internal__
@__notuple__
class Optional:
    T: type = NoneType

@tuple
@__internal__
@__notuple__
class Int[N: Static[int]]:
    pass

@tuple
@__internal__
@__notuple__
class UInt[N: Static[int]]:
    pass

@__internal__
class pyobj:
    p: Ptr[byte]

@tuple
@__internal__
class str:
    ptr: Ptr[byte]
    len: int

@tuple
@__internal__
class Array:
    len: int
    ptr: Ptr[T]
    T: type

@extend
class type:
    def __new__(obj):
        pass
function = Function

@__internal__
class Ref[T]:
    pass

@tuple
@__internal__
@__notuple__
class Union[TU]:
    # compiler-generated
    def __new__(val):
        TU

@extend
class Function:
    @llvm
    def __new__() -> Function[T, TR]:
        ret ptr null

# dummy
@__internal__
class TypeVar[T]: pass
@__internal__
class ByVal: pass
@__internal__
class ByRef: pass
@__internal__
class Tuple: pass

@__internal__
class ClassVar[T]:
    pass

@__internal__
class RTTI:
    id: int

@__internal__
@tuple
class ellipsis:
    def __new__() -> ellipsis:
        return ()
Ellipsis = ellipsis()

@tuple
@__internal__
class __array__:
    T: type
    def __new__(sz: Static[int]) -> Array[T]:
        pass

@dataclass(init=True)
@tuple
@__internal__
class Import:
    __ipath__: str
    __iname__: str
    P: Static[str]
    def __new__(P: Static[str], name: str, path: str) -> Import[P]:
        return (name, path, )

def __ptr__(var):
    pass

def staticlen(obj):
    pass

def compile_error(msg: Static[str]):
    pass

def isinstance(obj, what):
    pass

@__attribute__
def overload():
    pass

def hasattr(obj, attr: Static[str], *args, **kwargs):
    """Special handling"""
    pass

@overload
def hasattr(obj, attr: Static[str]):
    pass

def getattr(obj, attr: Static[str]):
    pass

def setattr(obj, attr: Static[str], what):
    pass

def tuple(iterable):
    pass

def super():
    pass

def superf(*args):
    """Special handling"""
    pass

def __realized__(fn, args):
    pass

def statictuple(*args):
    return args

def __has_rtti__(T: type):
    pass

# class Type[T]: ...


#(init=False, repr=False, eq=False, order=False, hash=False, pickle=False, python=False, gpu=False, container=False)
@__internal__
@tuple
class NamedTuple:
    args: T
    N: Static[int]  # name cache ID
    T: type

    # todo)) nicer way to express type(())
    def __new__(args: T = (), T: type) -> NamedTuple[0, T]:
        return ((), )

    def __new__(args: T, N: Static[int], T: type) -> NamedTuple[N, T]:
        return (args, )

    def __getitem__(self, key: Static[str]):
        return getattr(self, key)

    def __keys__(self):
        return __internal__.namedkeys(N)

    def __repr__(self):
        keys = self.__keys__()
        values = [v.__repr__() for v in self.args]
        s = ', '.join(f"{keys[i]}: {values[i]}" for i in range(len(keys)))
        return f"({s})"


@__internal__
@tuple
class Partial:
    args: T  # format: (agr1, arg2, ..., (star_args...))
    kwargs: K

    F: Static[str] # function
    M: Static[str] # mask
    T: type # Tuple
    K: type # NamedTuple

    def __new__(args: T, kwargs: K, F: Static[str], M: Static[str], T: type, K: type) -> Partial[F, M, T, K]:
        # __static_print__(args)
        # __static_print__(kwargs)
        return (args, kwargs)

    def __repr__(self):
        return __magic__.repr_partial(self)