# Copyright (c) 2021 Felix Fontein
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
# Do not use this from other collections or standalone plugins/modules!

from __future__ import annotations

import re
import typing as t

from ansible.module_utils.common.text.converters import to_text

if t.TYPE_CHECKING:
    from collections.abc import Sequence  # pragma: no cover

_ASCII_PRINTABLE_MATCHER = re.compile(r"^[\x20-\x7e]*$")


def is_ascii_label(domain: str) -> bool:
    """
    Check whether domain name has only ASCII labels.
    """
    return _ASCII_PRINTABLE_MATCHER.match(domain) is not None


class InvalidDomainName(Exception):
    """
    The provided domain name is not valid.
    """


def split_into_labels(domain: str) -> tuple[list[str], t.Literal["", "."]]:
    """
    Split domain name to a list of labels. Start with the top-most label.

    Returns a list of labels and a tail, which is either ``''`` or ``'.'``.
    Raises ``InvalidDomainName`` if the domain name is not valid.
    """
    result = []
    index = len(domain)
    tail: t.Literal["", "."] = ""
    if domain.endswith("."):
        index -= 1
        tail = "."
    if index > 0:
        while index >= 0:
            next_index = domain.rfind(".", 0, index)
            label = domain[next_index + 1 : index]
            if label == "" or label[0] == "-" or label[-1] == "-" or len(label) > 63:
                raise InvalidDomainName(domain)
            result.append(label)
            index = next_index
    return result, tail


def join_labels(labels: Sequence[str], tail: str = "") -> str:
    """
    Combines the result of split_into_labels() back into a domain name.
    """
    return ".".join(reversed(labels)) + tail


def normalize_label(label: str) -> str:
    """
    Normalize a domain label. Returns a lower-case ASCII label.

    If a ulabel is provided, it is converted to an alabel.
    """
    if not is_ascii_label(label):
        # Convert ulabel to alabel
        label = to_text(b"xn--" + to_text(label).encode("punycode"))
    # Always convert to lower-case
    return label.lower()
