package net.minecraft.world;

import java.util.AbstractList;
import java.util.function.Predicate;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.ListTag;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import java.util.List;

public class ContainerHelper {
    public static ItemStack removeItem(final List<ItemStack> list, final int integer2, final int integer3) {
        if (integer2 < 0 || integer2 >= list.size() || list.get(integer2).isEmpty() || integer3 <= 0) {
            return ItemStack.EMPTY;
        }
        return list.get(integer2).split(integer3);
    }
    
    public static ItemStack takeItem(final List<ItemStack> list, final int integer) {
        if (integer < 0 || integer >= list.size()) {
            return ItemStack.EMPTY;
        }
        return list.set(integer, ItemStack.EMPTY);
    }
    
    public static CompoundTag saveAllItems(final CompoundTag le, final NonNullList<ItemStack> gi) {
        return saveAllItems(le, gi, true);
    }
    
    public static CompoundTag saveAllItems(final CompoundTag le, final NonNullList<ItemStack> gi, final boolean boolean3) {
        final ListTag lk4 = new ListTag();
        for (int integer5 = 0; integer5 < gi.size(); ++integer5) {
            final ItemStack bki6 = gi.get(integer5);
            if (!bki6.isEmpty()) {
                final CompoundTag le2 = new CompoundTag();
                le2.putByte("Slot", (byte)integer5);
                bki6.save(le2);
                ((AbstractList<CompoundTag>)lk4).add(le2);
            }
        }
        if (!lk4.isEmpty() || boolean3) {
            le.put("Items", lk4);
        }
        return le;
    }
    
    public static void loadAllItems(final CompoundTag le, final NonNullList<ItemStack> gi) {
        final ListTag lk3 = le.getList("Items", 10);
        for (int integer4 = 0; integer4 < lk3.size(); ++integer4) {
            final CompoundTag le2 = lk3.getCompound(integer4);
            final int integer5 = le2.getByte("Slot") & 0xFF;
            if (integer5 >= 0 && integer5 < gi.size()) {
                gi.set(integer5, ItemStack.of(le2));
            }
        }
    }
    
    public static int clearOrCountMatchingItems(final Container amz, final Predicate<ItemStack> predicate, final int integer, final boolean boolean4) {
        int integer2 = 0;
        for (int integer3 = 0; integer3 < amz.getContainerSize(); ++integer3) {
            final ItemStack bki7 = amz.getItem(integer3);
            final int integer4 = clearOrCountMatchingItems(bki7, predicate, integer - integer2, boolean4);
            if (integer4 > 0 && !boolean4 && bki7.isEmpty()) {
                amz.setItem(integer3, ItemStack.EMPTY);
            }
            integer2 += integer4;
        }
        return integer2;
    }
    
    public static int clearOrCountMatchingItems(final ItemStack bki, final Predicate<ItemStack> predicate, final int integer, final boolean boolean4) {
        if (bki.isEmpty() || !predicate.test(bki)) {
            return 0;
        }
        if (boolean4) {
            return bki.getCount();
        }
        final int integer2 = (integer < 0) ? bki.getCount() : Math.min(integer, bki.getCount());
        bki.shrink(integer2);
        return integer2;
    }
}
