/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  it.unimi.dsi.fastutil.objects.Object2IntMap
 *  it.unimi.dsi.fastutil.objects.Object2IntMap$Entry
 *  it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap
 *  net.minecraft.class_2561
 *  net.minecraft.class_5250
 *  net.minecraft.class_5251
 *  net.minecraft.class_5481
 */
package meteordevelopment.meteorclient.utils.misc.text;

import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import meteordevelopment.meteorclient.utils.misc.text.ColoredText;
import meteordevelopment.meteorclient.utils.render.color.Color;
import net.minecraft.class_2561;
import net.minecraft.class_5250;
import net.minecraft.class_5251;
import net.minecraft.class_5481;

public class TextUtils {
    private TextUtils() {
    }

    public static List<ColoredText> toColoredTextList(class_2561 text) {
        ArrayDeque<ColoredText> stack = new ArrayDeque<ColoredText>();
        ArrayList<ColoredText> coloredTexts = new ArrayList<ColoredText>();
        TextUtils.preOrderTraverse(text, stack, coloredTexts);
        coloredTexts.removeIf(e -> e.text().isEmpty());
        return coloredTexts;
    }

    public static class_5250 parseOrderedText(class_5481 orderedText) {
        class_5250 parsedText = class_2561.method_43473();
        orderedText.accept((i, style, codePoint) -> {
            parsedText.method_10852((class_2561)class_2561.method_43470((String)new String(Character.toChars(codePoint))).method_10862(style));
            return true;
        });
        return parsedText;
    }

    public static Color getMostPopularColor(class_2561 text) {
        Object2IntMap.Entry biggestEntry = null;
        for (Object2IntMap.Entry entry : TextUtils.getColoredCharacterCount(TextUtils.toColoredTextList(text)).object2IntEntrySet()) {
            if (biggestEntry == null) {
                biggestEntry = entry;
                continue;
            }
            if (entry.getIntValue() <= biggestEntry.getIntValue()) continue;
            biggestEntry = entry;
        }
        return biggestEntry == null ? new Color(255, 255, 255) : (Color)biggestEntry.getKey();
    }

    public static Object2IntMap<Color> getColoredCharacterCount(List<ColoredText> coloredTexts) {
        Object2IntOpenHashMap colorCount = new Object2IntOpenHashMap();
        for (ColoredText coloredText : coloredTexts) {
            if (colorCount.containsKey((Object)coloredText.color())) {
                colorCount.put((Object)coloredText.color(), colorCount.getInt((Object)coloredText.color()) + coloredText.text().length());
                continue;
            }
            colorCount.put((Object)coloredText.color(), coloredText.text().length());
        }
        return colorCount;
    }

    private static void preOrderTraverse(class_2561 text, Deque<ColoredText> stack, List<ColoredText> coloredTexts) {
        if (text == null) {
            return;
        }
        String textString = text.getString();
        class_5251 mcTextColor = text.method_10866().method_10973();
        Color textColor = mcTextColor == null ? (stack.isEmpty() ? new Color(255, 255, 255) : stack.peek().color()) : new Color(text.method_10866().method_10973().method_27716() | 0xFF000000);
        ColoredText coloredText = new ColoredText(textString, textColor);
        coloredTexts.add(coloredText);
        stack.push(coloredText);
        for (class_2561 child : text.method_10855()) {
            TextUtils.preOrderTraverse(child, stack, coloredTexts);
        }
        stack.pop();
    }
}

