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

export default {
	name: "pickled_pickles",
	cookTime: cookTimeTotal,
	requiredTemp: "2",
	isValid: (inv, tempSlot) => {
		let seaPickleSlots = [];
		let glowBerriesSlot = -1;
		let filled = 0;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item) {
				filled++;
				if (item.typeId === "minecraft:sea_pickle" && seaPickleSlots.length < 2) {
					seaPickleSlots.push(slot);
				} else if (item.typeId === "minecraft:glow_berries" && glowBerriesSlot === -1) {
					glowBerriesSlot = slot;
				}
			}
		}
		const honey = inv.getItem(liquidSlot);
		const result = inv.getItem(resultSlot);
		return seaPickleSlots.length === 2 &&
			   glowBerriesSlot !== -1 &&
			   filled === 3 &&
			   honey && honey.typeId === "minecraft:honey_bottle" &&
			   !inv.getItem(6) &&
			   (!result || result.typeId === "brewinandchewin:pickled_pickles") &&
			   tempSlot && tempSlot.nameTag === "2";
	},
	onComplete: (inv, keg, overworld) => {
		let seaPickleSlots = [];
		let glowBerriesSlot = -1;
		for (let slot = 0; slot < 4; slot++) {
			const item = inv.getItem(slot);
			if (item && item.typeId === "minecraft:sea_pickle") seaPickleSlots.push(slot);
			if (item && item.typeId === "minecraft:glow_berries") glowBerriesSlot = slot;
		}
		for (const slot of seaPickleSlots) consumeItem(inv, slot, 1);
		if (glowBerriesSlot !== -1) consumeItem(inv, glowBerriesSlot, 1);
		const honey = inv.getItem(liquidSlot);
		if (honey) {
			consumeItem(inv, liquidSlot, 1);
			returnTaraToBuffer(inv, "minecraft:glass_bottle", 1);
		}
		addResult(inv, "brewinandchewin:pickled_pickles", 2);
	}
};
