"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const tslib_es6 = require("../../../../../../tslib@2.6.2/node_modules/tslib/tslib.es6.js");
const heyListen_es = require("../../../../../../hey-listen@1.0.8/node_modules/hey-listen/dist/hey-listen.es.js");
const animation = require("../render/utils/animation.js");
const setters = require("../render/utils/setters.js");
function animationControls() {
  var hasMounted = false;
  var pendingAnimations = [];
  var subscribers = /* @__PURE__ */ new Set();
  var controls = {
    subscribe: function(visualElement) {
      subscribers.add(visualElement);
      return function() {
        return void subscribers.delete(visualElement);
      };
    },
    start: function(definition, transitionOverride) {
      if (hasMounted) {
        var animations_1 = [];
        subscribers.forEach(function(visualElement) {
          animations_1.push(animation.animateVisualElement(visualElement, definition, {
            transitionOverride
          }));
        });
        return Promise.all(animations_1);
      } else {
        return new Promise(function(resolve) {
          pendingAnimations.push({
            animation: [definition, transitionOverride],
            resolve
          });
        });
      }
    },
    set: function(definition) {
      heyListen_es.invariant(hasMounted, "controls.set() should only be called after a component has mounted. Consider calling within a useEffect hook.");
      return subscribers.forEach(function(visualElement) {
        setters.setValues(visualElement, definition);
      });
    },
    stop: function() {
      subscribers.forEach(function(visualElement) {
        animation.stopAnimation(visualElement);
      });
    },
    mount: function() {
      hasMounted = true;
      pendingAnimations.forEach(function(_a) {
        var animation2 = _a.animation, resolve = _a.resolve;
        controls.start.apply(controls, tslib_es6.__spreadArray([], tslib_es6.__read(animation2), false)).then(resolve);
      });
      return function() {
        hasMounted = false;
        controls.stop();
      };
    }
  };
  return controls;
}
exports.animationControls = animationControls;
//# sourceMappingURL=animation-controls.js.map
