# See https://en.wikipedia.org/wiki/Web_colors#X11_color_names
def build_numbered_variants(
    translated_color_name: str,
    original_color_name: str,
    start_index: int,
    end_index: int,
) -> dict[str, str]:
    """
    Builds a dictionary of numbered variants of a color (inclusive of
    start_index and end_index). For example,

    build_numbered_variants("rojo", "red", 1, 4) will return:
    {
        "rojo1": "red1",
        "rojo2": "red2",
        "rojo3": "red3",
        "rojo4": "red4",
    }
    """
    return {
        f"{translated_color_name}{i}": f"{original_color_name}{i}"
        for i in range(start_index, end_index + 1)
    }


TRANSLATED_X11_COLORS = {
    "transparente": "transparent",
    "rojovioletamedio": "mediumvioletred",
    "rosaoscuro": "deeppink",
    **build_numbered_variants("rosaoscuro", "deeppink", 1, 4),
    "rojovioleta": "violetred",
    **build_numbered_variants("rojovioleta", "violetred", 1, 4),
    "rojovioletapalido": "palevioletred",
    **build_numbered_variants("rojovioletapalido", "palevioletred", 1, 4),
    "rosafuerte": "hotpink",
    **build_numbered_variants("rosafuerte", "hotpink", 1, 4),
    "rosachillon": "hotpink",
    **build_numbered_variants("rosachillon", "hotpink", 1, 4),
    "rosaclaro": "lightpink",
    **build_numbered_variants("rosaclaro", "lightpink", 1, 4),
    "rosado": "pink",
    **build_numbered_variants("rosado", "pink", 1, 4),
    "rosa": "pink",
    **build_numbered_variants("rosa", "pink", 1, 4),
    "rojooscuro": "darkred",
    "rojo": "red",
    **build_numbered_variants("rojo", "red", 1, 4),
    "rojoladrillo": "firebrick",
    **build_numbered_variants("rojoladrillo", "firebrick", 1, 4),
    "carmesi": "crimson",
    "rojoindio": "indianred",
    **build_numbered_variants("rojoindio", "indianred", 1, 4),
    "coralclaro": "lightcoral",
    "salmon": "salmon",
    **build_numbered_variants("salmon", "salmon", 1, 4),
    "salmonoscuro": "darksalmon",
    "salmonclaro": "lightsalmon",
    **build_numbered_variants("salmonclaro", "lightsalmon", 1, 4),
    "rojoanaranjado": "orangered",
    **build_numbered_variants("rojoanaranjado", "orangered", 1, 4),
    "tomate": "tomato",
    **build_numbered_variants("tomate", "tomato", 1, 4),
    "naranjaoscuro": "darkorange",
    **build_numbered_variants("naranjaoscuro", "darkorange", 1, 4),
    "coral": "coral",
    **build_numbered_variants("coral", "coral", 1, 4),
    "naranja": "orange",
    **build_numbered_variants("naranja", "orange", 1, 4),
    "caquioscuro": "darkkhaki",
    "dorado": "gold",
    **build_numbered_variants("dorado", "gold", 1, 4),
    "caqui": "khaki",
    **build_numbered_variants("caqui", "khaki", 1, 4),
    "durazno": "peachpuff",
    **build_numbered_variants("durazno", "peachpuff", 1, 4),
    "amarillo": "yellow",
    **build_numbered_variants("amarillo", "yellow", 1, 4),
    "varadeoropalido": "palegoldenrod",
    "mocasin": "moccasin",
    "papaya": "papayawhip",
    "batidodepapaya": "papayawhip",
    "varadeoroclaro": "lightgoldenrodyellow",
    "varadeclaro": "goldenrodyellow",
    **build_numbered_variants("varadeclaro", "goldenrodyellow", 1, 4),
    "gasalimon": "lemonchiffon",
    **build_numbered_variants("gasalimon", "lemonchiffon", 1, 4),
    "amarilloclaro": "lightyellow",
    **build_numbered_variants("amarilloclaro", "lightyellow", 1, 4),
    "granate": "maroon",
    **build_numbered_variants("granate", "maroon", 1, 4),
    "marron": "brown",
    **build_numbered_variants("marron", "brown", 1, 4),
    "marronsilla": "saddlebrown",
    "siena": "sienna",
    **build_numbered_variants("siena", "sienna", 1, 4),
    "chocolate": "chocolate",
    **build_numbered_variants("chocolate", "chocolate", 1, 4),
    "varadeorooscuro": "darkgoldenrod",
    **build_numbered_variants("varadeorooscuro", "darkgoldenrod", 1, 4),
    "peru": "peru",
    "marronrosado": "rosybrown",
    **build_numbered_variants("marronrosado", "rosybrown", 1, 4),
    "varadeoro": "goldenrod",
    **build_numbered_variants("varadeoro", "goldenrod", 1, 4),
    "marronarena": "sandybrown",
    "canela": "tan",
    **build_numbered_variants("canela", "tan", 1, 4),
    "madera": "burlywood",
    **build_numbered_variants("madera", "burlywood", 1, 4),
    "trigo": "wheat",
    **build_numbered_variants("trigo", "wheat", 1, 4),
    "blanconavajo": "navajowhite",
    **build_numbered_variants("blanconavajo", "navajowhite", 1, 4),
    "bizcocho": "bisque",
    **build_numbered_variants("bizcocho", "bisque", 1, 4),
    "almendra": "blanchedalmond",
    "carnesil": "cornsilk",
    **build_numbered_variants("carnesil", "cornsilk", 1, 4),
    "indigo": "indigo",
    "purpura": "purple",
    **build_numbered_variants("purpura", "purple", 1, 4),
    "morado": "purple",
    **build_numbered_variants("morado", "purple", 1, 4),
    "magentaoscuro": "darkmagenta",
    "violetaoscuro": "darkviolet",
    "azulpizarraoscuro": "darkslateblue",
    "azulvioleta": "blueviolet",
    "orquideaoscura": "darkorchid",
    **build_numbered_variants("orquideaoscura", "darkorchid", 1, 4),
    "fucsia": "fuchsia",
    "magenta": "magenta",
    **build_numbered_variants("magenta", "magenta", 1, 4),
    "azulpizarra": "slateblue",
    **build_numbered_variants("azulpizarra", "slateblue", 1, 4),
    "azulpizarramedio": "mediumslateblue",
    "orquideamedio": "mediumorchid",
    **build_numbered_variants("orquideamedio", "mediumorchid", 1, 4),
    "moradomedio": "mediumpurple",
    **build_numbered_variants("moradomedio", "mediumpurple", 1, 4),
    "orquidea": "orchid",
    **build_numbered_variants("orquidea", "orchid", 1, 4),
    "violeta": "violet",
    "ciruela": "plum",
    **build_numbered_variants("ciruela", "plum", 1, 4),
    "cardo": "thistle",
    **build_numbered_variants("cardo", "thistle", 1, 4),
    "lavanda": "lavender",
    "azulmedianoche": "midnightblue",
    "marino": "navy",
    "azulmarino": "navy",
    "azuloscuro": "darkblue",
    "azulmedio": "mediumblue",
    "azul": "blue",
    **build_numbered_variants("azul", "blue", 1, 4),
    "azulreal": "royalblue",
    **build_numbered_variants("azulreal", "royalblue", 1, 4),
    "azulacero": "steelblue",
    **build_numbered_variants("azulacero", "steelblue", 1, 4),
    "azuldodger": "dodgerblue",
    **build_numbered_variants("azuldodger", "dodgerblue", 1, 4),
    "azulcelesteprofundo": "deepskyblue",
    **build_numbered_variants("azulcelesteprofundo", "deepskyblue", 1, 4),
    "azulaciano": "cornflowerblue",
    "aciano": "cornflowerblue",
    "azulceleste": "skyblue",
    **build_numbered_variants("azulceleste", "skyblue", 1, 4),
    "celeste": "skyblue",
    **build_numbered_variants("celeste", "skyblue", 1, 4),
    "azulcelesteclaro": "lightskyblue",
    **build_numbered_variants("azulcelesteclaro", "lightskyblue", 1, 4),
    "azulaceroclaro": "lightsteelblue",
    **build_numbered_variants("azulaceroclaro", "lightsteelblue", 1, 4),
    "azulclaro": "lightblue",
    **build_numbered_variants("azulclaro", "lightblue", 1, 4),
    "azulpolvo": "powderblue",
    "verdeazulado": "teal",
    "cianoscuro": "darkcyan",
    "verdemarclaro": "lightseagreen",
    "azulcadete": "cadetblue",
    **build_numbered_variants("azulcadete", "cadetblue", 1, 4),
    "turquesaoscuro": "darkturquoise",
    "turquesamedio": "mediumturquoise",
    "turquesa": "turquoise",
    **build_numbered_variants("turquesa", "turquoise", 1, 4),
    "agua": "aqua",
    "cian": "cyan",
    **build_numbered_variants("cian", "cyan", 1, 4),
    "aguamarina": "aquamarine",
    **build_numbered_variants("aguamarina", "aquamarine", 1, 4),
    "turquesapalido": "paleturquoise",
    **build_numbered_variants("turquesapalido", "paleturquoise", 1, 4),
    "cianclaro": "lightcyan",
    **build_numbered_variants("cianclaro", "lightcyan", 1, 4),
    "verdeoscuro": "darkgreen",
    "verde": "green",
    **build_numbered_variants("verde", "green", 1, 4),
    "verdeolivaoscuro": "darkolivegreen",
    **build_numbered_variants("verdeolivaoscuro", "darkolivegreen", 1, 4),
    "verdebosque": "forestgreen",
    "verdemar": "seagreen",
    **build_numbered_variants("verdemar", "seagreen", 1, 4),
    "oliva": "olive",
    "olivapalido": "olivedrab",
    **build_numbered_variants("olivapalido", "olivedrab", 1, 4),
    "verdemarmedio": "mediumseagreen",
    "verdelimon": "limegreen",
    "lima": "lime",
    "verdeprimavera": "springgreen",
    **build_numbered_variants("verdeprimavera", "springgreen", 1, 4),
    "verdeprimaveramedio": "mediumspringgreen",
    "verdemaroscuro": "darkseagreen",
    **build_numbered_variants("verdemaroscuro", "darkseagreen", 1, 4),
    "aquamarinamedia": "mediumaquamarine",
    "amarilloverde": "yellowgreen",
    "verdepasto": "lawngreen",
    "verdecesped": "lawngreen",
    "chartreuse": "chartreuse",
    **build_numbered_variants("chartreuse", "chartreuse", 1, 4),
    "verdeclaro": "lightgreen",
    "verdeamarillo": "greenyellow",
    "verdepalido": "palegreen",
    **build_numbered_variants("verdepalido", "palegreen", 1, 4),
    "rosanevado": "mistyrose",
    **build_numbered_variants("rosanevado", "mistyrose", 1, 4),
    "blancoantiguo": "antiquewhite",
    **build_numbered_variants("blancoantiguo", "antiquewhite", 1, 4),
    "lino": "linen",
    "beige": "beige",
    "humoblanco": "whitesmoke",
    "rosalavanda": "lavenderblush",
    **build_numbered_variants("rosalavanda", "lavenderblush", 1, 4),
    "encajeviejo": "oldlace",
    "azulalice": "aliceblue",
    "conchamarina": "seashell",
    **build_numbered_variants("conchamarina", "seashell", 1, 4),
    "blancofantasma": "ghostwhite",
    "melon": "honeydew",
    **build_numbered_variants("melon", "honeydew", 1, 4),
    "blancofloral": "floralwhite",
    "azur": "azure",
    **build_numbered_variants("azur", "azure", 1, 4),
    "cremadementa": "mintcream",
    "nieve": "snow",
    **build_numbered_variants("nieve", "snow", 1, 4),
    "marfil": "ivory",
    **build_numbered_variants("marfil", "ivory", 1, 4),
    "blanco": "white",
    "negro": "black",
    "grispizarraoscuro": "darkslategray",
    **build_numbered_variants("grispizarraoscuro", "darkslategray", 1, 4),
    "gristenue": "dimgray",
    "grispizarra": "slategray",
    **build_numbered_variants("grispizarra", "slategray", 1, 4),
    "gris": "gray",
    **build_numbered_variants("gris", "gray", 0, 100),
    "grispizarraclaro": "lightslategray",
    "grisoscuro": "darkgray",
    "plata": "silver",
    "plateado": "silver",
    "grisclaro": "lightgray",
    "gainsboro": "gainsboro",
}


def get_html_color(color_name: str | None) -> str | None:
    """
    Attempts to find a canonical HTML color name for the provided color name.

    Insensitive to accents, spaces, and capitalization. If the color name is not
    found (i.e. it is not some translation of an X11 color name) it returns the
    color name itself.

    Args:
        color_name: The color name to translate.

    Returns:
        The canonical HTML color name.
    """
    if color_name is None:
        return None
    color_name = color_name.lower()
    color_name = color_name.replace(" ", "")
    color_name = color_name.replace("á", "a")
    color_name = color_name.replace("é", "e")
    color_name = color_name.replace("í", "i")
    color_name = color_name.replace("ó", "o")
    color_name = color_name.replace("ú", "u")
    color_name = color_name.replace("ñ", "n")
    return TRANSLATED_X11_COLORS.get(color_name, color_name)
