import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
import { ComponentCompanion } from "./base";
export class PlayerInteract extends ComponentCompanion {
    constructor(interact = () => { var _a; utils.mclog(`${(_a = this.npc) === null || _a === void 0 ? void 0 : _a.ID} Interact`); }) {
        super();
        this.eventBinding = this.onInteract.bind(this);
        this.interact = interact;
    }
    async onComponentAdded(npc) {
        super.onComponentAdded(npc);
        mc.world.afterEvents.dataDrivenEntityTrigger.subscribe(this.eventBinding, { entityTypes: ["minecraft:player"], eventTypes: ["minecraft:remove_raid_trigger"] });
    }
    async onComponentRemoved(npc) {
        mc.world.afterEvents.dataDrivenEntityTrigger.unsubscribe(this.eventBinding);
    }
    onInteract(event) {
        if (event.entity instanceof mc.Player) {
            const player = new utils.SafetyCheckedPlayer(event.entity);
            const on_interact = (subEvent) => {
                var _a;
                if (subEvent.entity.id === ((_a = this.npc) === null || _a === void 0 ? void 0 : _a.Entity.id)) {
                    this.interact(player);
                    mc.world.afterEvents.dataDrivenEntityTrigger.unsubscribe(on_interact);
                }
            };
            mc.world.afterEvents.dataDrivenEntityTrigger.subscribe(on_interact, { eventTypes: ["on_interact"] });
            utils.waitForEvent(1, () => mc.world.afterEvents.dataDrivenEntityTrigger.unsubscribe(on_interact));
        }
    }
}
