import { system, world } from "@minecraft/server";

const recipes = [
    {
        output: "onion_rings",
        ingredients: [
            { item: "ww_dx:onion", count: 1 },
            { item: "ww_dx:flour", count: 1 },
            { item: "ww_dx:olive_oil", count: 1 },
        ],
    },
    {
        output: "tortilla",
        ingredients: [
            { item: "ww_dx:dough", count: 2 },
            { item: "ww_dx:olive_oil", count: 1 },
        ],
    },
    {
        output: "vegetable_stir_fry",
        ingredients: [
            { item: "ww_dx:broccoli", count: 1 },
            { item: "ww_dx:zucchini", count: 1 },
            { item: "ww_dx:soy_sauce", count: 1 },
            { item: "ww_dx:garlic", count: 1 },

        ],
    },
    {
        output: "chicken_fried_rice",
        ingredients: [
            { item: "minecraft:chicken", count: 1 },
            { item: "ww_dx:rice", count: 1 },
            { item: "ww_dx:soy_sauce", count: 1 },
            { item: "minecraft:carrot", count: 1 },
            { item: "ww_dx:peas", count: 1 },
            { item: "minecraft:bowl", count: 1 },
        ],
    },
    {
        output: "grilled_cheese_sandwich",
        ingredients: [
            { item: "minecraft:bread", count: 2 },
            { item: "ww_dx:cheese", count: 1 },
        ],
    },
    {
        output: "fish_tacos",
        ingredients: [
            { item: "minecraft:cod", count: 1 },
            { item: "ww_dx:tortilla", count: 1 },
            { item: "ww_dx:lettuce", count: 1 },
            { item: "ww_dx:cheese", count: 1 },
        ],
    },
    {
        output: "fish_tacos",
        ingredients: [
            { item: "minecraft:salmon", count: 1 },
            { item: "ww_dx:tortilla", count: 1 },
            { item: "ww_dx:lettuce", count: 1 },
            { item: "ww_dx:cheese", count: 1 },
        ],
    },
    {
        output: "fish_tacos",
        ingredients: [
            { item: "minecraft:tropical_fish", count: 1 },
            { item: "ww_dx:tortilla", count: 1 },
            { item: "ww_dx:lettuce", count: 1 },
            { item: "ww_dx:cheese", count: 1 },
        ],
    },
    {
        output: "tacos",
        ingredients: [
            { item: "ww_dx:raw_ground_beef", count: 1 },
            { item: "ww_dx:tortilla", count: 1 },
            { item: "ww_dx:lettuce", count: 1 },
            { item: "ww_dx:cheese", count: 1 },
        ],
    },
    {
        output: "beef_burrito",
        ingredients: [
            { item: "ww_dx:raw_ground_beef", count: 1 },
            { item: "ww_dx:tortilla", count: 1 },
            { item: "ww_dx:lettuce", count: 1 },
            { item: "ww_dx:chili_powder", count: 1 },
            { item: "ww_dx:corn", count: 1 },
            { item: "ww_dx:fajita_veggies", count: 1 },
        ],
    },
    {
        output: "chicken_burrito",
        ingredients: [
            { item: "minecraft:chicken", count: 1 },
            { item: "ww_dx:tortilla", count: 1 },
            { item: "ww_dx:lettuce", count: 1 },
            { item: "ww_dx:chili_powder", count: 1 },
            { item: "ww_dx:corn", count: 1 },
            { item: "ww_dx:fajita_veggies", count: 1 },
        ],
    },
    {
        output: "steak_burrito",
        ingredients: [
            { item: "minecraft:beef", count: 1 },
            { item: "ww_dx:tortilla", count: 1 },
            { item: "ww_dx:lettuce", count: 1 },
            { item: "ww_dx:chili_powder", count: 1 },
            { item: "ww_dx:corn", count: 1 },
            { item: "ww_dx:fajita_veggies", count: 1 },
        ],
    },
    {
        output: "fajita_veggies",
        ingredients: [
            { item: "ww_dx:red_bell_pepper", count: 1 },
            { item: "ww_dx:green_bell_pepper", count: 1 },
            { item: "ww_dx:yellow_bell_pepper", count: 1 },
            { item: "ww_dx:onion", count: 1 },
        ],
    },
];


const frying_pan = new Map(); 


function processFryingPanInventory(entity) {
    const id = entity.id;

    
    if (frying_pan.get(id)?.isProcessing) {
        return;
    }

    const inventoryComponent = entity.getComponent("minecraft:inventory");

    if (!inventoryComponent || !inventoryComponent.container) {
       
        return;
    }

    const inventory = inventoryComponent.container;

    for (const recipe of recipes) {
       
        let matches = true;

        const ingredientsMap = new Map(
            recipe.ingredients.map((ingredient) => [ingredient.item, ingredient.count])
        );

        for (let i = 0; i < inventory.size; i++) {
            const item = inventory.getItem(i);
            if (item) {
                if (ingredientsMap.has(item.typeId)) {
                    const remainingCount = ingredientsMap.get(item.typeId) - item.amount;
                    if (remainingCount <= 0) {
                        ingredientsMap.delete(item.typeId);
                    } else {
                        ingredientsMap.set(item.typeId, remainingCount);
                    }
                } else {
                   
                    matches = false;
                    break;
                }
            }
        }

        if (matches && ingredientsMap.size === 0) {
            entity.runCommandAsync("summon ww_dx:processing_particle ~ ~1 ~")

          
            for (const ingredient of recipe.ingredients) {
                let remainingToRemove = ingredient.count;

                for (let i = 0; i < inventory.size; i++) {
                    const item = inventory.getItem(i);
                    if (item && item.typeId === ingredient.item) {
                        if (item.amount > remainingToRemove) {
                            item.amount -= remainingToRemove;
                            inventory.setItem(i, item);
                            break;
                        } else {
                            remainingToRemove -= item.amount;
                            inventory.setItem(i, undefined);
                        }
                    }
                }
            }

          

            frying_pan.set(id, { isProcessing: true });
            system.runTimeout(() => {
                if (!entity.isValid()) return;
                const { x, y, z } = entity.location;
                entity.runCommandAsync(`loot spawn ${x} ${y + 1} ${z} loot ${recipe.output}`);
                entity.runCommandAsync("event entity @e[type=ww_dx:processing_particle,r=1] ww_dx:despawn")
                entity.runCommandAsync("playsound ww_dx:finish_cooking @a[r=5]")
               
                frying_pan.set(id, { isProcessing: false });
            }, 200);

            return; 
        } else {
   
        }
    }

}
let worldInitialize = false;
world.afterEvents.playerSpawn.subscribe((event) => {
    worldInitialize = true;

})
function checkFryingPan() {
    if(!worldInitialize)return;
    const currentDimension = world.getDimension(world.getPlayers()[0].dimension.id);

    for (const entity of currentDimension.getEntities()) {
        if (entity.typeId === "ww_dx:frying_pan") {
            if (!frying_pan.has(entity.id)) {
                frying_pan.set(entity.id, { isProcessing: false });

            }

            processFryingPanInventory(entity);
        }
    }
}


system.runInterval(() => {
    checkFryingPan();
}, 20); 
