# We only want to normalize urls with an HTTP(S) scheme.
# urllib3 infers URLs without a scheme (None) to be http.

# Almost all of these patterns were derived from the
# 'rfc3986' module: https://github.com/python-hyper/rfc3986

"""
    Data structure for representing an HTTP URL. Used as a return value for
    :func:`parse_url`. Both the scheme and host are normalized as they are
    both case-insensitive according to RFC 3986.
    """

"""For backwards-compatibility with urlparse. We're nice like that."""

"""
        Authority component as defined in RFC 3986 3.2.
        This includes userinfo (auth), host and port.

        i.e.
            userinfo@host:port
        """

"""
        Network location including host and port.

        If you need the equivalent of urllib.parse's ``netloc``,
        use the ``authority`` property instead.
        """

"""
        Convert self into a url

        This function should more or less round-trip with :func:`.parse_url`. The
        returned url may not be exactly the same as the url inputted to
        :func:`.parse_url`, but it should be equivalent by the RFC (e.g., urls
        with a blank port will have : removed).

        Example:

        .. code-block:: python

            import urllib3

            U = urllib3.util.parse_url("https://google.com/mail/")

            print(U.url)
            # "https://google.com/mail/"

            print( urllib3.util.Url("https", "username:password",
                                    "host.com", 80, "/path", "query", "fragment"
                                    ).url
                )
            # "https://username:password@host.com:80/path?query#fragment"
        """

        # We use "is not None" we want things to happen with empty strings (or 0 port)
        
"""Percent-encodes a URI component without reapplying
    onto an already percent-encoded component.
    """

    # Normalize existing percent-encoded bytes.
    # Try to see if the component we're encoding is already percent-encoded
    # so we can skip all '%' characters but still encode all others.
    
    # See http://tools.ietf.org/html/rfc3986#section-5.2.4 for pseudo-code
    
        # '.' is the current directory, so ignore it, it is superfluous
        
        # Anything other than '..', should be appended to the output
        
        # In this case segment == '..', if we can, we should pop the last
        # element
        
    # If the path starts with '/' and the output is empty or the first string
    # is non-empty
    
    # If the path starts with '/.' or '/..' ensure we add one more empty
    # string to add a trailing '/'
    
                # IPv6 hosts of the form 'a::b%zone' are encoded in a URL as
                # such per RFC 6874: 'a::b%25zone'. Unquote the ZoneID
                # separator as necessary to return a valid RFC 4007 scoped IP.
                
"""Percent-encodes a request target so that there are no invalid characters

    Pre-condition for this function is that 'target' must start with '/'.
    If that is the case then _TARGET_RE will always produce a match.
    """

"""
    Given a url, return a parsed :class:`.Url` namedtuple. Best-effort is
    performed to parse incomplete urls. Fields not provided will be None.
    This parser is RFC 3986 and RFC 6874 compliant.

    The parser logic and helper functions are based heavily on
    work done in the ``rfc3986`` module.

    :param str url: URL to parse into a :class:`.Url` namedtuple.

    Partly backwards-compatible with :mod:`urllib.parse`.

    Example:

    .. code-block:: python

        import urllib3

        print( urllib3.util.parse_url('http://google.com/mail/'))
        # Url(scheme='http', host='google.com', port=None, path='/mail/', ...)

        print( urllib3.util.parse_url('google.com:80'))
        # Url(scheme=None, host='google.com', port=80, path=None, ...)

        print( urllib3.util.parse_url('/foo?bar'))
        # Url(scheme=None, host=None, port=None, path='/foo', query='bar', ...)
    """

    # For the sake of backwards compatibility we put empty
    # string values for path if there are any defined values
    # beyond the path in the URL.
    # TODO: Remove this when we break backwards compatibility.
    
_LS32_PAT = "(?:{hex}:{hex}|{ipv4})".format(hex=_HEX_PAT, ipv4=_IPV4_PAT)

_UNRESERVED_PAT = r"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._\-~"

_IPV6_PAT = "(?:" + "|".join([x % _subs for x in _variations]) + ")"

_ZONE_ID_PAT = "(?:%25|%)(?:[" + _UNRESERVED_PAT + "]|%[a-fA-F0-9]{2})+"

_IPV6_ADDRZ_PAT = r"\[" + _IPV6_PAT + r"(?:" + _ZONE_ID_PAT + r")?\]"

_BRACELESS_IPV6_ADDRZ_RE = re.compile("^" + _IPV6_ADDRZ_PAT[2:-2] + "$")

_HOST_PORT_PAT = ("^(%s|%s|%s)(?::0*?(|0|[1-9][0-9]{0,4}))?$") % (

_HOST_PORT_RE = re.compile(_HOST_PORT_PAT, re.UNICODE | re.DOTALL)

    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-~"

        return super().__new__(cls, scheme, auth, host, port, path, query, fragment)

        """For backwards-compatibility with urlparse. We're nice like that."""

    component: str | None, allowed_chars: typing.Container[str]

    is_percent_encoded = percent_encodings == uri_bytes.count(b"%")

        encoded_component.extend(b"%" + (hex(byte_ord)[2:].encode().zfill(2).upper()))

    segments = path.split("/")  # Turn the path into a list of segments

    output = []  # Initialize the variable to use to store output

def _normalize_host(host: str | None, scheme: str | None) -> str | None:

                    if zone_id.startswith("%25") and zone_id != "%25":

                    zone_id = _encode_invalid_chars(zone_id, _UNRESERVED_CHARS)

                    return f"{host[:start].lower()}%{zone_id}{host[end:]}"

                    b".".join([_idna_encode(label) for label in host.split(".")]),

                "Unable to parse URL without the 'idna' module"

            return idna.encode(name.lower(), strict=True, std3_rules=True)

    """Percent-encodes a request target so that there are no invalid characters

        raise LocationParseError(f"{target!r} is not a valid request URI")

        scheme, authority, path, query, fragment = _URI_RE.match(url).groups()  # type: ignore[union-attr]

        normalize_uri = scheme is None or scheme.lower() in _NORMALIZABLE_SCHEMES

            host, port = _HOST_PORT_RE.match(host_port).groups()  # type: ignore[union-attr]

                auth = _encode_invalid_chars(auth, _USERINFO_CHARS)

            fragment = _encode_invalid_chars(fragment, _FRAGMENT_CHARS)