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

export default {
	name: "kippers",
	cookTime: cookTimeTotal,
	requiredTemp: "3",
	isValid: (inv, tempSlot) => {
		let kelpSlot = -1;
		let fishSlots = [];
		let filled = 0;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item) {
				filled++;
				if (item.typeId === "minecraft:dried_kelp" && kelpSlot === -1) {
					kelpSlot = slot;
				} else if (kippersFishIds.includes(item.typeId)) {
					fishSlots.push(slot);
				}
			}
		}
		const result = inv.getItem(resultSlot);
		return kelpSlot !== -1 &&
			   fishSlots.length === 2 &&
			   filled === 3 &&
			   (!inv.getItem(liquidSlot) && !inv.getItem(6)) &&
			   (!result || result.typeId === "brewinandchewin:kippers") &&
			   tempSlot && tempSlot.nameTag === "3";
	},
	onComplete: (inv, keg, overworld) => {
		let kelpSlot = -1;
		let fishSlots = [];
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item && item.typeId === "minecraft:dried_kelp") kelpSlot = slot;
			if (item && kippersFishIds.includes(item.typeId)) fishSlots.push(slot);
		}
		if (kelpSlot !== -1) consumeItem(inv, kelpSlot, 1);
		for (const slot of fishSlots) consumeItem(inv, slot, 1);
		addResult(inv, "brewinandchewin:kippers", 2);
	}
};
