import { resultSlot, liquidSlot } from "./helpers.js";
import { allowedMeats } from "./helpers.js";
import { cookTimeTotal } from "./helpers.js";
import { consumeItem, addResult } from "./helpers.js";

export default {
	name: "jerky",
	cookTime: cookTimeTotal,
	requiredTemp: "3",
	isValid: (inv, tempSlot) => {
		let meatSlots = [];
		let onlyMeatOrEmpty = true;
		let filled = 0;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item) {
				filled++;
				if (allowedMeats.includes(item.typeId)) {
					meatSlots.push(slot);
				} else {
					onlyMeatOrEmpty = false;
					break;
				}
			}
		}
		const result = inv.getItem(resultSlot);
		return (!inv.getItem(liquidSlot) && !inv.getItem(6)) &&
			   (!result || result.typeId === "brewinandchewin:jerky") &&
			   tempSlot && tempSlot.nameTag === "3" &&
			   filled === 3 && meatSlots.length === 3 && onlyMeatOrEmpty;
	},
	onComplete: (inv, keg, overworld) => {
		let meatSlots = [];
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item && allowedMeats.includes(item.typeId)) {
				meatSlots.push(slot);
			}
		}
		for (const slot of meatSlots) {
			consumeItem(inv, slot, 1);
		}
		addResult(inv, "brewinandchewin:jerky", 3);
	}
};
