'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var utils = require('@motionone/utils'); var stopAnimation = require('./stop-animation.cjs.js'); const createAnimation = (factory) => factory(); const withControls = (animationFactory, options, duration = utils.defaults.duration) => { return new Proxy({ animations: animationFactory.map(createAnimation).filter(Boolean), duration, options, }, controls); }; /** * TODO: * Currently this returns the first animation, ideally it would return * the first active animation. */ const getActiveAnimation = (state) => state.animations[0]; const controls = { get: (target, key) => { const activeAnimation = getActiveAnimation(target); switch (key) { case "duration": return target.duration; case "currentTime": return utils.time.s((activeAnimation === null || activeAnimation === void 0 ? void 0 : activeAnimation[key]) || 0); case "playbackRate": case "playState": return activeAnimation === null || activeAnimation === void 0 ? void 0 : activeAnimation[key]; case "finished": if (!target.finished) { target.finished = Promise.all(target.animations.map(selectFinished)).catch(utils.noop); } return target.finished; case "stop": return () => { target.animations.forEach((animation) => stopAnimation.stopAnimation(animation)); }; case "forEachNative": /** * This is for internal use only, fire a callback for each * underlying animation. */ return (callback) => { target.animations.forEach((animation) => callback(animation, target)); }; default: return typeof (activeAnimation === null || activeAnimation === void 0 ? void 0 : activeAnimation[key]) === "undefined" ? undefined : () => target.animations.forEach((animation) => animation[key]()); } }, set: (target, key, value) => { switch (key) { case "currentTime": value = utils.time.ms(value); case "currentTime": case "playbackRate": for (let i = 0; i < target.animations.length; i++) { target.animations[i][key] = value; } return true; } return false; }, }; const selectFinished = (animation) => animation.finished; exports.controls = controls; exports.withControls = withControls;