import * as mc from "@minecraft/server";
import { ComponentCompanion } from "./base";
export class OnOwnerDeathEventComponent extends ComponentCompanion {
    constructor() {
        super(...arguments);
        this.deathEventBinding = this.onDeath.bind(this);
        this.spawnEventBinding = this.onRespawn.bind(this);
        this.isOwnerDead = false;
    }
    async onComponentAdded(npc) {
        super.onComponentAdded(npc);
        mc.world.afterEvents.entityDie.subscribe(this.deathEventBinding, { entityTypes: ["minecraft:player"] });
        mc.world.afterEvents.playerSpawn.subscribe(this.spawnEventBinding);
    }
    async onComponentRemoved(npc) {
        mc.world.afterEvents.entityDie.unsubscribe(this.deathEventBinding);
        mc.world.afterEvents.playerSpawn.unsubscribe(this.spawnEventBinding);
    }
    onDeath(event) {
        var _a;
        if (event.deadEntity.id === ((_a = this.npc) === null || _a === void 0 ? void 0 : _a.Owner.id)) {
            this.npc.broadcastEvent("owner_death");
            this.isOwnerDead = true;
        }
    }
    onRespawn(event) {
        var _a;
        if (this.isOwnerDead && event.player.id === ((_a = this.npc) === null || _a === void 0 ? void 0 : _a.Owner.id)) {
            this.npc.broadcastEvent("owner_respawn");
            this.isOwnerDead = false;
        }
    }
}
