import { ItemStack } from "@minecraft/server";

// Слоты и тайминги
export const liquidSlot = 4;
export const resultSlot = 5;
export const slotGen = 6;
export const cookTimeTotal = 9600;
export const cookTimeTotalMini = 4800;

// Allowed lists
export const allowedMeats = [
	"farmersdelight:minced_beef", "farmersdelight:chicken_cuts", "farmersdelight:mutton_chops",
	"farmersdelight:bacon", "minecraft:beef", "minecraft:chicken", "minecraft:muttonraw",
	"minecraft:porkchop", "minecraft:rabbit", "minecraft:rotten_flesh"
];
export const allowedVeggies = [
	"minecraft:carrot", "minecraft:potato", "minecraft:golden_carrot", "minecraft:beetroot",
	"farmersdelight:onion", "farmersdelight:tomato", "farmersdelight:cabbage", "farmersdelight:cabbage_leaf"
];
export const kippersFishIds = [
	"minecraft:cod", "minecraft:salmon", "minecraft:tropical_fish",
	"farmersdelight:cod_slice", "farmersdelight:salmon_slice",
	"crabbersdelight:tropical_fish_slice", "crabbersdelight:pufferfish_slice"
];

// Утилиты работы со слотом инвентаря
export function consumeItem(inv, slot, amount = 1) {
	const item = inv.getItem(slot);
	if (!item) return;
	if (item.amount > amount) {
		item.amount -= amount;
		inv.setItem(slot, item);
	} else {
		inv.setItem(slot, undefined);
	}
}

export function addResult(inv, resultId, amount) {
	const result = inv.getItem(resultSlot);
	if (!result) {
		inv.setItem(resultSlot, new ItemStack(resultId, amount));
	} else {
		result.amount += amount;
		inv.setItem(resultSlot, result);
	}
}

export function returnTaraToBuffer(inv, typeId, amount) {
	for (let i = 11; i <= 26 && amount > 0; i++) {
		let slotItem = inv.getItem(i);
		if (slotItem && slotItem.typeId === typeId && slotItem.amount < slotItem.maxAmount) {
			let canAdd = Math.min(amount, slotItem.maxAmount - slotItem.amount);
			slotItem.amount += canAdd;
			amount -= canAdd;
			inv.setItem(i, slotItem);
		}
	}
	for (let i = 11; i <= 26 && amount > 0; i++) {
		if (!inv.getItem(i)) {
			let toPlace = Math.min(amount, 64);
			inv.setItem(i, new ItemStack(typeId, toPlace));
			amount -= toPlace;
		}
	}
}

export function isResultSlotFull(inv) {
	const resultItem = inv.getItem(resultSlot);
	if (!resultItem) return false;
	const maxStackSize = resultItem.maxAmount || 64;
	return resultItem.amount >= maxStackSize;
}
