import * as mc from "@minecraft/server";
import { centrifuge, syringeReady } from "../constants";
import { setSyringeDNAInfo, takeOneItem, } from "../utilities";
import { NAMESPACE } from "../anvilConstants";
export function startCentrifuge(player, itemStack) {
    const entity = player.getEntitiesFromViewDirection({ maxDistance: 5 })[0];
    if (entity && entity.entity.typeId === centrifuge && (entity.entity.getProperty(`${NAMESPACE}:state`) === "idle" || entity.entity.getProperty(`${NAMESPACE}:state`) === "drop")) {
        entity.entity.setDynamicProperty(`${NAMESPACE}:dinosaur`, itemStack.getDynamicProperty(`${NAMESPACE}:dinosaur`));
        entity.entity.setDynamicProperty(`${NAMESPACE}:player`, itemStack.getDynamicProperty(`${NAMESPACE}:player`));
        entity.entity.triggerEvent("start");
        takeOneItem(player, itemStack);
    }
    else {
        player.runCommand(`titleraw @s actionbar ${JSON.stringify({ rawtext: [{ translate: `${NAMESPACE}.place_in_the_centrifuge` }] })}`);
    }
}
export function centrifugeScriptEvent(eventData) {
    const id = eventData.id;
    const sourceEntity = eventData.sourceEntity;
    if (id === `${NAMESPACE}:centrifuge_stop` && sourceEntity && sourceEntity.typeId === centrifuge) {
        const dinoName = sourceEntity.getDynamicProperty(`${NAMESPACE}:dinosaur`);
        const playerName = sourceEntity.getDynamicProperty(`${NAMESPACE}:player`);
        const itemStack = new mc.ItemStack(syringeReady, 1);
        setSyringeDNAInfo(itemStack, true, dinoName, playerName);
        sourceEntity.dimension.spawnItem(itemStack, sourceEntity.location);
        sourceEntity.triggerEvent("idle");
    }
}
