import re

from oscar.core.decorators import deprecated
from oscar.core.loading import get_model

from ocyan.plugin.oscar.utils import set_user_properties

COUNTRY_REGEX = re.compile(r"countries/(?P<country_code>\w+)/$")

PaymentMethod = get_model("checkout", "PaymentMethod")


def country_from_url(url):
    """
    Parse a url for the country code

    >>> country_from_url("klaas")
    >>> country_from_url("http://example.com/countries/YUBA/")
    'YUBA'
    >>> country_from_url("http://example.com/countries/YUBA/Lu")
    >>> country_from_url("http://example.com/countries/nl")
    >>> country_from_url("http://example.com/countries/nl/")
    'nl'
    """
    country = COUNTRY_REGEX.search(url)
    if country is not None:
        return country.group("country_code")

    return None


set_user_properties = deprecated(set_user_properties)
