import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
export class MoveToOwnerComponent extends utils.ActorComponent {
    constructor() {
        super(...arguments);
        this.eventBinding = this.onEventRecieved.bind(this);
    }
    onComponentAdded() {
        mc.system.afterEvents.scriptEventReceive.subscribe(this.eventBinding);
        this.actor.triggerEvent(this.options.startEvent);
        utils.mclog(`${this.actor.ActorId}'s ${this.id} timeout in ${this.actor.location.distance(this.options.owner.location)} seconds`, this.id);
        utils.waitForEvent(this.actor.location.distance(this.options.owner.location) * 20, () => {
            if (this.actor.isValid() && this.options.stopEvent.length > 0)
                this.actor.triggerEvent(this.options.stopEvent);
            this.options.onFailure();
        }, this.actor.id + this.id);
    }
    onComponentRemoved() {
        mc.system.afterEvents.scriptEventReceive.unsubscribe(this.eventBinding);
        utils.abortEvents(this.actor.id + this.id);
    }
    onEventRecieved(event) {
        var _a, _b;
        if (event.id === utils.addName("reach_owner") && ((_a = event.sourceEntity) === null || _a === void 0 ? void 0 : _a.id) === ((_b = this.actor) === null || _b === void 0 ? void 0 : _b.id)) {
            if (this.options.stopEvent.length > 0)
                this.actor.triggerEvent(this.options.stopEvent);
            utils.abortEvents(this.actor.id + this.id);
            this.options.onSuccess();
        }
    }
}
