'use strict';

var interpolate = require('../../utils/interpolate.js');
var _default = require('../keyframes/offsets/default.js');
var time = require('../keyframes/offsets/time.js');
var isEasingArray = require('../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/easing/utils/is-easing-array.js');
var map = require('../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/easing/utils/map.js');
var ease = require('../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/easing/ease.js');

function defaultEasing(values, easing) {
    return values.map(() => easing || ease.easeInOut).splice(0, values.length - 1);
}
function keyframes({ duration = 300, keyframes: keyframeValues, times, ease = "easeInOut", }) {
    /**
     * Easing functions can be externally defined as strings. Here we convert them
     * into actual functions.
     */
    const easingFunctions = isEasingArray.isEasingArray(ease)
        ? ease.map(map.easingDefinitionToFunction)
        : map.easingDefinitionToFunction(ease);
    /**
     * This is the Iterator-spec return value. We ensure it's mutable rather than using a generator
     * to reduce GC during animation.
     */
    const state = {
        done: false,
        value: keyframeValues[0],
    };
    /**
     * Create a times array based on the provided 0-1 offsets
     */
    const absoluteTimes = time.convertOffsetToTimes(
    // Only use the provided offsets if they're the correct length
    // TODO Maybe we should warn here if there's a length mismatch
    times && times.length === keyframeValues.length
        ? times
        : _default.defaultOffset(keyframeValues), duration);
    const mapTimeToKeyframe = interpolate.interpolate(absoluteTimes, keyframeValues, {
        ease: Array.isArray(easingFunctions)
            ? easingFunctions
            : defaultEasing(keyframeValues, easingFunctions),
    });
    return {
        calculatedDuration: duration,
        next: (t) => {
            state.value = mapTimeToKeyframe(t);
            state.done = t >= duration;
            return state;
        },
    };
}

exports.defaultEasing = defaultEasing;
exports.keyframes = keyframes;
