import * as mc from "@minecraft/server";
import { GlobalNamespace, getInventoryContainer, waitForTicks } from "../../utils/index";
import { Task } from "../../npc/tasks/task";
export class DespawnTask extends Task {
    async performTask() {
        this.npc.Entity.addEffect("resistance", 100, { amplifier: 255, showParticles: false });
        const playerInventory = getInventoryContainer(this.options.player);
        if (playerInventory.emptySlotsCount <= 0) {
            this.npc.broadcastEvent("despawn_failure");
            return { outcome: "failed" };
        }
        await waitForTicks(1);
        this.npc.Entity.playAnimation("despawn", { stopExpression: "false" });
        this.npc.broadcastEvent("despawn_success");
        await waitForTicks(2.9 * 20, this.id);
        this.shouldAbort();
        if (playerInventory.emptySlotsCount <= 0) {
            this.npc.broadcastEvent("despawn_failure");
            this.npc.Entity.playAnimation("root");
            return { outcome: "failed" };
        }
        this.npc.Entity.dimension.spawnParticle(GlobalNamespace.Namespace + "craftee_despawn", this.npc.Entity.location.add({ x: 0, y: 0.75, z: 0 }));
        mc.world.playSound("random.pop", this.npc.Entity.location);
        if (this.npc.Hat)
            this.npc.Entity.dimension.spawnItem(new mc.ItemStack(this.npc.Hat, 1), this.npc.Entity.location);
        this.npc.Entity.runCommand(`scriptevent ${GlobalNamespace.Namespace}on_destruct`);
        const spawnItem = new mc.ItemStack(`${this.npc.Entity.typeId}_spawn_egg`, 1);
        spawnItem.keepOnDeath = true;
        playerInventory.addItem(spawnItem);
        return { outcome: "succeeded" };
    }
    async cleanupTask() {
        this.npc.Entity.removeEffect("resistance");
    }
}
