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

// Costume IDs mapped to variable names
const chestcostumeVariables = {
    "just:inflatable_trex": { variable: "trex_active", value: 1 },
    "just:easter_bunny": { variable: "bunny_active", value: 1 },
    "just:easter_bunny_costume": { variable: "bunny_costume_active", value: 1 },
    "just:sus": { variable: "imposter_mode", value: 1 },
    "just:sus_imposter": { variable: "sus_active", value: 1 },
    "just:castle_crasher": { variable: "crasher_active", value: 1}


};


// Track currently applied costume per player
const currentCostume = new Map();

system.runInterval(() => {
    for (const player of world.getPlayers()) {
        const equipment = player.getComponent("minecraft:equippable");
        if (!equipment) continue;

        const chest = equipment.getEquipment("Chest");
        const chestId = chest?.typeId ?? null;

        const newCostume = chestcostumeVariables[chestId];
        const current = currentCostume.get(player.id);

        // If costume changed (either new one or unequipped)
        if (current?.variable !== newCostume?.variable) {
            const dummyId = Math.floor(Math.random() * 100000); // Generate once per cycle

            // Stop previous animation if any
            if (current) {
                player.playAnimation("animation.sny_bc.data_transfer.empty", {
                    stopExpression: `t.target='${player.name}';t.${current.variable}=0;t.dummy_id=${dummyId};return 0;`
                });
                //player.sendMessage(`§cUnset ${current.variable}`);
            }

            // Start new animation if a new costume is equipped
            if (newCostume) {
                player.playAnimation("animation.sny_bc.data_transfer.empty", {
                    stopExpression: `t.target='${player.name}';t.${newCostume.variable}=${newCostume.value};t.dummy_id=${dummyId};return 0;`
                });
                //player.sendMessage(`§aSet ${newCostume.variable} = ${newCostume.value}`);
                currentCostume.set(player.id, newCostume); // Update the map with new costume
            } else {
                currentCostume.delete(player.id); // Player removed chest item
            }
        }
    }
}, 10); // every 0.5 seconds

//costume to hats
