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

let fakeInventory = "null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0null0";

function UpdateGolemInventories() {
    const allGolemsOverworld = world.getDimension("overworld").getEntities({type: 'zpig:obsidian_golem',});
    const allGolemsNether = world.getDimension("nether").getEntities({type: 'zpig:obsidian_golem',});
    const allGolemsEnd = world.getDimension("the_end").getEntities({type: 'zpig:obsidian_golem',});
    const allGolems = allGolemsOverworld.concat(allGolemsNether, allGolemsEnd);
    if (allGolems.length > 1) {
        allGolems.forEach((golem) => {
            golem.removeTag("zpig:last");
        });
    }
    else if (allGolems.length === 1) {
        allGolems.forEach((golem) => {
            golem.addTag("zpig:last");
        });
    }
    const validGolemsOverworld = world.getDimension("overworld").getEntities({type: 'zpig:obsidian_golem', excludeTags: ['zpig:spawn'],});
    const validGolemsNether = world.getDimension("nether").getEntities({type: 'zpig:obsidian_golem', excludeTags: ['zpig:spawn'],});
    const validGolemsEnd = world.getDimension("the_end").getEntities({type: 'zpig:obsidian_golem', excludeTags: ['zpig:spawn'],});
    const validGolems = validGolemsOverworld.concat(validGolemsNether, validGolemsEnd);
    if (validGolems.length > 1) {
        const newGolemsOverworld = world.getDimension("overworld").getEntities({type: 'zpig:obsidian_golem', tags: ['zpig:spawn'],});
        const newGolemsNether = world.getDimension("nether").getEntities({type: 'zpig:obsidian_golem', tags: ['zpig:spawn'],});
        const newGolemsEnd = world.getDimension("the_end").getEntities({type: 'zpig:obsidian_golem', tags: ['zpig:spawn'],});
        const newGolems = newGolemsOverworld.concat(newGolemsNether, newGolemsEnd);
        let inventoryCounts = new Map();
        validGolems.forEach((golem) => {
            newGolems.forEach((spawnedGolem) => {
                for (let i = 0; i < 27; i++) {
                    const goodItem = golem.getComponent('inventory').container.getItem(i);
                    spawnedGolem.getComponent('inventory').container.setItem(i, goodItem);
                }
            });
            let serializedInventory = "";
            for (let i = 0; i < 27; i++) {
                const item = golem.getComponent('inventory').container.getItem(i);
                serializedInventory += item ? item.typeId : "null";
                serializedInventory += item ? item.amount : "0";
            }
            inventoryCounts.set(serializedInventory, (inventoryCounts.get(serializedInventory) || 0) + 1);
        });
        inventoryCounts.set(fakeInventory, (inventoryCounts.get(fakeInventory) || 0) + 1);
        let uniqueSerializedInventory = [...inventoryCounts.entries()].find(([_, count]) => count === 1);
        if (uniqueSerializedInventory) {
            uniqueSerializedInventory = uniqueSerializedInventory[0];
            validGolems.forEach((validGolem) => {
                let serializedInventory = "";
                for (let i = 0; i < 27; i++) {
                    const item = validGolem.getComponent('inventory').container.getItem(i);
                    serializedInventory += item ? item.typeId : "null";
                    serializedInventory += item ? item.amount : "0";
                }
                if (serializedInventory === uniqueSerializedInventory) {
                    fakeInventory = uniqueSerializedInventory;
                    validGolems.forEach((obsidianGolem) => {
                        for (let i = 0; i < 27; i++) {
                            const uniqueItem = validGolem.getComponent('inventory').container.getItem(i);
                            obsidianGolem.getComponent('inventory').container.setItem(i, uniqueItem);
                        }
                    });
                }
            });
        }
    }
}

system.runInterval(UpdateGolemInventories, 1);