import { EquipmentSlot, system, world } from "@minecraft/server";
export class MainHandChangeEventData {
    constructor(oldItemStackData, newItemStack, player) {
        this.oldItemStackData = oldItemStackData;
        this.newItemStack = newItemStack;
        this.player = player;
    }
}
const callbacks = [];
export class MainHandChangeEvent {
    constructor() {
        this.id = callbacks.length;
    }
    subscribe(callback) {
        callbacks.push(callback);
        return this.id;
    }
    static unsubscribe(id) {
        callbacks.splice(id, 1);
    }
}
const record = {};
system.runInterval(() => {
    var _a, _b, _c, _d;
    for (const player of world.getPlayers()) {
        const equipment = player.getComponent('equippable');
        const hand = equipment?.getEquipment(EquipmentSlot.Mainhand);
        const data = {
            typeId: hand === null || hand === void 0 ? void 0 : hand.typeId,
            nameTag: hand === null || hand === void 0 ? void 0 : hand.nameTag,
            lore: (_a = hand === null || hand === void 0 ? void 0 : hand.getLore()) !== null && _a !== void 0 ? _a : [],
            lockMode: hand === null || hand === void 0 ? void 0 : hand.lockMode,
            keepOnDeath: hand === null || hand === void 0 ? void 0 : hand.keepOnDeath,
            canDestroy: (_b = hand === null || hand === void 0 ? void 0 : hand.getCanDestroy()) !== null && _b !== void 0 ? _b : [],
            canPlaceOn: (_c = hand === null || hand === void 0 ? void 0 : hand.getCanPlaceOn()) !== null && _c !== void 0 ? _c : [],
            properties: (_d = hand === null || hand === void 0 ? void 0 : hand.getDynamicPropertyIds()) === null || _d === void 0 ? void 0 : _d.map(id => { return { id, value: hand === null || hand === void 0 ? void 0 : hand.getDynamicProperty(id) }; }),
            SlotIndex: player.selectedSlotIndex
        };
        if (!record.hasOwnProperty(player.id)) {
            record[player.id] = data;
            continue;
        }
        const oldItemStack = record[player.id];
        if (JSON.stringify(oldItemStack) === JSON.stringify(data))
            continue;
        callbacks.forEach(callback => {
            callback(new MainHandChangeEventData(oldItemStack, hand, player));
        });
        record[player.id] = data;
    }
});