import { Vector3 } from 'three';
export const radians = angle => angle * (Math.PI / 180);
export const uID = () => `_${Math.random().toString(36).substring(2, 9)}`;
export const clamp = (val, min = 0, max = 1) => Math.max(min, Math.min(max, val));
export const pick = (arr, exception = null) => {
    let picked = null;
    while (!picked) {
        const pickedElement = arr[Math.floor(Math.random() * arr.length)];
        if (pickedElement !== exception) {
            picked = pickedElement;
        }
    }
    return picked;
};
export const getPhiTheta = (lat, lng) => {
    const phi = radians(90 - lat);
    const theta = radians(lng + 180);
    return { phi, theta };
};
export const gpstoVec3 = (lat, lng, radius = 1) => {
    const { phi, theta } = getPhiTheta(lat, lng);
    const x = radius * Math.sin(phi) * Math.cos(theta) * -1;
    const y = radius * Math.cos(phi);
    const z = radius * Math.sin(phi) * Math.sin(theta);
    return new Vector3(x, y, z);
};
export const getRandom = (arr, n) => {
    var result = new Array(n), len = arr.length, taken = new Array(len);
    if (n > len) {
        throw new RangeError('getRandom: more elements taken than available');
    }
    while (n--) {
        var x = Math.floor(Math.random() * len);
        result[n] = arr[x in taken ? taken[x] : x];
        taken[x] = --len in taken ? taken[len] : len;
    }
    return result;
};
