'use strict';

var resolveDynamicVariants = require('../../render/utils/resolve-dynamic-variants.js');
var visualElementTarget = require('./visual-element-target.js');
var visualElementVariant = require('./visual-element-variant.js');

function animateVisualElement(visualElement, definition, options = {}) {
    visualElement.notify("AnimationStart", definition);
    let animation;
    if (Array.isArray(definition)) {
        const animations = definition.map((variant) => visualElementVariant.animateVariant(visualElement, variant, options));
        animation = Promise.all(animations);
    }
    else if (typeof definition === "string") {
        animation = visualElementVariant.animateVariant(visualElement, definition, options);
    }
    else {
        const resolvedDefinition = typeof definition === "function"
            ? resolveDynamicVariants.resolveVariant(visualElement, definition, options.custom)
            : definition;
        animation = Promise.all(visualElementTarget.animateTarget(visualElement, resolvedDefinition, options));
    }
    return animation.then(() => {
        visualElement.notify("AnimationComplete", definition);
    });
}

exports.animateVisualElement = animateVisualElement;
