
export function randomRange(min, max) {
    return Math.random() * (max - min) + min;
}

/**
 * Get a random item from a weighted pool
 * @param {{weight: number, any}[]} pool 
 */
export function getItemFromWeightedPool(pool, rolls = 1) {
    // Calculate total weight
    const totalWeight = pool.reduce((acc, item) => acc + item.weight, 0);

    // Generate a random number between 0 and totalWeight
    const rnd = Math.random() * totalWeight;

    // Iterate through the pool and pick the item based on cumulative weight
    let cumulativeWeight = 0;
    const rolledItems = [];
    for (let i = 0; i < rolls; i++) {
        for (const item of pool) {
            cumulativeWeight += item.weight;
            if (rnd <= cumulativeWeight) {
                rolledItems.push(item)
            }
        }
    }

    return rolledItems;
}


export function clamp(min, max, value) {
    return Math.min(Math.max(value, min), max);
}


export function withinRange(min, max, value) {
    return value >= min && value <= max;
}

/**
 * 
 * @param {Number} a - The start value
 * @param {Number} b - The end value
 * @param {Number} ratio - The ratio between a and b
 * @returns 
 */
export function lerp(a, b, ratio) {
    return a + ratio * (b - a);
}

export function degToRad(degrees) {
    return degrees * Math.PI / 180;
}

export function radToDeg(radians) {
    return radians * 180 / Math.PI;
}


/**
 * Wraps a rotation value to the range -180 to 180.
 * @param {number} angle - The rotation value to wrap.
 * @returns {number} - The wrapped rotation value.
 */
export function wrapRotation(angle) {
    angle = ((angle + 180) % 360) - 180;
    return angle;
}