"""
The MIT License (MIT)

Copyright (c) 2021-Present AbstractUmbra

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations

import asyncio
import datetime
import json
import logging
import pathlib
import re
import warnings
from enum import Enum
from functools import wraps
from typing import (
    TYPE_CHECKING,
    Any,
    Callable,
    ClassVar,
    Generic,
    Iterable,
    Literal,
    Mapping,
    Optional,
    Type,
    TypeVar,
    Union,
    overload,
)
from urllib.parse import quote as _uriquote

import aiohttp


try:
    import orjson
except ModuleNotFoundError:
    HAS_ORJSON = False
else:
    HAS_ORJSON = True

from .errors import AuthenticationRequired


if TYPE_CHECKING:
    from typing_extensions import Concatenate, ParamSpec

    from .types.relationship import RelationshipResponse

C = TypeVar("C", bound="Any")
T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)
if TYPE_CHECKING:
    B = ParamSpec("B")


LOGGER = logging.getLogger(__name__)

__all__ = (
    "MANGADEX_URL_REGEX",
    "MANGADEX_TIME_REGEX",
    "MISSING",
    "Route",
    "CustomRoute",
    "cached_slot_property",
    "to_json",
    "json_or_text",
    "php_query_builder",
    "deprecated",
    "to_snake_case",
    "to_camel_case",
    "get_image_mime_type",
    "as_chunks",
    "delta_to_iso",
    "iso_to_delta",
    "relationship_finder",
    "clean_isoformat",
    "MANGA_TAGS",
)

_PROJECT_DIR = pathlib.Path(__file__)
MAX_DEPTH: int = 10_000
MANGADEX_URL_REGEX = re.compile(
    r"(?:http[s]?:\/\/)?mangadex\.org\/(?P<type>title|chapter|author|tag)\/(?P<ID>[a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{12})\/?(?P<title>.*)"
)
MANGADEX_TIME_REGEX = re.compile(
    r"^(P(?P<days>[1-9]|[1-9][0-9])D)?(P?(?P<weeks>[1-9])W)?(P?T((?P<hours>[1-9]|1[0-9]|2[0-4])H)?((?P<minutes>[1-9]|[1-5][0-9]|60)M)?((?P<seconds>[1-9]|[1-5][0-9]|60)S)?)?$"
)
"""
``r"^(P([1-9]|[1-9][0-9])D)?(P?([1-9])W)?(P?T(([1-9]|1[0-9]|2[0-4])H)?(([1-9]|[1-5][0-9]|60)M)?(([1-9]|[1-5][0-9]|60)S)?)?$"``

This `regex pattern <https://docs.python.org/3/library/re.html#re-objects>`_ follows the ISO-8601 standard (MangaDex uses `PHP DateInterval <https://www.php.net/manual/en/dateinterval.construct.php>`_).
The pattern *is* usable but more meant as a guideline for your formatting.

