from __future__ import annotations


def _get_text(value) -> str:
    if value is None:
        return ""

    return str(value).strip().lower()


def check_is_cod(method, option=None) -> bool:
    """
    تشخیص روش ارسال پس‌کرایه.

    این تابع نام روش ارسال و option آن را بررسی می‌کند.
    """

    method_text = " ".join(
        [
            _get_text(getattr(method, "name", None)),
            _get_text(getattr(method, "title", None)),
            _get_text(getattr(method, "code", None)),
            _get_text(getattr(method, "slug", None)),
        ]
    )

    option_text = ""

    if option is not None:
        option_text = " ".join(
            [
                _get_text(getattr(option, "name", None)),
                _get_text(getattr(option, "title", None)),
                _get_text(getattr(option, "code", None)),
                _get_text(getattr(option, "slug", None)),
            ]
        )

    text = f"{method_text} {option_text}"

    cod_keywords = (
        "پس کرایه",
        "پس‌کرایه",
        "پس-کرایه",
        "کرایه در مقصد",
        "پرداخت در مقصد",
        "cod",
        "cash on delivery",
        "collect on delivery",
    )

    return any(keyword in text for keyword in cod_keywords)
