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

const swapItems = ["minecraft:torch", "minecraft:redstone_torch", "minecraft:soul_torch", "minecraft:lava_bucket"];

world.beforeEvents.itemUse.subscribe((e) => {
    const { source, itemStack } = e

    const selectedSlot = source.selectedSlotIndex;


    system.runTimeout(() => {
        if (selectedSlot !== source.selectedSlotIndex) return;
        if (itemStack?.hasTag("ray:miner_helmet")) return;
        if (!source.getComponent("inventory").container.getItem(selectedSlot)) return;
        if (!source.hasTag("ray:PlaceBlock")) {
            const equip = source.getComponent("equippable");
            const getEquipment = (slot) => equip.getEquipment(slot);

            if (itemStack?.typeId.includes("shield") || itemStack?.typeId.includes("totem_of_undying")) return;

            if (
                getEquipment(EquipmentSlot.Offhand)?.typeId.includes("shield") ||
                getEquipment(EquipmentSlot.Offhand)?.typeId.includes("totem_of_undying")
            )
                return;

            if (
                swapItems.some((w) => itemStack.typeId.includes(w)) ||
                (itemStack.typeId.includes("ray") && itemStack.typeId.includes("torch"))
            ) {
                if (getEquipment(EquipmentSlot.Offhand)) {
                    equip.setEquipment(EquipmentSlot.Mainhand, getEquipment(EquipmentSlot.Offhand));
                } else {
                    equip.setEquipment(EquipmentSlot.Mainhand, new ItemStack("minecraft:air"));
                }
                source.runCommand(`replaceitem entity @s slot.weapon.offhand 0 ${itemStack.typeId} ${itemStack.amount}`);
            }
        } else {
            source.removeTag("ray:PlaceBlock");
        }
    }, 1);

})

world.afterEvents.playerPlaceBlock.subscribe(({ player }) => {
    player.addTag("ray:PlaceBlock");
});

world.afterEvents.itemStartUseOn.subscribe(({ source }) => {
    source.addTag("ray:PlaceBlock");
});