import * as mc from "@minecraft/server";
import { ComponentCompanion } from "./base";
export class OnOwnerHurtEventComponent extends ComponentCompanion {
    constructor() {
        super(...arguments);
        this.eventBinding = this.onHurt.bind(this);
        this.hurtFilters = [];
    }
    async onComponentAdded(npc) {
        super.onComponentAdded(npc);
        mc.world.afterEvents.entityHurt.subscribe(this.eventBinding, { entityTypes: [npc.Owner.typeId] });
    }
    async onComponentRemoved(npc) {
        mc.world.afterEvents.entityHurt.unsubscribe(this.eventBinding);
    }
    onHurt(event) {
        var _a;
        if (event.hurtEntity.id === ((_a = this.npc) === null || _a === void 0 ? void 0 : _a.Owner.id)) {
            for (const hurtFilter of this.hurtFilters) {
                if (hurtFilter.filter(event.damageSource)) {
                    hurtFilter.event(event);
                    return;
                }
            }
            this.npc.broadcastEvent("owner_hurt");
        }
    }
}
