import { world, ItemStack, GameMode } from '@minecraft/server';

// This array defines the tool items that will lose durability.
const toolTypeIds = [
    'we:wild_wooden_pickaxe',
    'we:wild_wooden_axe',
    'we:wild_wooden_shovel',
    'we:wild_wooden_hoe',
    'we:wild_stone_pickaxe',
    'we:wild_stone_axe',
    'we:wild_stone_shovel',
    'we:wild_stone_hoe',
    'we:wild_iron_pickaxe',
    'we:wild_iron_axe',
    'we:wild_iron_shovel',
    'we:wild_iron_hoe',
    'we:wild_golden_pickaxe',
    'we:wild_golden_axe',
    'we:wild_golden_shovel',
    'we:wild_golden_hoe',
    'we:wild_diamond_pickaxe',
    'we:wild_diamond_axe',
    'we:wild_diamond_shovel',
    'we:wild_diamond_hoe',
    'we:wild_netherite_pickaxe',
    'we:wild_netherite_axe',
    'we:wild_netherite_shovel',
    'we:wild_netherite_hoe'
];

// This array defines the weapon items that will lose durability.
const weaponTypeIds = [
];

world.afterEvents.playerBreakBlock.subscribe(evd => {
    const { player, itemStackBeforeBreak: itemUsed } = evd;

    // This returns if itemUsed is undefined or if the player is in Creative.
    if (!itemUsed || player.matches({ gameMode: GameMode.creative })) return;

    if (toolTypeIds.includes(itemUsed.typeId) || weaponTypeIds.includes(itemUsed.typeId)) {
        // This retrieves the player's equippable component.
        const playerEquippableComp = player.getComponent("equippable");

        // This returns if playerEquippableComp is undefined.
        if (!playerEquippableComp) return;

        // This retrieves the enchantable component of the item.
        const itemEnchantmentComp = itemUsed.getComponent("minecraft:enchantable");
        const unbreakingLevel = itemEnchantmentComp?.getEnchantment("unbreaking")?.level ?? 0;

        // Calculates the chance of an item breaking based on its unbreaking level. This is the vanilla unbreaking formula.
        const breakChance = 100 / (unbreakingLevel + 1);
        // Generates a random chance value between 0 and 100.
        const randomizeChance = Math.random() * 100;

        // This returns if breakChance is less than randomizeChance.
        if (breakChance < randomizeChance) return;

        // This retrieves the durability component of the item.
        const itemUsedDurabilityComp = itemUsed.getComponent("durability");

        // This returns if itemUsedDurabilityComp is undefined.
        if (!itemUsedDurabilityComp) return;

        let durabilityModifier = 0;
        if (toolTypeIds.includes(itemUsed.typeId)) {
            // If the item is a tool, then it will set the durability modifier to 1.
            durabilityModifier = 1;
        } else {
            // If the item is a weapon, then it will set the durability modifier to 2.
            durabilityModifier = 2;
        }

        // This will set the new durability value.
        itemUsedDurabilityComp.damage += durabilityModifier;

        // Declares and checks if the item is out of durability
        const maxDurability = itemUsedDurabilityComp.maxDurability
        const currentDamage = itemUsedDurabilityComp.damage
        if (currentDamage >= maxDurability) {

            // If the item is out of durability, plays the item breaking sound and removes the item
            player.playSound('random.break', { pitch: 1, location: player.location, volume: 1 })
            playerEquippableComp.setEquipment("Mainhand", new ItemStack('minecraft:air', 1));
        }
        else if (currentDamage < maxDurability) {

            // This sets the item in the player's selected slot.
            playerEquippableComp.setEquipment("Mainhand", itemUsed);
        }
    }
});