package wtf.opal.utility.render;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.text.CharacterVisitor;
import net.minecraft.text.Style;
import net.minecraft.util.Formatting;

@Environment(EnvType.CLIENT)
public final class OrderedTextVisitor implements CharacterVisitor {
   private final StringBuilder builder = new StringBuilder();
   private Formatting lastFormatting = null;

   @Override
   public boolean accept(int index, Style style, int codePoint) {
      if (style.isBold()) {
         this.builder.append(Formatting.BOLD);
         this.lastFormatting = Formatting.BOLD;
      }

      if (style.getColor() != null) {
         for (Formatting var7 : Formatting.values()) {
            if (var7.isColor() && var7.getColorValue() == style.getColor().getRgb() && var7 != this.lastFormatting) {
               this.builder.append(var7);
               this.lastFormatting = var7;
               break;
            }
         }
      }

      this.builder.append(new String(Character.toChars(codePoint)));
      return true;
   }

   public String getFormattedString() {
      return this.builder.toString();
   }
}
