package net.minecraft.world.level.storage.loot.entries;

import net.minecraft.util.GsonHelper;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import java.util.function.Consumer;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.ValidationContext;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;

public abstract class CompositeEntryBase extends LootPoolEntryContainer {
    protected final LootPoolEntryContainer[] children;
    private final ComposableEntryContainer composedChildren;
    
    protected CompositeEntryBase(final LootPoolEntryContainer[] arr, final LootItemCondition[] arr) {
        super(arr);
        this.children = arr;
        this.composedChildren = this.compose(arr);
    }
    
    @Override
    public void validate(final ValidationContext csg) {
        super.validate(csg);
        if (this.children.length == 0) {
            csg.reportProblem("Empty children list");
        }
        for (int integer3 = 0; integer3 < this.children.length; ++integer3) {
            this.children[integer3].validate(csg.forChild(".entry[" + integer3 + "]"));
        }
    }
    
    protected abstract ComposableEntryContainer compose(final ComposableEntryContainer[] arr);
    
    @Override
    public final boolean expand(final LootContext crx, final Consumer<LootPoolEntry> consumer) {
        return this.canRun(crx) && this.composedChildren.expand(crx, consumer);
    }
    
    public static <T extends CompositeEntryBase> Serializer<T> createSerializer(final ResourceLocation sm, final Class<T> class2, final CompositeEntryConstructor<T> a) {
        return new Serializer<T>(sm, class2) {
            @Override
            protected T deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootPoolEntryContainer[] arr, final LootItemCondition[] arr) {
                return a.create(arr, arr);
            }
        };
    }
    
    public abstract static class Serializer<T extends CompositeEntryBase> extends LootPoolEntryContainer.Serializer<T> {
        public Serializer(final ResourceLocation sm, final Class<T> class2) {
            super(sm, class2);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final T csj, final JsonSerializationContext jsonSerializationContext) {
            jsonObject.add("children", jsonSerializationContext.serialize(csj.children));
        }
        
        @Override
        public final T deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
            final LootPoolEntryContainer[] arr2 = GsonHelper.<LootPoolEntryContainer[]>getAsObject(jsonObject, "children", jsonDeserializationContext, LootPoolEntryContainer[].class);
            return this.deserialize(jsonObject, jsonDeserializationContext, arr2, arr);
        }
        
        protected abstract T deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootPoolEntryContainer[] arr, final LootItemCondition[] arr);
    }
    
    @FunctionalInterface
    public interface CompositeEntryConstructor<T extends CompositeEntryBase> {
        T create(final LootPoolEntryContainer[] arr, final LootItemCondition[] arr);
    }
}
