package dev.latvian.mods.kubejs.recipe.ingredientaction;

import com.mojang.serialization.Codec;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingInput;

import java.util.List;

public interface IngredientAction {
	Codec<IngredientAction> CODEC = IngredientActionType.CODEC.dispatch("type", IngredientAction::getType, IngredientActionType::codec);
	StreamCodec<RegistryFriendlyByteBuf, IngredientAction> STREAM_CODEC = IngredientActionType.STREAM_CODEC.dispatch(IngredientAction::getType, IngredientActionType::streamCodec);

	static ItemStack getRemaining(CraftingInput input, int index, List<IngredientActionHolder> ingredientActions) {
		var stack = input.getItem(index);

		if (stack == null || stack.isEmpty()) {
			return ItemStack.EMPTY;
		}

		for (var holder : ingredientActions) {
			if (holder.filter().checkFilter(index, stack)) {
				return holder.action().transform(stack.copy(), index, input);
			}
		}

		var remainder = stack.getCraftingRemainder();
		if (remainder != null) {
			return remainder.create();
		}

		return ItemStack.EMPTY;
	}

	IngredientActionType<?> getType();

	ItemStack transform(ItemStack old, int index, CraftingInput input);
}
