"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var just_curves_1 = require("./vendor/just-curves");
var CENT = 1.0;
var OBJECT = 'object';
var NUMBER = 'number';
function percentOfTime(t0, t1, tnow) {
    var span = t1 - t0;
    if (span === 0) {
        return CENT;
    } // No divide-by-zero
    var remaining = t1 - tnow;
    return CENT - remaining / span;
}
function valueAtPercent(v0, v1, pc) {
    var span = v1 - v0;
    var gain = span * pc;
    return v0 + gain;
}
function interpolateValue(v0, v1, t0, t1, tnow, curve) {
    var pc = percentOfTime(t0, t1, tnow);
    if (pc > CENT) {
        pc = CENT;
    }
    if (curve) {
        pc = curve(pc);
    }
    return valueAtPercent(v0, v1, pc);
}
function interpolate(now, curve, started, ends, origin, destination) {
    // If curve is a string, transform into a function using justCurves
    var curveFunc = typeof curve === 'string' ? just_curves_1.default[curve] : curve;
    if (typeof curveFunc !== 'function') {
        return origin;
    }
    if (Array.isArray(origin) && Array.isArray(destination)) {
        var arrayOutput = [];
        for (var i = 0; i < origin.length; i++) {
            arrayOutput[i] = interpolate(now, curveFunc, started, ends, origin[i], destination[i]);
        }
        return arrayOutput;
    }
    if (origin && typeof origin === OBJECT && destination && typeof destination === OBJECT) {
        var objectOutput = {};
        for (var key in origin) {
            objectOutput[key] = interpolate(now, curveFunc, started, ends, origin[key], destination[key]);
        }
        return objectOutput;
    }
    if (typeof origin === NUMBER && typeof destination === NUMBER) {
        return interpolateValue(origin, destination, started, ends, now, curveFunc);
    }
    return origin;
}
exports.default = {
    interpolate: interpolate,
};
//# sourceMappingURL=Interpolate.js.map