# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Preprocessing functions for ULMFiT."""

from __future__ import annotations

import html
import re
from typing import TYPE_CHECKING, Optional

if TYPE_CHECKING:
    from collections.abc import Collection

import emoji

_TK_UNK: str = "xxunk"
_TK_REP: str = "xxrep"
_TK_WREP: str = "xxwrep"
_TK_END: str = "xxend"
_TK_URL: str = "xxurl"


def replace_url(text: str) -> str:
    """
    Replace URLs in text with the **xxurl** token.

    :param str text: text to be processed

    :return: text with URLs replaced
    :rtype: str

    :Example:

        >>> from pythainlp.ulmfit import replace_url
        >>> replace_url("go to github.com")
        'go to xxurl'
    """
    URL_PATTERN = r"""(?i)\b((?:https?:(?:/{1,3}|[a-z0-9%])|[a-z0-9.\-]+[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)/)(?:[^\s()<>{}\[\]]|\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\))+(?:\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])|(?:(?<!@)[a-z0-9]+(?:[.\-][a-z0-9]+)*[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)\b/?(?!@)))"""
    return re.sub(URL_PATTERN, _TK_URL, text)


def fix_html(text: str) -> str:
    """
    Replace HTML strings in text (code from `fastai`).

    :param str text: text to be processed

    :return: text with HTML strings replaced
    :rtype: str

    :Example:

        >>> from pythainlp.ulmfit import fix_html
        >>> fix_html("Anbsp;amp;nbsp;B @.@ ")
        'A & B.'
    """
    re1 = re.compile(r"  +")
    text = (
        text.replace("#39;", "'")
        .replace("amp;", "&")
        .replace("#146;", "'")
        .replace("nbsp;", " ")
        .replace("#36;", "$")
        .replace("\\n", "\n")
        .replace("quot;", "'")
        .replace("<br />", "\n")
        .replace('\\"', '"')
        .replace("<unk>", _TK_UNK)
        .replace(" @.@ ", ".")
        .replace(" @-@ ", "-")
        .replace(" @,@ ", ",")
        .replace("\\", " \\ ")
    )
    return re1.sub(" ", html.unescape(text))


def rm_useless_spaces(text: str) -> str:
    """Collapse repeated spaces in text (code from `fastai`)."""
    return re.sub(" {2,}", " ", text)


def spec_add_spaces(text: str) -> str:
    """Add spaces around slashes, hash signs, and newlines in text."""
    return re.sub(r"([/#\n])", r" \1 ", text)


def replace_rep_after(text: str) -> str:
    """
    Replace character repetitions in text, after the repeated character.

    This prevents 'น้อยยยยยยยย' from becoming 'น้อ xxrep 8 ย'.
    Instead, the word stays as 'น้อย xxrep 8'.

    :param str text: text to be processed

    :return: text with the **xxrep** token and the counter
        after the repeated character
    :rtype: str

    :Example:

        >>> from pythainlp.ulmfit import replace_rep_after
        >>>
        >>> text = "กาาาาาาา"
        >>> replace_rep_after(text)
        'กาxxrep7 '
    """

    def _replace_rep(m: re.Match[str]) -> str:
        c, cc = m.groups()
        return f"{c}{_TK_REP}{len(cc) + 1} "

    re_rep = re.compile(r"(\S)(\1{3,})")

    return re_rep.sub(_replace_rep, text)


def replace_wrep_post(toks: Collection[str]) -> list[str]:
    """
    Replace repeated words after tokenization.

    The `replace_wrep` function of `fastai` does not work well with Thai.

    :param Collection[str] toks: list of words

    :return: list of words with the **xxwrep** token and the counter
        added before repeated words
    :rtype: list[str]

    :Example:

        >>> from pythainlp.ulmfit import replace_wrep_post_nonum
        >>>
        >>> toks = ["กา", "น้ำ", "น้ำ", "น้ำ", "น้ำ"]
        >>> replace_wrep_post(toks)
        ['กา', 'xxwrep', '3', 'น้ำ']

    """
    previous_word: Optional[str] = None
    rep_count = 0
    res: list[Optional[str]] = []
    for current_word in [*list(toks), _TK_END]:
        if current_word == previous_word:
            rep_count += 1
        elif (current_word != previous_word) & (rep_count > 0):
            res += [_TK_WREP, str(rep_count), previous_word]
            rep_count = 0
        else:
            res.append(previous_word)
        previous_word = current_word
    return list(filter(None, res[1:]))


def rm_useless_newlines(text: str) -> str:
    """Replace repeated newlines in text with a space."""
    return re.sub(r"[\n]{2,}", " ", text)


