import { Entity, EntityTameMountComponent } from "@minecraft/server"
import { Piki } from "../../utils/typingConstants"

export class EntityHelper {
    /**
     * 
     * @param {string} effectType The effect type id
     * @param {number} duration The duration in seconds
     * @param {number} amplifier The effect level
     * @param {boolean} showParticles Whether to show the particles or not
     * @returns 
     */
    static createEntityEffect(effectType, duration, amplifier, showParticles) {
        return {
            effectType: effectType,
            duration: duration * 20,
            amplifier: amplifier,
            showParticles: showParticles
        }
    }

    /**
     * @deprecated
     * @param {import("@minecraft/server").Vector3} lastLocation 
     * @param {import("@minecraft/server").Vector3} currentLocation 
     * @param {number} tolerance
     */
    static isEntityMoving(lastLocation, currentLocation, tolerance = 0.01) {
        return !lastLocation || Math.abs(lastLocation.x - currentLocation.x) > tolerance ||
                Math.abs(lastLocation.y - currentLocation.y) > tolerance ||
                Math.abs(lastLocation.z - currentLocation.z) > tolerance
    }

    /**
     * @param {Entity} entity
     */
    static entityHasVelocity(entity, tolerance = 0.01) {
        const { x, y, z } = entity.getVelocity();
        return [x, y, z].some(a => Math.abs(a) > tolerance);
    }

    /**
     * Resets the property value of an entity that starts with `piki_beyondsurvival:_`
     * @param {Entity} entity 
     */
    static resetProperties(entity) {
        for (const property of entity.getDynamicPropertyIds()) {
            if (!property.startsWith(Piki.namespace + "_")) continue
            entity.setDynamicProperty(property, undefined)
        }
    }
}