const colors = new Map();
colors.set("a", "#55ff55");
colors.set("b", "#55ffff");
colors.set("c", "#ff5555");
colors.set("d", "#ff55ff");
colors.set("e", "#ffff55");
colors.set("f", "#ffffff");
colors.set("g", "#ddd605");
colors.set("0", "#000000");
colors.set("1", "#0000aa");
colors.set("2", "#00aa00");
colors.set("3", "#00aaaa");
colors.set("4", "#aa0000");
colors.set("5", "#aa00aa");
colors.set("6", "#ffaa00");
colors.set("7", "#aaaaaa");
colors.set("8", "#555555");
colors.set("9", "#5555ff");
export function convertMinecraftColorToSpan(text: string) {
    if (!text) return text;
    let result = "";
    let color = "currentColor";
    let bold = false;
    let italic = false;
    let underline = false;
    let strikethrough = false;
    let obfuscated = false;
    let currentSpan = "";
    for (let i = 0; i < text.length; i++) {
        // Optional & support
        if (text[i] == "§" || text[i] == "&") {
            if (i + 1 < text.length) {
                let c = text[i + 1];
                if (currentSpan) {
                    result += `<span style="color: ${color};`
                    if (bold) result += "font-weight: bold;"
                    if (italic) result += "font-style: italic;"
                    if (underline) result += "text-decoration: underline;"
                    if (strikethrough) result += "text-decoration: line-through;"
                    if (obfuscated) result += "text-shadow: 0 0 0.5em white;"
                    result += `">${currentSpan}</span>`;
                    
                    currentSpan = "";
                }
                if (c == "r") {
                    color = "currentColor";
                    bold = false;
                    italic = false;
                    underline = false;
                    strikethrough = false;
                    obfuscated = false;
                } else if (c == "l") {
                    bold = true;
                } else if (c == "o") {
                    italic = true;
                } else if (c == "n") {
                    underline = true;
                } else if (c == "m") {
                    strikethrough = true;
                } else if (c == "k") {
                    obfuscated = true;
                } else if (colors.has(c)) {
                    const newColor = colors.get(c);
                    color = newColor;
                }
                i++;
            }
        } else if (text[i] == "\n") {
            currentSpan += "<br>";
        } else {
            currentSpan += text[i];
        }
    }
    if (currentSpan) {
        result += `<span style="color: ${color};`
        if (bold) result += "font-weight: bold;"
        if (italic) result += "font-style: italic;"
        if (underline) result += "text-decoration: underline;"
        if (strikethrough) result += "text-decoration: line-through;"
        if (obfuscated) result += "text-shadow: 0 0 0.5em white;"
        result += `">${currentSpan}</span>`;
    }
    return result;
}