def rm_brackets(text: str) -> str:
    """Remove empty brackets and artifacts within brackets from text."""
    # remove empty brackets
    new_line = re.sub(r"\(\)", "", text)
    new_line = re.sub(r"\{\}", "", new_line)
    new_line = re.sub(r"\[\]", "", new_line)
    # brackets with only punctuation marks
    new_line = re.sub(r"\([^a-zA-Z0-9ก-๙]+\)", "", new_line)
    new_line = re.sub(r"\{[^a-zA-Z0-9ก-๙]+\}", "", new_line)
    new_line = re.sub(r"\[[^a-zA-Z0-9ก-๙]+\]", "", new_line)
    # artifacts after (
    new_line = re.sub(
        r"(?<=\()[^a-zA-Z0-9ก-๙]+(?=[a-zA-Z0-9ก-๙])", "", new_line
    )
    new_line = re.sub(
        r"(?<=\{)[^a-zA-Z0-9ก-๙]+(?=[a-zA-Z0-9ก-๙])", "", new_line
    )
    new_line = re.sub(
        r"(?<=\[)[^a-zA-Z0-9ก-๙]+(?=[a-zA-Z0-9ก-๙])", "", new_line
    )
    # artifacts before )
    new_line = re.sub(
        r"(?<=[a-zA-Z0-9ก-๙])[^a-zA-Z0-9ก-๙]+(?=\))", "", new_line
    )
    new_line = re.sub(
        r"(?<=[a-zA-Z0-9ก-๙])[^a-zA-Z0-9ก-๙]+(?=\})", "", new_line
    )
    new_line = re.sub(
        r"(?<=[a-zA-Z0-9ก-๙])[^a-zA-Z0-9ก-๙]+(?=\])", "", new_line
    )
    return new_line


def ungroup_emoji(toks: Collection[str]) -> list[str]:
    """
    Ungroup emojis joined by Zero Width Joiner (ZWJ).

    See https://emojipedia.org/emoji-zwj-sequence/

    :param Collection[str] toks: list of words
    :return: list of words with ZWJ emoji sequences split
    :rtype: list[str]
    """
    res = []
    for tok in toks:
        if emoji.emoji_count(tok) == len(tok):
            res.extend(list(tok))
        else:
            res.append(tok)
    return res


def lowercase_all(toks: Collection[str]) -> list[str]:
    """
    Lowercase all English words.

    English words in Thai text usually do not carry capitalization nuances.

    :param Collection[str] toks: list of words
    :return: list of lowercased words
    :rtype: list[str]
    """
    return list(map(str.lower, toks))


def replace_rep_nonum(text: str) -> str:
    """
    Replace character repetitions in text, without a counter.

    This prevents 'น้อยยยยยยยย' from becoming 'น้อ xxrep ย'.
    Instead, the word stays as 'น้อย xxrep '.

    :param str text: text to be processed

    :return: text with the **xxrep** token after the repeated character
    :rtype: str

    :Example:

        >>> from pythainlp.ulmfit import replace_rep_nonum
        >>>
        >>> text = "กาาาาาาา"
        >>> replace_rep_nonum(text)
        'กา xxrep '

    """

    def _replace_rep(m: re.Match[str]) -> str:
        c, _ = m.groups()
        return f"{c} {_TK_REP} "

    re_rep = re.compile(r"(\S)(\1{3,})")
    return re_rep.sub(_replace_rep, text)


def replace_wrep_post_nonum(toks: Collection[str]) -> list[str]:
    """
    Replace repeated words after tokenization, without a counter.

    The `replace_wrep` function of `fastai` does not work well with Thai.

    :param Collection[str] toks: list of words

    :return: list of words with the **xxwrep** token added before
        repeated words
    :rtype: list[str]

    :Example:

        >>> from pythainlp.ulmfit import replace_wrep_post_nonum
        >>>
        >>> toks = ["กา", "น้ำ", "น้ำ", "น้ำ", "น้ำ"]
        >>> replace_wrep_post_nonum(toks)
        ['กา', 'xxwrep', 'น้ำ']

    """
    previous_word: Optional[str] = None
    rep_count = 0
    res: list[Optional[str]] = []
    for current_word in [*list(toks), _TK_END]:
        if current_word == previous_word:
            rep_count += 1
        elif (current_word != previous_word) & (rep_count > 0):
            res += [_TK_WREP, previous_word]
            rep_count = 0
        else:
            res.append(previous_word)
        previous_word = current_word
    return list(filter(None, res[1:]))


def remove_space(toks: Collection[str]) -> list[str]:
    """
    Remove spaces from a list of words, for bag-of-words models.

    :param Collection[str] toks: list of words

    :return: list of words with space tokens (" ") filtered out
    :rtype: list[str]
    """
    res = []
    for t in toks:
        stripped = t.strip()
        if stripped:
            res.append(stripped)
    return res
