var _a;
import * as mc from "@minecraft/server";
import { ComponentCompanion } from "../../npc/components/base";
import { PlayDeadTask } from "../tasks/playDead";
export class PlayDeadComponent extends ComponentCompanion {
    get PlayDeadThreshold() {
        return this.playDeadThreshold;
    }
    set PlayDeadThreshold(v) {
        var _b;
        this.playDeadThreshold = v;
        (_b = this.npc) === null || _b === void 0 ? void 0 : _b.Entity.setProperty('play_dead_threshold', v);
    }
    constructor(threshold, playDeadOptions) {
        super();
        this.playDeadThreshold = 1;
        this.playDeadOptions = {
            deathSeconds: 20,
            deathEvents: ["start_is_dead", "end_guard_owner", "end_goal_idle", "remove_player"],
            recoverySeconds: 3.5,
            recoveryEvents: ["end_is_dead", "add_player"]
        };
        this.eventBinding = this.onDeath.bind(this);
        this.PlayDeadThreshold = threshold;
        this.playDeadOptions = playDeadOptions !== null && playDeadOptions !== void 0 ? playDeadOptions : this.playDeadOptions;
    }
    async onComponentAdded(npc) {
        super.onComponentAdded(npc);
        this.PlayDeadThreshold = this.PlayDeadThreshold;
        new PlayDeadTask(npc, this.playDeadOptions).cleanupTask();
        mc.world.afterEvents.entityHurt.subscribe(this.eventBinding, { entityTypes: [npc.Entity.typeId] });
    }
    async onComponentRemoved(npc) {
        mc.world.afterEvents.entityHurt.unsubscribe(this.eventBinding);
    }
    onDeath(event) {
        var _b;
        if (event.hurtEntity.id === ((_b = this.npc) === null || _b === void 0 ? void 0 : _b.ID) && this.npc.Health.currentValue <= this.PlayDeadThreshold) {
            this.npc.startTask(new PlayDeadTask(this.npc, this.playDeadOptions), true);
        }
    }
}
_a = PlayDeadComponent;
PlayDeadComponent.ComponentId = _a.name;
