import { world, system, EntityComponentTypes } from "@minecraft/server";

//heals its owner
system.afterEvents.scriptEventReceive.subscribe((event) => {
    const pet = event.sourceEntity;
    if (event.id != "oct_eg:hydro_pet") return;

    const owner = pet.getComponent("minecraft:tameable").tamedToPlayer;

    switch (event.message) {
        //heals the owner
        case "heal_owner":
            owner?.addEffect("minecraft:regeneration", 4 * 20, { amplifier: 2, showParticles: false });
            pet.playAnimation("anim.pet.heal");
            pet.dimension.spawnParticle("oct_eg:hydro_pet.healing", pet.location);
            addCooldown(pet);
            break;

        //checks if it is still on cooldown
        case "cooldown_check":
            if (!isOnCooldown(pet))
                pet.triggerEvent("oct_eg:heal_owner.true")
            break;
        case "tp_to_owner":
            pet.teleport(owner.location);
            break;
        default:
            break;
    }
});


/**
 * @param {Entity} pet
 */
function addCooldown(pet) {
    const currentTime = system.currentTick;
    const cooldown = 10 * 20;
    pet.setDynamicProperty("oct_eg:cooldown", currentTime + cooldown);
}

/**
 * @param {Entity} pet
 */
function isOnCooldown(pet) {
    return pet.getDynamicProperty("oct_eg:cooldown") > system.currentTick;
}