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

/**
 * @typedef {Object} Vec3Obj
 * @property {Number} x - x value.
 * @property {Number} y - y value.
 * @property {Number} z - z value.
 */

/**
 * Checks if two vectors are equal.
 *
 * @param {Vec3Obj} v1 - Vector 1.
 * @param {Vec3Obj} v2 - Vector 2.
 * 
 * @returns {Boolean} True if vectors match, false otherwise.
*/
export function isSameVector(v1, v2) {
    if (!v1 || !v2) return v1 === v2;
    return v1.x === v2.x && v1.y === v2.y && v1?.z === v2?.z;
}

/**
 * Class for performing Vector3 operations.
 *
 * @class
 */
export class Vec3 {

    /**
     * Adds two vectors.
     *
     * @param {Vec3Obj} v1 - Vector 1.
     * @param {Vec3Obj} v2 - Vector 2.
     * 
     * @returns {Vec3Obj} Sum of Vector 1 and Vector 2.
    */
    static add(v1, v2) {
        return { x: v1.x + v2.x, y: v1.y + v2.y, z: v1.z + v2.z };
    }

    /**
     * Subtracts two vectors.
     *
     * @param {Vec3Obj} v1 - Vector 1.
     * @param {Vec3Obj} v2 - Vector 2.
     * 
     * @returns {Vec3Obj} Difference between Vector 1 and Vector 2.
    */
    static subtract(v1, v2) {
        return { x: v1.x - v2.x, y: v1.y - v2.y, z: v1.z - v2.z };
    }

    /**
     * Multiplies a vector by a factor.
     *
     * @param {Vec3Obj} v - Vector.
     * @param {Number} f - Factor.
     * 
     * @returns {Vec3Obj} Multiplication of Vector by Factor.
    */
    static multiply(v, f) {
        return { x: v.x * f, y: v.y * f, z: v.z * f };
    }


    /**
     * Calculates the distance between two vectors.
     *
     * @param {Vec3Obj} v1 - Vector 1.
     * @param {Vec3Obj} v2 - Vector 2.
     *
     * @returns {Number} Distance between the two vectors.
    */
    static distance(v1, v2) {
        return Math.sqrt((v2.x - v1.x) ** 2 + (v2.y - v1.y) ** 2 + (v2.z - v1.z) ** 2);
    }

    /**
     * Calculates the magnitude of a vector.
     *
     * @param {Vec3Obj} v - Vector.
     *
     * @returns {Number} Magnitude of the vector.
    */
    static magnitude(v) {
        return Math.sqrt(v.x ** 2 + v.y ** 2 + v.z ** 2);
    }
}