package net.minecraft.network.chat;

import com.mojang.brigadier.Message;
import java.util.List;
import com.google.common.collect.Lists;
import java.util.function.Function;
import net.minecraft.ChatFormatting;
import java.util.Collection;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.Iterator;
import net.minecraft.world.entity.Entity;
import javax.annotation.Nullable;
import net.minecraft.commands.CommandSourceStack;

public class ComponentUtils {
    public static MutableComponent mergeStyles(final MutableComponent mx, final Style nb) {
        if (nb.isEmpty()) {
            return mx;
        }
        final Style nb2 = mx.getStyle();
        if (nb2.isEmpty()) {
            return mx.setStyle(nb);
        }
        if (nb2.equals(nb)) {
            return mx;
        }
        return mx.setStyle(nb2.applyTo(nb));
    }
    
    public static MutableComponent updateForEntity(@Nullable final CommandSourceStack cz, final Component mr, @Nullable final Entity aom, final int integer) throws CommandSyntaxException {
        if (integer > 100) {
            return mr.copy();
        }
        final MutableComponent mx5 = (mr instanceof ContextAwareComponent) ? ((ContextAwareComponent)mr).resolve(cz, aom, integer + 1) : mr.plainCopy();
        for (final Component mr2 : mr.getSiblings()) {
            mx5.append(updateForEntity(cz, mr2, aom, integer + 1));
        }
        return mx5.withStyle(resolveStyle(cz, mr.getStyle(), aom, integer));
    }
    
    private static Style resolveStyle(@Nullable final CommandSourceStack cz, final Style nb, @Nullable final Entity aom, final int integer) throws CommandSyntaxException {
        final HoverEvent mv5 = nb.getHoverEvent();
        if (mv5 != null) {
            final Component mr6 = mv5.<Component>getValue(HoverEvent.Action.SHOW_TEXT);
            if (mr6 != null) {
                final HoverEvent mv6 = new HoverEvent((HoverEvent.Action<T>)HoverEvent.Action.SHOW_TEXT, (T)updateForEntity(cz, mr6, aom, integer + 1));
                return nb.withHoverEvent(mv6);
            }
        }
        return nb;
    }
    
    public static Component getDisplayName(final GameProfile gameProfile) {
        if (gameProfile.getName() != null) {
            return new TextComponent(gameProfile.getName());
        }
        if (gameProfile.getId() != null) {
            return new TextComponent(gameProfile.getId().toString());
        }
        return new TextComponent("(unknown)");
    }
    
    public static Component formatList(final Collection<String> collection) {
        return ComponentUtils.<String>formatAndSortList(collection, string -> new TextComponent(string).withStyle(ChatFormatting.GREEN));
    }
    
    public static <T extends Comparable<T>> Component formatAndSortList(final Collection<T> collection, final Function<T, Component> function) {
        if (collection.isEmpty()) {
            return TextComponent.EMPTY;
        }
        if (collection.size() == 1) {
            return function.apply(collection.iterator().next());
        }
        final List<T> list3 = Lists.newArrayList(collection);
        list3.sort(Comparable::compareTo);
        return ComponentUtils.<T>formatList(list3, function);
    }
    
    public static <T> MutableComponent formatList(final Collection<T> collection, final Function<T, Component> function) {
        if (collection.isEmpty()) {
            return new TextComponent("");
        }
        if (collection.size() == 1) {
            return function.apply(collection.iterator().next()).copy();
        }
        final MutableComponent mx3 = new TextComponent("");
        boolean boolean4 = true;
        for (final T object6 : collection) {
            if (!boolean4) {
                mx3.append(new TextComponent(", ").withStyle(ChatFormatting.GRAY));
            }
            mx3.append(function.apply(object6));
            boolean4 = false;
        }
        return mx3;
    }
    
    public static MutableComponent wrapInSquareBrackets(final Component mr) {
        return new TextComponent("[").append(mr).append("]");
    }
    
    public static Component fromMessage(final Message message) {
        if (message instanceof Component) {
            return (Component)message;
        }
        return new TextComponent(message.getString());
    }
}
