package net.minecraft.data.models.blockstates;

import java.util.function.Supplier;
import net.minecraft.world.level.block.state.BlockState;
import com.google.gson.JsonObject;
import java.util.function.Consumer;
import java.util.function.Function;
import com.google.gson.JsonArray;
import net.minecraft.world.level.block.state.StateDefinition;
import com.google.gson.JsonElement;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.util.List;
import net.minecraft.world.level.block.Block;

public class MultiPartGenerator implements BlockStateGenerator {
    private final Block block;
    private final List<Entry> parts;
    
    private MultiPartGenerator(final Block bvr) {
        this.parts = Lists.newArrayList();
        this.block = bvr;
    }
    
    @Override
    public Block getBlock() {
        return this.block;
    }
    
    public static MultiPartGenerator multiPart(final Block bvr) {
        return new MultiPartGenerator(bvr);
    }
    
    public MultiPartGenerator with(final List<Variant> list) {
        this.parts.add(new Entry((List)list));
        return this;
    }
    
    public MultiPartGenerator with(final Variant iq) {
        return this.with(ImmutableList.<Variant>of(iq));
    }
    
    public MultiPartGenerator with(final Condition il, final List<Variant> list) {
        this.parts.add(new ConditionalEntry(il, (List)list));
        return this;
    }
    
    public MultiPartGenerator with(final Condition il, final Variant... arr) {
        return this.with(il, ImmutableList.<Variant>copyOf(arr));
    }
    
    public MultiPartGenerator with(final Condition il, final Variant iq) {
        return this.with(il, ImmutableList.<Variant>of(iq));
    }
    
    @Override
    public JsonElement get() {
        final StateDefinition<Block, BlockState> cfk2 = this.block.getStateDefinition();
        this.parts.forEach(b -> b.validate(cfk2));
        final JsonArray jsonArray3 = new JsonArray();
        this.parts.stream().map(Entry::get).forEach(jsonArray3::add);
        final JsonObject jsonObject4 = new JsonObject();
        jsonObject4.add("multipart", jsonArray3);
        return jsonObject4;
    }
    
    static class Entry implements Supplier<JsonElement> {
        private final List<Variant> variants;
        
        private Entry(final List<Variant> list) {
            this.variants = list;
        }
        
        public void validate(final StateDefinition<?, ?> cfk) {
        }
        
        public void decorate(final JsonObject jsonObject) {
        }
        
        @Override
        public JsonElement get() {
            final JsonObject jsonObject2 = new JsonObject();
            this.decorate(jsonObject2);
            jsonObject2.add("apply", Variant.convertList(this.variants));
            return jsonObject2;
        }
    }
    
    static class ConditionalEntry extends Entry {
        private final Condition condition;
        
        private ConditionalEntry(final Condition il, final List<Variant> list) {
            super((List)list);
            this.condition = il;
        }
        
        @Override
        public void validate(final StateDefinition<?, ?> cfk) {
            this.condition.validate(cfk);
        }
        
        @Override
        public void decorate(final JsonObject jsonObject) {
            jsonObject.add("when", this.condition.get());
        }
    }
}
