package com.yourname.premiumah.util;

import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.Map;

public final class InventoryUtil {
    private InventoryUtil() {
    }

    public static boolean isAir(ItemStack item) {
        return item == null || item.getType() == Material.AIR || item.getAmount() <= 0;
    }

    public static boolean canFit(Inventory inventory, ItemStack item) {
        if (isAir(item)) {
            return true;
        }
        int remaining = item.getAmount();
        int max = item.getMaxStackSize();
        for (ItemStack slot : inventory.getStorageContents()) {
            if (isAir(slot)) {
                remaining -= max;
            } else if (slot.isSimilar(item)) {
                remaining -= Math.max(0, max - slot.getAmount());
            }
            if (remaining <= 0) {
                return true;
            }
        }
        return false;
    }

    public static Map<Integer, ItemStack> addItem(Inventory inventory, ItemStack item) {
        if (isAir(item)) {
            return new HashMap<>();
        }
        return inventory.addItem(item.clone());
    }
}
