const Utils = {
    lineEq: (y2, y1, x2, x1, currentVal) => {
        var m = (y2 - y1) / (x2 - x1), b = y1 - m * x1;
        return m * currentVal + b;
    },

    lerp: (v0, v1, amt, maxMove = 0, minDiff = 0.0001) => {
        let diff = v1 - v0;
        if (maxMove > 0) {
            diff = Math.min(diff, maxMove);
            diff = Math.max(diff, -maxMove);
        }
        if (Math.abs(diff) < minDiff) {
            return v1;
        }
        return v0 + diff * amt;
    },

    debounce (func, wait, immediate) {
        var timeout;
        wait = wait ? wait : 1000 / 60;
    
        return function () {
            var context = this, args = arguments;
            var later = function () {
                timeout = null;
                if (!immediate) func.apply(context, args);
            };
            var callNow = immediate && !timeout;
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
            if (callNow) func.apply(context, args);
        };
    },
    
    loadImage (src) {
        return new Promise((resolve, reject) => {
            let img = new Image();
            img.onload  = () => { resolve(img) };
            img.onerror = () => { resolve(img) };
            img.src = src;
        });
    },

    randomInt (min, max) { return Math.round(Math.random() * (max - min) + min); },
    randomFloat (min, max) { return Math.random() * (max - min) + min; },
    randomModifier (magnitude, base = 0) { return base + (Math.random() * magnitude - (magnitude / 2)); },
    randomSign () { return Math.random(1) > 0.5 ? 1 : -1; },
    randomElement (array) { return array[Math.floor(Math.random() * array.length)]; },
    radialRandom () { return ((Math.random() * 2 - 1) + (Math.random() * 2 - 1) + (Math.random() * 2 - 1)) / 2; },

    S4 () { return (((1+Math.random())*0x10000)|0).toString(16).substring(1); },
    guid () {
        return (this.S4() + this.S4() + "-" + this.S4() + "-4" + this.S4().substr(0,3) + "-" + this.S4() + "-" + this.S4() + this.S4() + this.S4()).toLowerCase();
    }
};

export { Utils as default };