const FD_POT_RECIPES = [
    {
        id: 'farmersdelight:chocolate_pie',
        result: 'farmersdelight:chocolate_pie',
        ingredients: [
            { item: 'farmersdelight:pie_crust' },
            { item: 'minecraft:cocoa_beans' },
            { item: 'minecraft:cocoa_beans' },
            { tag: 'forge:milk' },
            { tag: 'forge:milk' },
            { item: 'minecraft:sugar' }
        ]
    },
    {
        id: 'farmersdelight:honey_glazed_ham_block',
        result: 'farmersdelight:honey_glazed_ham_block',
        ingredients: [
            { item: 'farmersdelight:smoked_ham' },
            { item: 'minecraft:honey_bottle' },
            { item: 'minecraft:sweet_berries' },
            { item: 'minecraft:sweet_berries' },
            { item: 'farmersdelight:cooked_rice' },
            { item: 'minecraft:bowl' }
        ]
    },
    {
        id: 'farmersdelight:roast_chicken_block',
        result: 'farmersdelight:roast_chicken_block',
        ingredients: [
            { item: 'minecraft:cooked_chicken' },
            { item: 'minecraft:baked_potato' },
            { item: 'minecraft:carrot' },
            { tag: 'forge:crops/onion' },
            { tag: 'forge:eggs' },
            { item: 'minecraft:bowl' }
        ]
    },
    {
        id: 'farmersdelight:shepherds_pie_block',
        result: 'farmersdelight:shepherds_pie_block',
        ingredients: [
            { tag: 'forge:cooked_mutton' },
            { item: 'minecraft:baked_potato' },
            { item: 'minecraft:baked_potato' },
            { tag: 'forge:milk' },
            { tag: 'forge:crops/onion' },
            { item: 'minecraft:bowl' }
        ]
    }
]

function addContainerIngredient(ingredients, container) {
    if (!container) return ingredients

    const item = container.item ?? container.id ?? container.containerId
    if (!item) return ingredients

    return ingredients.concat([{ item: item }])
}

ServerEvents.recipes(event => {
    FD_POT_RECIPES.forEach(recipe => {
        let ingredients = recipe.ingredients
        let container = recipe.container

        if (!container && ingredients.some(ingredient => ingredient.item === 'minecraft:bowl')) {
            container = { item: 'minecraft:bowl' }
            ingredients = ingredients.filter(ingredient => ingredient.item !== 'minecraft:bowl')
        }

        if (!container && ingredients.some(ingredient => ingredient.item === 'minecraft:glass_bottle')) {
            container = { item: 'minecraft:glass_bottle' }
            ingredients = ingredients.filter(ingredient => ingredient.item !== 'minecraft:glass_bottle')
        }

        if (!recipe.keepOriginal) event.remove({ id: recipe.id })

        const customRecipe = {
            type: 'farmersdelight:cooking',
            ingredients: ingredients,
            result: {
                item: recipe.result,
                count: recipe.count ?? 1
            },
            cookingtime: recipe.cookingtime ?? 200,
            experience: recipe.experience ?? 0.0,
            recipe_book_tab: recipe.recipeBookTab ?? 'meals'
        }

        if (container) customRecipe.container = container

        event.custom(customRecipe).id(recipe.potId ?? `kubejs:fd_pot/${recipe.id.replace(':', '_')}`)
        event.custom({
            type: 'create:mixing',
            heatRequirement: 'heated',
            ingredients: addContainerIngredient(ingredients, container),
            results: [
                {
                    item: recipe.result,
                    count: recipe.count ?? 1
                }
            ],
            processingTime: recipe.cookingtime ?? 200
        }).id(`kubejs:mixing/farmersdelight_pot/${recipe.id.replace(':', '/')}`)
    })
})
