"""
Python polyfills for operator
"""

from __future__ import annotations

import operator
import sys
from typing import Any, overload, SupportsIndex, TYPE_CHECKING, TypeVar
from typing_extensions import TypeVarTuple, Unpack

from ..decorators import substitute_in_graph
from . import import_fresh_module


# Import the pure Python operator module, blocking the C extension
py_operator = import_fresh_module("operator", blocked=["_operator"])
if py_operator is None:
    raise ImportError("operator module not found")


if TYPE_CHECKING:
    from collections.abc import Callable, Container, Iterable, MutableSequence, Sequence


# Most unary and binary operators are handled by BuiltinVariable (e.g., `pos`, `add`)
__all__ = [
    "add",
    "and_",
    "attrgetter",
    "concat",
    "contains",
    "countOf",
    "delitem",
    "floordiv",
    "getitem",
    "index",
    "indexOf",
    "inv",
    "itemgetter",
    "lshift",
    "methodcaller",
    "mod",
    "mul",
    "neg",
    "not_",
    "or_",
    "pos",
    "pow",
    "rshift",
    "setitem",
    "sub",
    "truediv",
    "truth",
]


_T = TypeVar("_T")
_T1 = TypeVar("_T1")
_T2 = TypeVar("_T2")
_Ts = TypeVarTuple("_Ts")
_U = TypeVar("_U")
_U1 = TypeVar("_U1")
_U2 = TypeVar("_U2")
_Us = TypeVarTuple("_Us")

# Sentinel object used to signal a missing attribute/key
_not_present = object()


@overload
# pyrefly: ignore [inconsistent-overload]
def attrgetter(attr: str, /) -> Callable[[Any], _U]: ...


@overload
# pyrefly: ignore [inconsistent-overload]
def attrgetter(
    attr1: str, attr2: str, /, *attrs: str
) -> Callable[[Any], tuple[_U1, _U2, Unpack[_Us]]]: ...


# Reference: https://docs.python.org/3/library/operator.html#operator.attrgetter
@substitute_in_graph(operator.attrgetter, is_embedded_type=True)  # type: ignore[arg-type,misc]
def attrgetter(*attrs: str) -> Callable[[Any], Any | tuple[Any, ...]]:
    if len(attrs) == 0:
        raise TypeError("attrgetter expected 1 argument, got 0")

    if any(not isinstance(attr, str) for attr in attrs):
        raise TypeError("attribute name must be a string")

    def resolve_attr(obj: Any, attr: str) -> Any:
        for name in attr.split("."):
            obj = getattr(obj, name)
        return obj

    if len(attrs) == 1:
        attr = attrs[0]

        def getter(obj: Any) -> Any:
            return resolve_attr(obj, attr)

    else:

        def getter(obj: Any) -> tuple[Any, ...]:  # type: ignore[misc]
            return tuple(resolve_attr(obj, attr) for attr in attrs)

    return getter


# Reference: https://docs.python.org/3/library/operator.html#operator.concat
@substitute_in_graph(operator.concat, can_constant_fold_through=True)  # type: ignore[arg-type]
def concat(a: Sequence[_T], b: Sequence[_T2], /) -> Sequence[_T | _T2]:
    return a + b  # type: ignore[operator]


