package net.minecraft.world.item;

import java.util.AbstractList;
import java.util.Iterator;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.core.NonNullList;
import net.minecraft.world.level.ItemLike;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
import net.minecraft.world.item.enchantment.EnchantmentInstance;
import net.minecraft.network.chat.Component;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.world.level.Level;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;

public class EnchantedBookItem extends Item {
    public EnchantedBookItem(final Properties a) {
        super(a);
    }
    
    @Override
    public boolean isFoil(final ItemStack ben) {
        return true;
    }
    
    @Override
    public boolean isEnchantable(final ItemStack ben) {
        return false;
    }
    
    public static ListTag getEnchantments(final ItemStack ben) {
        final CompoundTag jt2 = ben.getTag();
        if (jt2 != null) {
            return jt2.getList("StoredEnchantments", 10);
        }
        return new ListTag();
    }
    
    @Override
    public void appendHoverText(final ItemStack ben, @Nullable final Level bjw, final List<Component> list, final TooltipFlag bfw) {
        super.appendHoverText(ben, bjw, list, bfw);
        ItemStack.appendEnchantmentNames(list, getEnchantments(ben));
    }
    
    public static void addEnchantment(final ItemStack ben, final EnchantmentInstance bia) {
        final ListTag jz3 = getEnchantments(ben);
        boolean boolean4 = true;
        final ResourceLocation sm5 = Registry.ENCHANTMENT.getKey(bia.enchantment);
        for (int integer6 = 0; integer6 < jz3.size(); ++integer6) {
            final CompoundTag jt7 = jz3.getCompound(integer6);
            final ResourceLocation sm6 = ResourceLocation.tryParse(jt7.getString("id"));
            if (sm6 != null && sm6.equals(sm5)) {
                if (jt7.getInt("lvl") < bia.level) {
                    jt7.putShort("lvl", (short)bia.level);
                }
                boolean4 = false;
                break;
            }
        }
        if (boolean4) {
            final CompoundTag jt8 = new CompoundTag();
            jt8.putString("id", String.valueOf(sm5));
            jt8.putShort("lvl", (short)bia.level);
            ((AbstractList<CompoundTag>)jz3).add(jt8);
        }
        ben.getOrCreateTag().put("StoredEnchantments", jz3);
    }
    
    public static ItemStack createForEnchantment(final EnchantmentInstance bia) {
        final ItemStack ben2 = new ItemStack(Items.ENCHANTED_BOOK);
        addEnchantment(ben2, bia);
        return ben2;
    }
    
    @Override
    public void fillItemCategory(final CreativeModeTab bdd, final NonNullList<ItemStack> fy) {
        if (bdd == CreativeModeTab.TAB_SEARCH) {
            for (final Enchantment bhx5 : Registry.ENCHANTMENT) {
                if (bhx5.category != null) {
                    for (int integer6 = bhx5.getMinLevel(); integer6 <= bhx5.getMaxLevel(); ++integer6) {
                        fy.add(createForEnchantment(new EnchantmentInstance(bhx5, integer6)));
                    }
                }
            }
        }
        else if (bdd.getEnchantmentCategories().length != 0) {
            for (final Enchantment bhx5 : Registry.ENCHANTMENT) {
                if (bdd.hasEnchantmentCategory(bhx5.category)) {
                    fy.add(createForEnchantment(new EnchantmentInstance(bhx5, bhx5.getMaxLevel())));
                }
            }
        }
    }
}
