package net.minecraft.world.item.crafting;

import net.minecraft.world.Container;
import java.util.Map;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.core.Registry;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import com.google.common.collect.Maps;
import net.minecraft.world.level.ItemLike;
import java.util.List;
import net.minecraft.world.item.ItemStack;
import com.google.common.collect.Lists;
import net.minecraft.world.level.Level;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.resources.ResourceLocation;

public class RepairItemRecipe extends CustomRecipe {
    public RepairItemRecipe(final ResourceLocation uh) {
        super(uh);
    }
    
    @Override
    public boolean matches(final CraftingContainer bgu, final Level bqb) {
        final List<ItemStack> list4 = Lists.newArrayList();
        for (int integer5 = 0; integer5 < bgu.getContainerSize(); ++integer5) {
            final ItemStack bki6 = bgu.getItem(integer5);
            if (!bki6.isEmpty()) {
                list4.add(bki6);
                if (list4.size() > 1) {
                    final ItemStack bki7 = list4.get(0);
                    if (bki6.getItem() != bki7.getItem() || bki7.getCount() != 1 || bki6.getCount() != 1 || !bki7.getItem().canBeDepleted()) {
                        return false;
                    }
                }
            }
        }
        return list4.size() == 2;
    }
    
    @Override
    public ItemStack assemble(final CraftingContainer bgu) {
        final List<ItemStack> list3 = Lists.newArrayList();
        for (int integer4 = 0; integer4 < bgu.getContainerSize(); ++integer4) {
            final ItemStack bki5 = bgu.getItem(integer4);
            if (!bki5.isEmpty()) {
                list3.add(bki5);
                if (list3.size() > 1) {
                    final ItemStack bki6 = list3.get(0);
                    if (bki5.getItem() != bki6.getItem() || bki6.getCount() != 1 || bki5.getCount() != 1 || !bki6.getItem().canBeDepleted()) {
                        return ItemStack.EMPTY;
                    }
                }
            }
        }
        if (list3.size() == 2) {
            final ItemStack bki7 = list3.get(0);
            final ItemStack bki5 = list3.get(1);
            if (bki7.getItem() == bki5.getItem() && bki7.getCount() == 1 && bki5.getCount() == 1 && bki7.getItem().canBeDepleted()) {
                final Item bke6 = bki7.getItem();
                final int integer5 = bke6.getMaxDamage() - bki7.getDamageValue();
                final int integer6 = bke6.getMaxDamage() - bki5.getDamageValue();
                final int integer7 = integer5 + integer6 + bke6.getMaxDamage() * 5 / 100;
                int integer8 = bke6.getMaxDamage() - integer7;
                if (integer8 < 0) {
                    integer8 = 0;
                }
                final ItemStack bki8 = new ItemStack(bki7.getItem());
                bki8.setDamageValue(integer8);
                final Map<Enchantment, Integer> map12 = Maps.newHashMap();
                final Map<Enchantment, Integer> map13 = EnchantmentHelper.getEnchantments(bki7);
                final Map<Enchantment, Integer> map14 = EnchantmentHelper.getEnchantments(bki5);
                final Map<K, Integer> map15;
                final Map<K, Integer> map16;
                final int integer9;
                final Map<Enchantment, Integer> map17;
                Registry.ENCHANTMENT.stream().filter(Enchantment::isCurse).forEach(bnw -> {
                    integer9 = Math.max(map15.getOrDefault(bnw, 0), map16.getOrDefault(bnw, 0));
                    if (integer9 > 0) {
                        map17.put(bnw, integer9);
                    }
                    return;
                });
                if (!map12.isEmpty()) {
                    EnchantmentHelper.setEnchantments(map12, bki8);
                }
                return bki8;
            }
        }
        return ItemStack.EMPTY;
    }
    
    @Override
    public boolean canCraftInDimensions(final int integer1, final int integer2) {
        return integer1 * integer2 >= 2;
    }
    
    @Override
    public RecipeSerializer<?> getSerializer() {
        return RecipeSerializer.REPAIR_ITEM;
    }
}