# Reference: https://docs.python.org/3/library/operator.html#operator.countOf
@substitute_in_graph(operator.countOf, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def countOf(a: Iterable[_T], b: _T, /) -> int:
    return sum(it is b or it == b for it in a)


# Reference: https://docs.python.org/3/library/operator.html#operator.iconcat
@substitute_in_graph(operator.iconcat)  # type: ignore[arg-type]
def iconcat(a: Sequence[_T], b: Sequence[_T2], /) -> Sequence[_T | _T2]:
    a += b  # type: ignore[operator]
    return a  # type: ignore[return-value]


@overload
# pyrefly: ignore [inconsistent-overload]
def itemgetter(item: _T, /) -> Callable[[Any], _U]: ...


@overload
# pyrefly: ignore [inconsistent-overload]
def itemgetter(
    item1: _T1, item2: _T2, /, *items: Unpack[_Ts]
) -> Callable[[Any], tuple[_U1, _U2, Unpack[_Us]]]: ...


# Reference: https://docs.python.org/3/library/operator.html#operator.itemgetter
@substitute_in_graph(operator.itemgetter, is_embedded_type=True)  # type: ignore[arg-type,misc]
def itemgetter(*items: Any) -> Callable[[Any], Any | tuple[Any, ...]]:
    if len(items) == 0:
        raise TypeError("itemgetter expected 1 argument, got 0")

    if len(items) == 1:
        item = items[0]

        def getter(obj: Any) -> Any:
            return obj[item]

    else:

        def getter(obj: Any) -> tuple[Any, ...]:  # type: ignore[misc]
            return tuple(obj[item] for item in items)

    return getter


# Reference: https://docs.python.org/3/library/operator.html#operator.methodcaller
@substitute_in_graph(operator.methodcaller, is_embedded_type=True)  # type: ignore[arg-type]
def methodcaller(name: str, /, *args: Any, **kwargs: Any) -> Callable[[Any], Any]:
    if not isinstance(name, str):
        raise TypeError("method name must be a string")

    def caller(obj: Any) -> Any:
        return getattr(obj, name)(*args, **kwargs)

    return caller


@substitute_in_graph(operator.add, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def add(a: Any, b: Any, /) -> Any:
    return py_operator.add(a, b)


@substitute_in_graph(operator.sub, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def sub(a: Any, b: Any, /) -> Any:
    return py_operator.sub(a, b)


@substitute_in_graph(operator.mul, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def mul(a: Any, b: Any, /) -> Any:
    return py_operator.mul(a, b)


@substitute_in_graph(operator.floordiv, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def floordiv(a: Any, b: Any, /) -> Any:
    return py_operator.floordiv(a, b)


@substitute_in_graph(operator.truediv, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def truediv(a: Any, b: Any, /) -> Any:
    return py_operator.truediv(a, b)


@substitute_in_graph(operator.contains, can_constant_fold_through=False)  # type: ignore[arg-type,misc]
def contains(a: Container[object], b: object, /) -> bool:
    return py_operator.contains(a, b)


@substitute_in_graph(operator.truth, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def truth(a: Any, /) -> bool:
    return py_operator.truth(a)


if sys.version_info >= (3, 11):
    __all__.append("call")

    @substitute_in_graph(operator.call, can_constant_fold_through=False)  # type: ignore[arg-type,misc]
    def call(obj: Callable[..., Any], /, *args, **kwargs) -> Any:
        return py_operator.call(obj, *args, **kwargs)


@substitute_in_graph(operator.getitem, can_constant_fold_through=False)  # type: ignore[arg-type,misc]
def getitem(a, b, /) -> Any:
    return py_operator.getitem(a, b)


@substitute_in_graph(operator.indexOf, can_constant_fold_through=False)  # type: ignore[arg-type,misc]
def indexOf(a: Iterable[_T], b: _T, /) -> int:
    return py_operator.indexOf(a, b)


@substitute_in_graph(operator.inv, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def inv(a: Any, /) -> Any:
    return py_operator.inv(a)


@substitute_in_graph(operator.lshift, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def lshift(a: Any, b: Any, /) -> Any:
    return py_operator.lshift(a, b)


@substitute_in_graph(operator.rshift, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def rshift(a: Any, b: Any, /) -> Any:
    return py_operator.rshift(a, b)


@substitute_in_graph(operator.mod, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def mod(a: Any, b: Any, /) -> Any:
    return py_operator.mod(a, b)


@substitute_in_graph(operator.neg, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def neg(a: Any, /) -> Any:
    return py_operator.neg(a)


@substitute_in_graph(operator.pos, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def pos(a: Any, /) -> Any:
    return py_operator.pos(a)


@substitute_in_graph(operator.pow, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def pow(a: Any, b: Any, /) -> Any:
    return py_operator.pow(a, b)


@substitute_in_graph(operator.or_, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def or_(a: Any, b: Any, /) -> Any:
    return py_operator.or_(a, b)


@substitute_in_graph(operator.and_, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def and_(a: Any, b: Any, /) -> Any:
    return py_operator.and_(a, b)


@substitute_in_graph(operator.not_, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def not_(a: Any, /) -> Any:
    return py_operator.not_(a)


@substitute_in_graph(operator.setitem, can_constant_fold_through=False)  # type: ignore[arg-type,misc]
def setitem(a: MutableSequence[_T], b: SupportsIndex, c: _T, /) -> None:
    return py_operator.setitem(a, b, c)


@substitute_in_graph(operator.delitem, can_constant_fold_through=False)  # type: ignore[arg-type,misc]
def delitem(a: MutableSequence[_T], b: SupportsIndex, /) -> None:
    return py_operator.delitem(a, b)


@substitute_in_graph(operator.index, can_constant_fold_through=True)  # type: ignore[arg-type,misc]
def index(a: SupportsIndex, /) -> int:
    nb_index = getattr(a, "__index__", _not_present)
    if nb_index is _not_present:
        raise TypeError(
            f"{type(a).__name__!r} object cannot be interpreted as an integer"
        )
    out = nb_index()  # type: ignore[not-callable]
    if not isinstance(out, int):
        raise TypeError(f"__index__ returned non-int, (type {type(out).__name__})")
    return int(out)
