import * as mc from "@minecraft/server";
import { ComponentNPC } from "./base";
export class DestructOnRemoveEventComponent extends ComponentNPC {
    constructor() {
        super(...arguments);
        this.eventBinding = this.onDestruct.bind(this);
    }
    async onComponentAdded(npc) {
        super.onComponentAdded(npc);
        mc.world.afterEvents.entityRemove.subscribe(this.eventBinding, { entityTypes: [npc.Entity.typeId] });
    }
    async onComponentRemoved(npc) {
        mc.world.afterEvents.entityRemove.unsubscribe(this.eventBinding);
    }
    onDestruct(event) {
        var _a, _b;
        if (event.removedEntityId === ((_a = this.npc) === null || _a === void 0 ? void 0 : _a.ID)) {
            (_b = this.npc) === null || _b === void 0 ? void 0 : _b.destructor("Entity Despawn");
        }
    }
}
