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

import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
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 dbe) {
        super.validate(dbe);
        if (this.children.length == 0) {
            dbe.reportProblem("Empty children list");
        }
        for (int integer3 = 0; integer3 < this.children.length; ++integer3) {
            this.children[integer3].validate(dbe.forChild(".entry[" + integer3 + "]"));
        }
    }
    
    protected abstract ComposableEntryContainer compose(final ComposableEntryContainer[] arr);
    
    @Override
    public final boolean expand(final LootContext dat, final Consumer<LootPoolEntry> consumer) {
        return this.canRun(dat) && this.composedChildren.expand(dat, consumer);
    }
    
    public static <T extends CompositeEntryBase> Serializer<T> createSerializer(final CompositeEntryConstructor<T> a) {
        return new Serializer<T>() {
            @Override
            public void serialize(final JsonObject jsonObject, final T dbh, final JsonSerializationContext jsonSerializationContext) {
                jsonObject.add("children", jsonSerializationContext.serialize(dbh.children));
            }
            
            @Override
            public final T deserializeCustom(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr) {
                final LootPoolEntryContainer[] arr2 = GsonHelper.<LootPoolEntryContainer[]>getAsObject(jsonObject, "children", jsonDeserializationContext, LootPoolEntryContainer[].class);
                return a.create(arr2, arr);
            }
        };
    }
    
    @FunctionalInterface
    public interface CompositeEntryConstructor<T extends CompositeEntryBase> {
        T create(final LootPoolEntryContainer[] arr, final LootItemCondition[] arr);
    }
}
