import { world, EquipmentSlot, } from "@minecraft/server";
import { getItemInSlot } from "../../helper/utils";
export class EventItem {
    // Constructor to initialize the OnEventItem class with optional o
    constructor(config) {
        this.config = config;
    }
    // Register the event handlers for an item
    static registerBehavior(config) {
        const eventItem = new EventItem(config);
        this.items.push(eventItem);
    }
    // get item
    static getItem(id) {
        return this.items.find((item) => item.config.id === id);
    }
    // Initialize the custom event handling system
    static initialize() {
        // Block Break event
        world.afterEvents.playerBreakBlock.subscribe((event) => {
            const { player, block } = event;
            const itemStack = getItemInSlot(player, EquipmentSlot.Mainhand);
            if (!itemStack)
                return;
            const item = EventItem.getItem(itemStack.typeId);
            if (!item)
                return;
            if (itemStack?.typeId === item.config.id && item.config.onBlockBreak) {
                item.config.onBlockBreak(player, block.location);
            }
        });
        // Entity Hurt event
        world.afterEvents.entityHurt.subscribe((event) => {
            const entity = event.hurtEntity;
            const source = event.damageSource.damagingEntity;
            if (!source)
                return;
            const itemStack = getItemInSlot(source, EquipmentSlot.Mainhand);
            if (!itemStack)
                return;
            const item = EventItem.getItem(itemStack.typeId);
            if (!item)
                return;
            if (itemStack?.typeId === item.config.id && item.config.onEntityHit) {
                item.config.onEntityHit(source, entity);
            }
        });
        world.afterEvents.itemUse.subscribe((event) => {
            const { itemStack, source: player } = event;
            if (!itemStack)
                return;
            const item = EventItem.getItem(itemStack.typeId);
            if (!item)
                return;
            if (itemStack?.typeId === item.config.id && item.config.onUse) {
                item.config.onUse(player);
            }
        });
        world.afterEvents.itemUseOn.subscribe((event) => {
            const { itemStack, source: player } = event;
            if (!itemStack)
                return;
            const item = EventItem.getItem(itemStack.typeId);
            if (!item)
                return;
            if (itemStack?.typeId === item.config.id && item.config.onUseOnAfter) {
                item.config.onUseOnAfter(event);
            }
        });
        world.beforeEvents.itemUseOn.subscribe((event) => {
            const { itemStack, source: player } = event;
            if (!itemStack)
                return;
            const item = EventItem.getItem(itemStack.typeId);
            if (!item)
                return;
            if (itemStack?.typeId === item.config.id && item.config.onUseOnBefore) {
                item.config.onUseOnBefore(event);
            }
        });
        // Complete Use event
        world.afterEvents.itemCompleteUse.subscribe((event) => {
            const { itemStack, source: player } = event;
            if (!itemStack)
                return;
            const item = EventItem.getItem(itemStack.typeId);
            if (!item)
                return;
            if (itemStack?.typeId === item.config.id && item.config.onCompleteUse) {
                item.config.onCompleteUse(player);
            }
        });
        // Release event
        world.afterEvents.itemReleaseUse.subscribe((event) => {
            const { itemStack, source: player } = event;
            if (!itemStack)
                return;
            const item = EventItem.getItem(itemStack.typeId);
            if (!item)
                return;
            if (itemStack?.typeId === item.config.id && item.config.onRelease) {
                item.config.onRelease(player);
            }
        });
    }
}
EventItem.items = [];
