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 Component mergeStyles(final Component lf, final Style ln) {
        if (ln.isEmpty()) {
            return lf;
        }
        if (lf.getStyle().isEmpty()) {
            return lf.setStyle(ln.copy());
        }
        return new TextComponent("").append(lf).setStyle(ln.copy());
    }
    
    public static Component updateForEntity(@Nullable final CommandSourceStack cq, final Component lf, @Nullable final Entity akq, int integer) throws CommandSyntaxException {
        if (integer > 100) {
            return lf;
        }
        ++integer;
        final Component lf2 = (lf instanceof ContextAwareComponent) ? ((ContextAwareComponent)lf).resolve(cq, akq, integer) : lf.copy();
        for (final Component lf3 : lf.getSiblings()) {
            lf2.append(updateForEntity(cq, lf3, akq, integer));
        }
        return mergeStyles(lf2, lf.getStyle());
    }
    
    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 new TextComponent("");
        }
        if (collection.size() == 1) {
            return function.apply(collection.iterator().next());
        }
        final List<T> list3 = Lists.newArrayList(collection);
        list3.sort(Comparable::compareTo);
        return ComponentUtils.formatList((Collection<Object>)list3, (Function<Object, Component>)function);
    }
    
    public static <T> Component 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());
        }
        final Component lf3 = new TextComponent("");
        boolean boolean4 = true;
        for (final T object6 : collection) {
            if (!boolean4) {
                lf3.append(new TextComponent(", ").withStyle(ChatFormatting.GRAY));
            }
            lf3.append(function.apply(object6));
            boolean4 = false;
        }
        return lf3;
    }
    
    public static Component wrapInSquareBrackets(final Component lf) {
        return new TextComponent("[").append(lf).append("]");
    }
    
    public static Component fromMessage(final Message message) {
        if (message instanceof Component) {
            return (Component)message;
        }
        return new TextComponent(message.getString());
    }
}
