import { world } from "@minecraft/server";
import { decreaseItemDurabilityInHand, } from "../../helper/utils";
export class DurabilityItem {
    // Constructor to initialize the DurabilityItem class
    constructor(config) {
        this.config = config;
    }
    // Register the durability behavior
    static registerBehavior(config) {
        const eventItem = new DurabilityItem(config);
        this.items.push(eventItem);
    }
    // Initialize the durability system (listening to block break event)
    static initialize() {
        world.afterEvents.playerBreakBlock.subscribe(this.onBlockBreak);
    }
    // Handle block break event to decrease durability
    static onBlockBreak(event) {
        const durabilityItem = DurabilityItem.getItem(event.itemStackBeforeBreak?.typeId);
        if (!durabilityItem)
            return;
        decreaseItemDurabilityInHand(event.player, {
            ignoreInCreative: durabilityItem.config.ignoreInCreative,
            destructionSound: durabilityItem.config.destructionSound,
            convertInto: durabilityItem.config.convertInto,
        });
    }
    // Helper method to get the DurabilityItem by item identifier
    static getItem(itemId) {
        return this.items.find((item) => item.config.id === itemId);
    }
}
DurabilityItem.items = [];
