package net.minecraft.commands.arguments;

import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.brigadier.Message;
import net.minecraft.world.level.Level;
import net.minecraft.core.Registry;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.commands.CommandSourceStack;
import java.util.function.Function;
import net.minecraft.resources.ResourceKey;
import net.minecraft.commands.SharedSuggestionProvider;
import com.mojang.brigadier.suggestion.Suggestions;
import java.util.concurrent.CompletableFuture;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import java.util.Collection;
import net.minecraft.resources.ResourceLocation;
import com.mojang.brigadier.arguments.ArgumentType;

public class DimensionArgument implements ArgumentType<ResourceLocation> {
    private static final Collection<String> EXAMPLES;
    private static final DynamicCommandExceptionType ERROR_INVALID_VALUE;
    
    public ResourceLocation parse(final StringReader stringReader) throws CommandSyntaxException {
        return ResourceLocation.read(stringReader);
    }
    
    public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> commandContext, final SuggestionsBuilder suggestionsBuilder) {
        if (commandContext.getSource() instanceof SharedSuggestionProvider) {
            return SharedSuggestionProvider.suggestResource(((SharedSuggestionProvider)commandContext.getSource()).levels().stream().<ResourceLocation>map(ResourceKey::location), suggestionsBuilder);
        }
        return (CompletableFuture<Suggestions>)Suggestions.empty();
    }
    
    public Collection<String> getExamples() {
        return DimensionArgument.EXAMPLES;
    }
    
    public static DimensionArgument dimension() {
        return new DimensionArgument();
    }
    
    public static ServerLevel getDimension(final CommandContext<CommandSourceStack> commandContext, final String string) throws CommandSyntaxException {
        final ResourceLocation uh3 = (ResourceLocation)commandContext.getArgument(string, (Class)ResourceLocation.class);
        final ResourceKey<Level> ug4 = ResourceKey.<Level>create(Registry.DIMENSION_REGISTRY, uh3);
        final ServerLevel zd5 = ((CommandSourceStack)commandContext.getSource()).getServer().getLevel(ug4);
        if (zd5 == null) {
            throw DimensionArgument.ERROR_INVALID_VALUE.create(uh3);
        }
        return zd5;
    }
    
    static {
        EXAMPLES = Stream.<ResourceKey>of((ResourceKey[])new ResourceKey[] { Level.OVERWORLD, Level.NETHER }).map(ug -> ug.location().toString()).collect(Collectors.toList());
        final TranslatableComponent translatableComponent;
        ERROR_INVALID_VALUE = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("argument.dimension.invalid", new Object[] { object });
            return translatableComponent;
        });
    }
}
