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

const itemGroups = {
    "green": new Set(["mebesm:grabpack_blue_g", "mebesm:grabpack_green_active", "mebesm:grabpack_green_and_blue", "mebesm:grabpack_green"]),
    "orange": new Set(["mebesm:grabpack_blue_o", "mebesm:grabpack_orange"]),
    "omni": new Set(["mebesm:grabpack_blue_om", "mebesm:grabpack_omni_and_blue", "mebesm:grabpack_omni"]),
    "purple": new Set(["mebesm:grabpack_blue_p", "mebesm:grabpack_purple_and_blue", "mebesm:grabpack_purple"]),
    "pressure": new Set(["mebesm:grabpack_blue_pr", "mebesm:grabpack_pressure_and_blue", "mebesm:grabpack_pressure"]),
    "red": new Set(["mebesm:grabpack_blue_r", "mebesm:grabpack_red_and_blue", "mebesm:grabpack_red"]),
    "yellow": new Set(["mebesm:grabpack_blue_y", "mebesm:grabpack_yellow_and_blue", "mebesm:grabpack_yellow_energi", "mebesm:grabpack_yellow_fire", "mebesm:grabpack_yellow_ice", "mebesm:grabpack_yellow"]),
    "blue": new Set(["mebesm:grabpack_blue"])
};

const leftItems = new Set([
    "mebesm:grabpack_blue", "mebesm:grabpack_blue_g", "mebesm:grabpack_blue_o",
    "mebesm:grabpack_blue_om", "mebesm:grabpack_blue_p", "mebesm:grabpack_blue_pr",
    "mebesm:grabpack_blue_r", "mebesm:grabpack_blue_y"
]);

const rightItems = new Set([
    "mebesm:grabpack_green", "mebesm:grabpack_green_active", "mebesm:grabpack_omni",
    "mebesm:grabpack_orange", "mebesm:grabpack_pressure", "mebesm:grabpack_purple",
    "mebesm:grabpack_red", "mebesm:grabpack_yellow", "mebesm:grabpack_yellow_energi",
    "mebesm:grabpack_yellow_fire", "mebesm:grabpack_yellow_ice"
]);

const leftRightItems = new Set([
    "mebesm:grabpack_green_and_blue", "mebesm:grabpack_omni_and_blue",
    "mebesm:grabpack_pressure_and_blue", "mebesm:grabpack_yellow_and_blue",
    "mebesm:grabpack_red_and_blue", "mebesm:grabpack_purple_and_blue"
]);

const playerData = new Map();

function getGroupName(itemId) {
    if (!itemId) return "air";
    for (const [groupName, items] of Object.entries(itemGroups)) {
        if (items.has(itemId)) return groupName;
    }
    return "unknown";
}

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

        const item = equippable.getEquipment(EquipmentSlot.Mainhand);
        const currentId = item?.typeId;
        const currentGroup = getGroupName(currentId);

        let lastData = playerData.get(player.id);
        if (!lastData) {
            lastData = { lastId: "", lastGroup: "" };
            playerData.set(player.id, lastData);
        }

        if (currentId !== lastData.lastId) {
            if (currentId && currentGroup !== "unknown") {
                if (currentGroup !== lastData.lastGroup) {
                    player.playAnimation("animation.grabpack.other");
                }
                else {
                    if (leftRightItems.has(currentId)) {
                        player.playAnimation("animation.grabpack.change_left_right");
                    } else if (leftItems.has(currentId)) {
                        player.playAnimation("animation.grabpack.change_left");
                    } else if (rightItems.has(currentId)) {
                        player.playAnimation("animation.grabpack.change_right");
                    }
                }
            }

            lastData.lastId = currentId;
            lastData.lastGroup = currentGroup;
        }
    }
}, 1);

world.afterEvents.playerLeave.subscribe(e => playerData.delete(e.playerId));