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

import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import org.apache.commons.lang3.ArrayUtils;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import net.minecraft.world.level.storage.loot.Serializer;
import com.google.common.collect.Lists;
import java.util.List;
import net.minecraft.world.level.storage.loot.predicates.ConditionUserBuilder;
import net.minecraft.world.level.storage.loot.ValidationContext;
import net.minecraft.world.level.storage.loot.predicates.LootItemConditions;
import net.minecraft.world.level.storage.loot.LootContext;
import java.util.function.Predicate;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;

public abstract class LootPoolEntryContainer implements ComposableEntryContainer {
    protected final LootItemCondition[] conditions;
    private final Predicate<LootContext> compositeCondition;
    
    protected LootPoolEntryContainer(final LootItemCondition[] arr) {
        this.conditions = arr;
        this.compositeCondition = LootItemConditions.<LootContext>andConditions(arr);
    }
    
    public void validate(final ValidationContext dbe) {
        for (int integer3 = 0; integer3 < this.conditions.length; ++integer3) {
            this.conditions[integer3].validate(dbe.forChild(".condition[" + integer3 + "]"));
        }
    }
    
    protected final boolean canRun(final LootContext dat) {
        return this.compositeCondition.test(dat);
    }
    
    public abstract LootPoolEntryType getType();
    
    public abstract static class Builder<T extends Builder<T>> implements ConditionUserBuilder<T> {
        private final List<LootItemCondition> conditions;
        
        public Builder() {
            this.conditions = Lists.newArrayList();
        }
        
        protected abstract T getThis();
        
        @Override
        public T when(final LootItemCondition.Builder a) {
            this.conditions.add(a.build());
            return this.getThis();
        }
        
        @Override
        public final T unwrap() {
            return this.getThis();
        }
        
        protected LootItemCondition[] getConditions() {
            return this.conditions.<LootItemCondition>toArray(new LootItemCondition[0]);
        }
        
        public AlternativesEntry.Builder otherwise(final Builder<?> a) {
            return new AlternativesEntry.Builder(new Builder[] { this, a });
        }
        
        public abstract LootPoolEntryContainer build();
    }
    
    public abstract static class Serializer<T extends LootPoolEntryContainer> implements net.minecraft.world.level.storage.loot.Serializer<T> {
        @Override
        public final void serialize(final JsonObject jsonObject, final T dbo, final JsonSerializationContext jsonSerializationContext) {
            if (!ArrayUtils.isEmpty((Object[])dbo.conditions)) {
                jsonObject.add("conditions", jsonSerializationContext.serialize(dbo.conditions));
            }
            this.serialize(jsonObject, dbo, jsonSerializationContext);
        }
        
        @Override
        public final T deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
            final LootItemCondition[] arr4 = GsonHelper.<LootItemCondition[]>getAsObject(jsonObject, "conditions", new LootItemCondition[0], jsonDeserializationContext, LootItemCondition[].class);
            return this.deserializeCustom(jsonObject, jsonDeserializationContext, arr4);
        }
        
        @Override
        public abstract void serialize(final JsonObject jsonObject, final T dbo, final JsonSerializationContext jsonSerializationContext);
        
        public abstract T deserializeCustom(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final LootItemCondition[] arr);
    }
}