It matches some things like: ``P1D2W`` (1 day, two weeks), ``P1D2WT3H4M`` (1 day, 2 weeks, 3 hours and 4 minutes)
"""


class Route:
    """A helper class for instantiating a HTTP method to MangaDex.

    Parameters
    -----------
    verb: :class:`str`
        The HTTP verb you wish to perform, e.g. ``"POST"``
    path: :class:`str`
        The prepended path to the API endpoint you with to target.
        e.g. ``"/manga/{manga_id}"``
    parameters: Any
        This is a special cased kwargs. Anything passed to these will substitute it's key to value in the `path`.
        E.g. if your `path` is ``"/manga/{manga_id}"``, and your parameters are ``manga_id="..."``, then it will expand into the path
        making ``"manga/..."``
    """

    BASE: ClassVar[str] = "https://api.mangadex.org"

    def __init__(self, verb: str, path: str, **parameters: Any) -> None:
        self.verb: str = verb
        self.path: str = path
        url = self.BASE + self.path
        if parameters:
            url = url.format_map({k: _uriquote(v) if isinstance(v, str) else v for k, v in parameters.items()})
        self.url: str = url


class CustomRoute(Route):
    """A helper class for instantiating a HTTP method to download from MangaDex.

    Parameters
    -----------
    verb: :class:`str`
        The HTTP verb you wish to perform. E.g. ``"POST"``
    base: :class:`str`
        The base URL for the download path.
    path: :class:`str`
        The prepended path to the API endpoint you with to target.
        e.g. ``"/manga/{manga_id}"``
    parameters: Any
        This is a special cased kwargs. Anything passed to these will substitute it's key to value in the `path`.
        E.g. if your `path` is ``"/manga/{manga_id}"``, and your parameters are ``manga_id="..."``, then it will expand into the path
        making ``"manga/..."``
    """

    def __init__(self, verb: str, base: str, path: str, **parameters: Any) -> None:
        self.verb: str = verb
        self.base: str = base
        self.path: str = path
        url = self.base + self.path
        if parameters:
            url = url.format_map({k: _uriquote(v) if isinstance(v, str) else v for k, v in parameters.items()})
        self.url: str = url


class MissingSentinel:
    def __eq__(self, _: Any) -> bool:
        return False

    def __bool__(self) -> bool:
        return False

    def __repr__(self) -> str:
        return "..."


MISSING: Any = MissingSentinel()


class _StrEnum(Enum):  # type: ignore # we import this as needed.
    value: str

    def __str__(self) -> str:
        return self.value


## This class and subsequent decorator have been copied from Rapptz' Discord.py
## (https://github.com/Rapptz/discord.py)
## Credit goes to Rapptz and contributors


class CachedSlotProperty(Generic[T, T_co]):
    def __init__(self, name: str, function: Callable[[T], T_co]) -> None:
        self.name: str = name
        self.function: Callable[[T], T_co] = function
        self.__doc__ = getattr(function, "__doc__")

    @overload
    def __get__(self, instance: None, owner: Type[T]) -> CachedSlotProperty[T, T_co]:
        ...

    @overload
    def __get__(self, instance: T, owner: Type[T]) -> T_co:
        ...

    def __get__(self, instance: Optional[T], owner: Type[T]) -> Any:
        if instance is None:
            return self

        try:
            return getattr(instance, self.name)
        except AttributeError:
            value = self.function(instance)
            setattr(instance, self.name, value)
            return value


def cached_slot_property(name: str) -> Callable[[Callable[[T], T_co]], CachedSlotProperty[T, T_co]]:
    def decorator(func: Callable[[T], T_co]) -> CachedSlotProperty[T, T_co]:
        return CachedSlotProperty(name, func)

    return decorator


def require_authentication(func: Callable[Concatenate[C, B], T]) -> Callable[Concatenate[C, B], T]:
    """A decorator to assure the `self` parameter of decorated methods has authentication set."""

    @wraps(func)
    def wrapper(item: C, *args: B.args, **kwargs: B.kwargs) -> T:
        if not item._http._authenticated:
            raise AuthenticationRequired("This method requires you to be authenticated to the API.")

        return func(item, *args, **kwargs)

    return wrapper


def deprecated(
    alternate: Optional[str] = None,
) -> Callable[[Callable[B, T]], Callable[B, T]]:
    """A decorator to mark a method as deprecated.

    Parameters
    -----------
    alternate: Optional[:class:`str`]
        The alternate method to use.
    """

    def decorator(func: Callable[B, T]) -> Callable[B, T]:
        @wraps(func)
        def wrapper(*args: B.args, **kwargs: B.kwargs) -> T:
            warnings.simplefilter("always", DeprecationWarning)  # turn off filter.
            if alternate is not None:
                fmt = "{0.__qualname__} is deprecated, use {1} instead."
            else:
                fmt = "{0.__qualname__} is deprecated."

            warnings.warn(fmt.format(func, alternate), stacklevel=2, category=DeprecationWarning)

            warnings.simplefilter("default", DeprecationWarning)  # reset filter.
            return func(*args, **kwargs)

        return wrapper

    return decorator


def calculate_limits(limit: int, offset: int, *, max_limit: int = 100) -> tuple[int, int]:
    """A helper function that will calculate the offset and limit parameters for API endpoints.

    Parameters
    -----------
    limit: :class:`int`
        The limit (or amount) of objects you are requesting.
    offset: :class:`int`
        The offset (or pagination start point) for the objects you are requesting.
    max_limit: :class:`int`
        The maximum limit value for the API Endpoint.

    Returns
    --------
    Tuple[:class:`int`, :class:`int`]
    """
    if offset >= MAX_DEPTH:
        raise ValueError(f"An offset of {MAX_DEPTH} will not return results.")

    offset = max(offset, 0)

    difference = MAX_DEPTH - offset
    if difference <= max_limit:
        new_limit = difference
        new_offset = MAX_DEPTH - new_limit
        return new_limit, new_offset

    new_limit = min(max(0, limit), max_limit)
    new_offset = min(max(0, offset), MAX_DEPTH - new_limit)

    return new_limit, new_offset


if HAS_ORJSON is True:

    def to_json(obj: Any) -> str:
        """A quick method that dumps a Python type to JSON object."""
        return orjson.dumps(obj).decode("utf-8")

    _from_json = orjson.loads  # type: ignore # this is guarded in an if.

else:

    def to_json(obj: Any) -> str:
        """A quick method that dumps a Python type to JSON object."""
        return json.dumps(obj, separators=(",", ":"), ensure_ascii=True)

    _from_json = json.loads


async def json_or_text(response: aiohttp.ClientResponse) -> Union[dict[str, Any], str]:
    """A quick method to parse a `aiohttp.ClientResponse` and test if it's json or text."""
    text = await response.text(encoding="utf-8")
    try:
        if response.headers["content-type"] == "application/json":
            try:
                return _from_json(text)
            except json.JSONDecodeError:
                pass
    except KeyError:
        pass

    return text


