import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
import { ComponentNPC } from "./base";
export class DestructEventComponent extends ComponentNPC {
    constructor() {
        super(...arguments);
        this.eventBinding = this.onDestruct.bind(this);
    }
    async onComponentAdded(npc) {
        super.onComponentAdded(npc);
        mc.system.afterEvents.scriptEventReceive.subscribe(this.eventBinding, { namespaces: [utils.GlobalNamespace.NamespaceUnformatted] });
    }
    async onComponentRemoved(npc) {
        mc.system.afterEvents.scriptEventReceive.unsubscribe(this.eventBinding);
    }
    onDestruct(event) {
        var _a, _b, _c, _d;
        if (event.id === utils.GlobalNamespace.Namespace + "on_destruct" && ((_a = event.sourceEntity) === null || _a === void 0 ? void 0 : _a.id) === ((_b = this.npc) === null || _b === void 0 ? void 0 : _b.ID)) {
            (_c = this.npc) === null || _c === void 0 ? void 0 : _c.broadcastEvent("natural_despawn");
            (_d = this.npc) === null || _d === void 0 ? void 0 : _d.destructor("Script Event On Destruct");
        }
    }
}
