import { __spreadArray, __read } from "../../../../../../tslib@2.6.2/node_modules/tslib/tslib.es6.js";
import { invariant } from "../../../../../../hey-listen@1.0.8/node_modules/hey-listen/dist/hey-listen.es.js";
import { stopAnimation, animateVisualElement } from "../render/utils/animation.js";
import { setValues } from "../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(animateVisualElement(visualElement, definition, {
            transitionOverride
          }));
        });
        return Promise.all(animations_1);
      } else {
        return new Promise(function(resolve) {
          pendingAnimations.push({
            animation: [definition, transitionOverride],
            resolve
          });
        });
      }
    },
    set: function(definition) {
      invariant(hasMounted, "controls.set() should only be called after a component has mounted. Consider calling within a useEffect hook.");
      return subscribers.forEach(function(visualElement) {
        setValues(visualElement, definition);
      });
    },
    stop: function() {
      subscribers.forEach(function(visualElement) {
        stopAnimation(visualElement);
      });
    },
    mount: function() {
      hasMounted = true;
      pendingAnimations.forEach(function(_a) {
        var animation = _a.animation, resolve = _a.resolve;
        controls.start.apply(controls, __spreadArray([], __read(animation), false)).then(resolve);
      });
      return function() {
        hasMounted = false;
        controls.stop();
      };
    }
  };
  return controls;
}
export {
  animationControls
};
//# sourceMappingURL=animation-controls.js.map