def php_query_builder(obj: Mapping[str, Optional[Union[str, int, bool, list[str], dict[str, str]]]]) -> str:
    """
    {"order": {"publishAt": "desc"}, "translatedLanguages": ["en", "jp"]}
    ->
    "order[publishAt]=desc&translatedLanguages[]=en&translatedLanguages[]=jp"
    """
    fmt = []
    for key, value in obj.items():
        if value is None:
            fmt.append(f"{key}=null")
        elif isinstance(value, bool):
            fmt.append(f"{key}={str(value).lower()}")
        elif isinstance(value, (str, int)):
            fmt.append(f"{key}={value}")
        elif isinstance(value, list):
            fmt.extend(f"{key}[]={item}" for item in value)
        elif isinstance(value, dict):
            fmt.extend(f"{key}[{subkey}]={subvalue}" for subkey, subvalue in value.items())

    return "&".join(fmt)


def get_image_mime_type(data: bytes) -> str:
    """Returns the image type from the first few bytes."""
    if data.startswith(b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"):
        return "image/png"
    elif data[:3] == b"\xff\xd8\xff" or data[6:10] in (b"JFIF", b"Exif"):
        return "image/jpeg"
    elif data.startswith((b"\x47\x49\x46\x38\x37\x61", b"\x47\x49\x46\x38\x39\x61")):
        return "image/gif"
    elif data.startswith(b"RIFF") and data[8:12] == b"WEBP":
        return "image/webp"
    else:
        raise ValueError("Unsupported image type given")


def to_snake_case(string: str) -> str:
    """Quick function to return snake_case from camelCase."""
    fmt: list[str] = []
    for character in string:
        if character.isupper():
            fmt.append(f"_{character.lower()}")
            continue
        fmt.append(character)
    return "".join(fmt)


def to_camel_case(string: str) -> str:
    """Quick function to return camelCase from snake_case."""
    first, *rest = string.split("_")
    chunks = [first.lower(), *map(str.capitalize, rest)]

    return "".join(chunks)


def as_chunks(iterator: Iterable[T], max_size: int) -> Iterable[list[T]]:
    ret = []
    n = 0
    for item in iterator:
        ret.append(item)
        n += 1
        if n == max_size:
            yield ret
            ret = []
            n = 0
    if ret:
        yield ret


def delta_to_iso(delta: datetime.timedelta) -> str:
    seconds = round(delta.total_seconds())
    weeks, seconds = divmod(seconds, 60 * 60 * 24 * 7)
    days, seconds = divmod(seconds, 60 * 60 * 24)
    hours, seconds = divmod(seconds, 60 * 60)
    minutes, seconds = divmod(seconds, 60)

    builder = "P" * bool(weeks or days)
    if days:
        builder += f"{days}D"
    if weeks:
        builder += f"{weeks}W"

    builder += "T" * bool(hours or minutes or seconds)

    if hours:
        builder += f"{hours}H"
    if minutes:
        builder += f"{minutes}M"
    if seconds:
        builder += f"{seconds}S"

    return builder


def iso_to_delta(iso: str) -> datetime.timedelta:

    if (match := MANGADEX_TIME_REGEX.fullmatch(iso)) is None:
        raise TypeError("The passed string does not match the regex pattern.")

    match_dict = match.groupdict()

    times: dict[str, float] = {key: float(value) for key, value in match_dict.items() if value}
    return datetime.timedelta(**times)


RT = TypeVar("RT", bound="RelationshipResponse", covariant=True)
RelType = Literal["manga", "cover_art", "scanlation_group", "artist", "author", "user", "leader", "member"]


@overload
def relationship_finder(relationships: list[RT], relationship_type: RelType, *, limit: Literal[1]) -> Optional[RT]:
    ...


@overload
def relationship_finder(relationships: list[RT], relationship_type: RelType, *, limit: Optional[int]) -> list[RT]:
    ...


def relationship_finder(
    relationships: list[RT], relationship_type: RelType, *, limit: Optional[int] = None
) -> Optional[Union[list[RT], RT]]:
    ret: list[RT] = []

    for relationship in relationships:
        if relationship["type"] == relationship_type:
            if limit == 1:
                return relationship
            else:
                ret.append(relationship)

    if limit is not None:
        return ret[:limit]

    if limit == 1:
        return

    return ret


def clean_isoformat(dt: datetime.datetime) -> str:
    if dt.tzinfo != datetime.timezone.utc:
        dt = dt.astimezone(datetime.timezone.utc)

    if dt.tzinfo is not None:
        dt = dt.replace(tzinfo=None)

    return dt.isoformat(timespec="seconds")


_path: pathlib.Path = _PROJECT_DIR.parent / "extras" / "tags.json"
with open(_path, "r") as _fp:
    MANGA_TAGS: dict[str, str] = json.load(_fp)


def __build_tags():  # type: ignore  # This is for pre-flight release usage only.
    async def build():
        from . import Client

        client = Client()
        tags = await client.update_tags()

        _diff_a = set(map(str.lower, MANGA_TAGS))
        _diff_b = set(map(str.lower, tags))

        if diff := (_diff_b ^ _diff_a):
            print(f"Tags have changed: {', '.join(diff)}")
        else:
            print("No tag changes.")

        await client.close()

    asyncio.run(build())

"""
The MIT License (MIT)

Copyright (c) 2021-Present AbstractUmbra

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations

import asyncio
import datetime
import json
import logging
import pathlib
import re
import warnings
from enum import Enum
from functools import wraps
from typing import (
    TYPE_CHECKING,
    Any,
    Callable,
    ClassVar,
    Generic,
    Iterable,
    Literal,
    Mapping,
    Optional,
    Type,
    TypeVar,
    Union,
    overload,
)
from urllib.parse import quote as _uriquote

import aiohttp


try:
    import orjson
except ModuleNotFoundError:
    HAS_ORJSON = False
else:
    HAS_ORJSON = True

from .errors import AuthenticationRequired


if TYPE_CHECKING:
    from typing_extensions import Concatenate, ParamSpec

    from .types.relationship import RelationshipResponse

C = TypeVar("C", bound="Any")
T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)
if TYPE_CHECKING:
    B = ParamSpec("B")


LOGGER = logging.getLogger(__name__)

__all__ = (
    "MANGADEX_URL_REGEX",
    "MANGADEX_TIME_REGEX",
    "MISSING",
    "Route",
    "CustomRoute",
    "cached_slot_property",
    "to_json",
    "json_or_text",
    "php_query_builder",
    "deprecated",
    "to_snake_case",
    "to_camel_case",
    "get_image_mime_type",
    "as_chunks",
    "delta_to_iso",
    "iso_to_delta",
    "relationship_finder",
    "clean_isoformat",
    "MANGA_TAGS",
)

_PROJECT_DIR = pathlib.Path(__file__)
MAX_DEPTH: int = 10_000
MANGADEX_URL_REGEX = re.compile(
    r"(?:http[s]?:\/\/)?mangadex\.org\/(?P<type>title|chapter|author|tag)\/(?P<ID>[a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{12})\/?(?P<title>.*)"
)
MANGADEX_TIME_REGEX = re.compile(
    r"^(P(?P<days>[1-9]|[1-9][0-9])D)?(P?(?P<weeks>[1-9])W)?(P?T((?P<hours>[1-9]|1[0-9]|2[0-4])H)?((?P<minutes>[1-9]|[1-5][0-9]|60)M)?((?P<seconds>[1-9]|[1-5][0-9]|60)S)?)?$"
)
"""
``r"^(P([1-9]|[1-9][0-9])D)?(P?([1-9])W)?(P?T(([1-9]|1[0-9]|2[0-4])H)?(([1-9]|[1-5][0-9]|60)M)?(([1-9]|[1-5][0-9]|60)S)?)?$"``

This `regex pattern <https://docs.python.org/3/library/re.html#re-objects>`_ follows the ISO-8601 standard (MangaDex uses `PHP DateInterval <https://www.php.net/manual/en/dateinterval.construct.php>`_).
The pattern *is* usable but more meant as a guideline for your formatting.

It matches some things like: ``P1D2W`` (1 day, two weeks), ``P1D2WT3H4M`` (1 day, 2 weeks, 3 hours and 4 minutes)
"""


class Route:
    """A helper class for instantiating a HTTP method to MangaDex.

    Parameters
    -----------
    verb: :class:`str`
        The HTTP verb you wish to perform, e.g. ``"POST"``
    path: :class:`str`
        The prepended path to the API endpoint you with to target.
        e.g. ``"/manga/{manga_id}"``
    parameters: Any
        This is a special cased kwargs. Anything passed to these will substitute it's key to value in the `path`.
        E.g. if your `path` is ``"/manga/{manga_id}"``, and your parameters are ``manga_id="..."``, then it will expand into the path
        making ``"manga/..."``
    """

    BASE: ClassVar[str] = "https://api.mangadex.org"

    def __init__(self, verb: str, path: str, **parameters: Any) -> None:
        self.verb: str = verb
        self.path: str = path
        url = self.BASE + self.path
        if parameters:
            url = url.format_map({k: _uriquote(v) if isinstance(v, str) else v for k, v in parameters.items()})
        self.url: str = url


class CustomRoute(Route):
    """A helper class for instantiating a HTTP method to download from MangaDex.

    Parameters
    -----------
    verb: :class:`str`
        The HTTP verb you wish to perform. E.g. ``"POST"``
    base: :class:`str`
        The base URL for the download path.
    path: :class:`str`
        The prepended path to the API endpoint you with to target.
        e.g. ``"/manga/{manga_id}"``
    parameters: Any
        This is a special cased kwargs. Anything passed to these will substitute it's key to value in the `path`.
        E.g. if your `path` is ``"/manga/{manga_id}"``, and your parameters are ``manga_id="..."``, then it will expand into the path
        making ``"manga/..."``
    """

    def __init__(self, verb: str, base: str, path: str, **parameters: Any) -> None:
        self.verb: str = verb
        self.base: str = base
        self.path: str = path
        url = self.base + self.path
        if parameters:
            url = url.format_map({k: _uriquote(v) if isinstance(v, str) else v for k, v in parameters.items()})
        self.url: str = url


class MissingSentinel:
    def __eq__(self, _: Any) -> bool:
        return False

    def __bool__(self) -> bool:
        return False

    def __repr__(self) -> str:
        return "..."


MISSING: Any = MissingSentinel()


class _StrEnum(Enum):  # type: ignore # we import this as needed.
    value: str

    def __str__(self) -> str:
        return self.value


## This class and subsequent decorator have been copied from Rapptz' Discord.py
## (https://github.com/Rapptz/discord.py)
## Credit goes to Rapptz and contributors


class CachedSlotProperty(Generic[T, T_co]):
    def __init__(self, name: str, function: Callable[[T], T_co]) -> None:
        self.name: str = name
        self.function: Callable[[T], T_co] = function
        self.__doc__ = getattr(function, "__doc__")

    @overload
    def __get__(self, instance: None, owner: Type[T]) -> CachedSlotProperty[T, T_co]:
        ...

    @overload
    def __get__(self, instance: T, owner: Type[T]) -> T_co:
        ...

    def __get__(self, instance: Optional[T], owner: Type[T]) -> Any:
        if instance is None:
            return self

        try:
            return getattr(instance, self.name)
        except AttributeError:
            value = self.function(instance)
            setattr(instance, self.name, value)
            return value


def cached_slot_property(name: str) -> Callable[[Callable[[T], T_co]], CachedSlotProperty[T, T_co]]:
    def decorator(func: Callable[[T], T_co]) -> CachedSlotProperty[T, T_co]:
        return CachedSlotProperty(name, func)

    return decorator


def require_authentication(func: Callable[Concatenate[C, B], T]) -> Callable[Concatenate[C, B], T]:
    """A decorator to assure the `self` parameter of decorated methods has authentication set."""

    @wraps(func)
    def wrapper(item: C, *args: B.args, **kwargs: B.kwargs) -> T:
        if not item._http._authenticated:
            raise AuthenticationRequired("This method requires you to be authenticated to the API.")

        return func(item, *args, **kwargs)

    return wrapper


def deprecated(
    alternate: Optional[str] = None,
) -> Callable[[Callable[B, T]], Callable[B, T]]:
    """A decorator to mark a method as deprecated.

    Parameters
    -----------
    alternate: Optional[:class:`str`]
        The alternate method to use.
    """

    def decorator(func: Callable[B, T]) -> Callable[B, T]:
        @wraps(func)
        def wrapper(*args: B.args, **kwargs: B.kwargs) -> T:
            warnings.simplefilter("always", DeprecationWarning)  # turn off filter.
            if alternate is not None:
                fmt = "{0.__qualname__} is deprecated, use {1} instead."
            else:
                fmt = "{0.__qualname__} is deprecated."

            warnings.warn(fmt.format(func, alternate), stacklevel=2, category=DeprecationWarning)

            warnings.simplefilter("default", DeprecationWarning)  # reset filter.
            return func(*args, **kwargs)

        return wrapper

    return decorator


def calculate_limits(limit: int, offset: int, *, max_limit: int = 100) -> tuple[int, int]:
    """A helper function that will calculate the offset and limit parameters for API endpoints.

    Parameters
    -----------
    limit: :class:`int`
        The limit (or amount) of objects you are requesting.
    offset: :class:`int`
        The offset (or pagination start point) for the objects you are requesting.
    max_limit: :class:`int`
        The maximum limit value for the API Endpoint.

    Returns
    --------
    Tuple[:class:`int`, :class:`int`]
    """
    if offset >= MAX_DEPTH:
        raise ValueError(f"An offset of {MAX_DEPTH} will not return results.")

    offset = max(offset, 0)

    difference = MAX_DEPTH - offset
    if difference <= max_limit:
        new_limit = difference
        new_offset = MAX_DEPTH - new_limit
        return new_limit, new_offset

    new_limit = min(max(0, limit), max_limit)
    new_offset = min(max(0, offset), MAX_DEPTH - new_limit)

    return new_limit, new_offset


if HAS_ORJSON is True:

    def to_json(obj: Any) -> str:
        """A quick method that dumps a Python type to JSON object."""
        return orjson.dumps(obj).decode("utf-8")

    _from_json = orjson.loads  # type: ignore # this is guarded in an if.

else:

    def to_json(obj: Any) -> str:
        """A quick method that dumps a Python type to JSON object."""
        return json.dumps(obj, separators=(",", ":"), ensure_ascii=True)

    _from_json = json.loads


async def json_or_text(response: aiohttp.ClientResponse) -> Union[dict[str, Any], str]:
    """A quick method to parse a `aiohttp.ClientResponse` and test if it's json or text."""
    text = await response.text(encoding="utf-8")
    try:
        if response.headers["content-type"] == "application/json":
            try:
                return _from_json(text)
            except json.JSONDecodeError:
                pass
    except KeyError:
        pass

    return text


def php_query_builder(obj: Mapping[str, Optional[Union[str, int, bool, list[str], dict[str, str]]]]) -> str:
    """
    {"order": {"publishAt": "desc"}, "translatedLanguages": ["en", "jp"]}
    ->
    "order[publishAt]=desc&translatedLanguages[]=en&translatedLanguages[]=jp"
    """
    fmt = []
    for key, value in obj.items():
        if value is None:
            fmt.append(f"{key}=null")
        elif isinstance(value, bool):
            fmt.append(f"{key}={str(value).lower()}")
        elif isinstance(value, (str, int)):
            fmt.append(f"{key}={value}")
        elif isinstance(value, list):
            fmt.extend(f"{key}[]={item}" for item in value)
        elif isinstance(value, dict):
            fmt.extend(f"{key}[{subkey}]={subvalue}" for subkey, subvalue in value.items())

    return "&".join(fmt)


def get_image_mime_type(data: bytes) -> str:
    """Returns the image type from the first few bytes."""
    if data.startswith(b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"):
        return "image/png"
    elif data[:3] == b"\xff\xd8\xff" or data[6:10] in (b"JFIF", b"Exif"):
        return "image/jpeg"
    elif data.startswith((b"\x47\x49\x46\x38\x37\x61", b"\x47\x49\x46\x38\x39\x61")):
        return "image/gif"
    elif data.startswith(b"RIFF") and data[8:12] == b"WEBP":
        return "image/webp"
    else:
        raise ValueError("Unsupported image type given")


def to_snake_case(string: str) -> str:
    """Quick function to return snake_case from camelCase."""
    fmt: list[str] = []
    for character in string:
        if character.isupper():
            fmt.append(f"_{character.lower()}")
            continue
        fmt.append(character)
    return "".join(fmt)


def to_camel_case(string: str) -> str:
    """Quick function to return camelCase from snake_case."""
    first, *rest = string.split("_")
    chunks = [first.lower(), *map(str.capitalize, rest)]

    return "".join(chunks)


def as_chunks(iterator: Iterable[T], max_size: int) -> Iterable[list[T]]:
    ret = []
    n = 0
    for item in iterator:
        ret.append(item)
        n += 1
        if n == max_size:
            yield ret
            ret = []
            n = 0
    if ret:
        yield ret


def delta_to_iso(delta: datetime.timedelta) -> str:
    seconds = round(delta.total_seconds())
    weeks, seconds = divmod(seconds, 60 * 60 * 24 * 7)
    days, seconds = divmod(seconds, 60 * 60 * 24)
    hours, seconds = divmod(seconds, 60 * 60)
    minutes, seconds = divmod(seconds, 60)

    builder = "P" * bool(weeks or days)
    if days:
        builder += f"{days}D"
    if weeks:
        builder += f"{weeks}W"

    builder += "T" * bool(hours or minutes or seconds)

    if hours:
        builder += f"{hours}H"
    if minutes:
        builder += f"{minutes}M"
    if seconds:
        builder += f"{seconds}S"

    return builder


def iso_to_delta(iso: str) -> datetime.timedelta:

    if (match := MANGADEX_TIME_REGEX.fullmatch(iso)) is None:
        raise TypeError("The passed string does not match the regex pattern.")

    match_dict = match.groupdict()

    times: dict[str, float] = {key: float(value) for key, value in match_dict.items() if value}
    return datetime.timedelta(**times)


RT = TypeVar("RT", bound="RelationshipResponse", covariant=True)
RelType = Literal["manga", "cover_art", "scanlation_group", "artist", "author", "user", "leader", "member"]


@overload
def relationship_finder(relationships: list[RT], relationship_type: RelType, *, limit: Literal[1]) -> Optional[RT]:
    ...


@overload
def relationship_finder(relationships: list[RT], relationship_type: RelType, *, limit: Optional[int]) -> list[RT]:
    ...


def relationship_finder(
    relationships: list[RT], relationship_type: RelType, *, limit: Optional[int] = None
) -> Optional[Union[list[RT], RT]]:
    ret: list[RT] = []

    for relationship in relationships:
        if relationship["type"] == relationship_type:
            if limit == 1:
                return relationship
            else:
                ret.append(relationship)

    if limit is not None:
        return ret[:limit]

    if limit == 1:
        return

    return ret


def clean_isoformat(dt: datetime.datetime) -> str:
    if dt.tzinfo != datetime.timezone.utc:
        dt = dt.astimezone(datetime.timezone.utc)

    if dt.tzinfo is not None:
        dt = dt.replace(tzinfo=None)

    return dt.isoformat(timespec="seconds")


_path: pathlib.Path = _PROJECT_DIR.parent / "extras" / "tags.json"
with open(_path, "r") as _fp:
    MANGA_TAGS: dict[str, str] = json.load(_fp)


def __build_tags():  # type: ignore  # This is for pre-flight release usage only.
    async def build():
        from . import Client

        client = Client()
        tags = await client.update_tags()

        _diff_a = set(map(str.lower, MANGA_TAGS))
        _diff_b = set(map(str.lower, tags))

        if diff := (_diff_b ^ _diff_a):
            print(f"Tags have changed: {', '.join(diff)}")
        else:
            print("No tag changes.")

        await client.close()

    asyncio.run(build())

The MIT License (MIT)  Copyright (c) 2021-Present AbstractUmbra  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ``r"^(P([1-9]|[1-9][0-9])D)?(P?([1-9])W)?(P?T(([1-9]|1[0-9]|2[0-4])H)?(([1-9]|[1-5][0-9]|60)M)?(([1-9]|[1-5][0-9]|60)S)?)?$"``  This `regex pattern <https://docs.python.org/3/library/re.html#re-objects>`_ follows the ISO-8601 standard (MangaDex uses `PHP DateInterval <https://www.php.net/manual/en/dateinterval.construct.php>`_). The pattern *is* usable but more meant as a guideline for your formatting.  It matches some things like: ``P1D2W`` (1 day, two weeks), ``P1D2WT3H4M`` (1 day, 2 weeks, 3 hours and 4 minutes) A helper class for instantiating a HTTP method to MangaDex.      Parameters     -----------     verb: :class:`str`         The HTTP verb you wish to perform, e.g. ``"POST"``     path: :class:`str`         The prepended path to the API endpoint you with to target.         e.g. ``"/manga/{manga_id}"``     parameters: Any         This is a special cased kwargs. Anything passed to these will substitute it's key to value in the `path`.         E.g. if your `path` is ``"/manga/{manga_id}"``, and your parameters are ``manga_id="..."``, then it will expand into the path         making ``"manga/..."`` A helper class for instantiating a HTTP method to download from MangaDex.      Parameters     -----------     verb: :class:`str`         The HTTP verb you wish to perform. E.g. ``"POST"``     base: :class:`str`         The base URL for the download path.     path: :class:`str`         The prepended path to the API endpoint you with to target.         e.g. ``"/manga/{manga_id}"``     parameters: Any         This is a special cased kwargs. Anything passed to these will substitute it's key to value in the `path`.         E.g. if your `path` is ``"/manga/{manga_id}"``, and your parameters are ``manga_id="..."``, then it will expand into the path         making ``"manga/..."`` # type: ignore # we import this as needed. ## This class and subsequent decorator have been copied from Rapptz' Discord.py ## (https://github.com/Rapptz/discord.py) ## Credit goes to Rapptz and contributors A decorator to assure the `self` parameter of decorated methods has authentication set. A decorator to mark a method as deprecated.      Parameters     -----------     alternate: Optional[:class:`str`]         The alternate method to use. # turn off filter. # reset filter. A helper function that will calculate the offset and limit parameters for API endpoints.      Parameters     -----------     limit: :class:`int`         The limit (or amount) of objects you are requesting.     offset: :class:`int`         The offset (or pagination start point) for the objects you are requesting.     max_limit: :class:`int`         The maximum limit value for the API Endpoint.      Returns     --------     Tuple[:class:`int`, :class:`int`] A quick method that dumps a Python type to JSON object. # type: ignore # this is guarded in an if. A quick method that dumps a Python type to JSON object. A quick method to parse a `aiohttp.ClientResponse` and test if it's json or text. {"order": {"publishAt": "desc"}, "translatedLanguages": ["en", "jp"]}     ->     "order[publishAt]=desc&translatedLanguages[]=en&translatedLanguages[]=jp" Returns the image type from the first few bytes. Quick function to return snake_case from camelCase. Quick function to return camelCase from snake_case. # type: ignore  # This is for pre-flight release usage only.