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

import com.google.gson.JsonParseException;
import net.minecraft.tags.ItemTags;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import java.util.Iterator;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.item.ItemStack;
import java.util.function.Consumer;
import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.item.Item;
import net.minecraft.tags.Tag;

public class TagEntry extends LootPoolSingletonContainer {
    private final Tag<Item> tag;
    private final boolean expand;
    
    private TagEntry(final Tag<Item> aaz, final boolean boolean2, final int integer3, final int integer4, final LootItemCondition[] arr, final LootItemFunction[] arr) {
        super(integer3, integer4, arr, arr);
        this.tag = aaz;
        this.expand = boolean2;
    }
    
    public void createItemStack(final Consumer<ItemStack> consumer, final LootContext crx) {
        this.tag.getValues().forEach(bei -> consumer.accept(new ItemStack(bei)));
    }
    
    private boolean expandTag(final LootContext crx, final Consumer<LootPoolEntry> consumer) {
        if (this.canRun(crx)) {
            for (final Item bei5 : this.tag.getValues()) {
                consumer.accept(new EntryBase() {
                    @Override
                    public void createItemStack(final Consumer<ItemStack> consumer, final LootContext crx) {
                        consumer.accept(new ItemStack(bei5));
                    }
                });
            }
            return true;
        }
        return false;
    }
    
    @Override
    public boolean expand(final LootContext crx, final Consumer<LootPoolEntry> consumer) {
        if (this.expand) {
            return this.expandTag(crx, consumer);
        }
        return super.expand(crx, consumer);
    }
    
    public static Builder<?> expandTag(final Tag<Item> aaz) {
        return LootPoolSingletonContainer.simpleBuilder((integer2, integer3, arr, arr) -> new TagEntry(aaz, true, integer2, integer3, arr, arr));
    }
    
    public static class Serializer extends LootPoolSingletonContainer.Serializer<TagEntry> {
        public Serializer() {
            super(new ResourceLocation("tag"), TagEntry.class);
        }
        
        @Override
        public void serialize(final JsonObject jsonObject, final TagEntry csu, final JsonSerializationContext jsonSerializationContext) {
            super.serialize(jsonObject, csu, jsonSerializationContext);
            jsonObject.addProperty("name", csu.tag.getId().toString());
            jsonObject.addProperty("expand", csu.expand);
        }
        
        @Override
        protected TagEntry deserialize(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext, final int integer3, final int integer4, final LootItemCondition[] arr, final LootItemFunction[] arr) {
            final ResourceLocation sm8 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "name"));
            final Tag<Item> aaz9 = ItemTags.getAllTags().getTag(sm8);
            if (aaz9 == null) {
                throw new JsonParseException("Can't find tag: " + sm8);
            }
            final boolean boolean10 = GsonHelper.getAsBoolean(jsonObject, "expand");
            return new TagEntry(aaz9, boolean10, integer3, integer4, arr, arr, null);
        }
    }
}
