import { resultSlot, liquidSlot, slotGen } from "./helpers.js";
import { cookTimeTotalMini } from "./helpers.js";
import { consumeItem, addResult, returnTaraToBuffer } from "./helpers.js";

export default {
	name: "saccharine_rum",
	cookTime: cookTimeTotalMini,
	requiredTemp: "3",
	isValid: (inv, tempSlot) => {
		let melonSlot = -1, sugarCaneSlot = -1, berriesSlot = -1;
		let filled = 0;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item) {
				filled++;
				if (item.typeId === "minecraft:melon_block" && melonSlot === -1) melonSlot = slot;
				else if (item.typeId === "minecraft:sugar_cane" && sugarCaneSlot === -1) sugarCaneSlot = slot;
				else if (item.typeId === "minecraft:sweet_berries" && berriesSlot === -1) berriesSlot = slot;
			}
		}
		const mead = inv.getItem(liquidSlot);
		const tankard = inv.getItem(slotGen);
		const result = inv.getItem(resultSlot);
		return melonSlot !== -1 && sugarCaneSlot !== -1 && berriesSlot !== -1 &&
			   filled === 3 &&
			   mead && mead.typeId === "brewinandchewin:mead" && mead.amount >= 1 &&
			   tankard && tankard.typeId === "brewinandchewin:tankard" && tankard.amount >= 1 &&
			   (!result || result.typeId === "brewinandchewin:saccharine_rum") &&
			   tempSlot && tempSlot.nameTag === "3";
	},
	onComplete: (inv, keg, overworld) => {
		let melonSlot = -1, sugarCaneSlot = -1, berriesSlot = -1;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item && item.typeId === "minecraft:melon_block") melonSlot = slot;
			if (item && item.typeId === "minecraft:sugar_cane") sugarCaneSlot = slot;
			if (item && item.typeId === "minecraft:sweet_berries") berriesSlot = slot;
		}
		if (melonSlot !== -1) consumeItem(inv, melonSlot, 1);
		if (sugarCaneSlot !== -1) consumeItem(inv, sugarCaneSlot, 1);
		if (berriesSlot !== -1) consumeItem(inv, berriesSlot, 1);
		
		const mead = inv.getItem(liquidSlot);
		if (mead) consumeItem(inv, liquidSlot, 1);
		
		const tankard = inv.getItem(slotGen);
		if (tankard) consumeItem(inv, slotGen, 1);
		
		addResult(inv, "brewinandchewin:saccharine_rum", 1);
		returnTaraToBuffer(inv, "brewinandchewin:tankard", 1);
	}
};
