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

export default {
	name: "salty_folly",
	cookTime: cookTimeTotalMini,
	requiredTemp: "2",
	isValid: (inv, tempSlot) => {
		let seagrassSlot = -1, kelpSlot = -1, pickleSlot = -1;
		let filled = 0;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item) {
				filled++;
				if (item.typeId === "minecraft:seagrass" && seagrassSlot === -1) seagrassSlot = slot;
				else if (item.typeId === "minecraft:dried_kelp" && kelpSlot === -1) kelpSlot = slot;
				else if (item.typeId === "minecraft:sea_pickle" && pickleSlot === -1) pickleSlot = slot;
			}
		}
		const vodka = inv.getItem(liquidSlot);
		const tankard = inv.getItem(slotGen);
		const result = inv.getItem(resultSlot);
		return seagrassSlot !== -1 && kelpSlot !== -1 && pickleSlot !== -1 &&
			   filled === 3 &&
			   vodka && vodka.typeId === "brewinandchewin:vodka" && vodka.amount >= 1 &&
			   tankard && tankard.typeId === "brewinandchewin:tankard" && tankard.amount >= 1 &&
			   (!result || result.typeId === "brewinandchewin:salty_folly") &&
			   tempSlot && tempSlot.nameTag === "2";
	},
	onComplete: (inv, keg, overworld) => {
		let seagrassSlot = -1, kelpSlot = -1, pickleSlot = -1;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item && item.typeId === "minecraft:seagrass") seagrassSlot = slot;
			if (item && item.typeId === "minecraft:dried_kelp") kelpSlot = slot;
			if (item && item.typeId === "minecraft:sea_pickle") pickleSlot = slot;
		}
		if (seagrassSlot !== -1) consumeItem(inv, seagrassSlot, 1);
		if (kelpSlot !== -1) consumeItem(inv, kelpSlot, 1);
		if (pickleSlot !== -1) consumeItem(inv, pickleSlot, 1);
		
		const vodka = inv.getItem(liquidSlot);
		if (vodka) consumeItem(inv, liquidSlot, 1);
		
		const tankard = inv.getItem(slotGen);
		if (tankard) consumeItem(inv, slotGen, 1);
		
		addResult(inv, "brewinandchewin:salty_folly", 1);
		returnTaraToBuffer(inv, "brewinandchewin:tankard", 1);
	}
};
