"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const PresenceContext = require("../../context/PresenceContext.js");
const index = require("../../context/MotionContext/index.js");
const useIsomorphicEffect = require("../../utils/use-isomorphic-effect.js");
const LazyContext = require("../../context/LazyContext.js");
const useReducedMotion = require("../../utils/use-reduced-motion.js");
function useVisualElement(Component, visualState, props, createVisualElement) {
  var lazyContext = React.useContext(LazyContext.LazyContext);
  var parent = index.useVisualElementContext();
  var presenceContext = React.useContext(PresenceContext.PresenceContext);
  var shouldReduceMotion = useReducedMotion.useReducedMotionConfig();
  var visualElementRef = React.useRef(void 0);
  if (!createVisualElement)
    createVisualElement = lazyContext.renderer;
  if (!visualElementRef.current && createVisualElement) {
    visualElementRef.current = createVisualElement(Component, {
      visualState,
      parent,
      props,
      presenceId: presenceContext === null || presenceContext === void 0 ? void 0 : presenceContext.id,
      blockInitialAnimation: (presenceContext === null || presenceContext === void 0 ? void 0 : presenceContext.initial) === false,
      shouldReduceMotion
    });
  }
  var visualElement = visualElementRef.current;
  useIsomorphicEffect.useIsomorphicLayoutEffect(function() {
    visualElement === null || visualElement === void 0 ? void 0 : visualElement.syncRender();
  });
  React.useEffect(function() {
    var _a;
    (_a = visualElement === null || visualElement === void 0 ? void 0 : visualElement.animationState) === null || _a === void 0 ? void 0 : _a.animateChanges();
  });
  useIsomorphicEffect.useIsomorphicLayoutEffect(function() {
    return function() {
      return visualElement === null || visualElement === void 0 ? void 0 : visualElement.notifyUnmount();
    };
  }, []);
  return visualElement;
}
exports.useVisualElement = useVisualElement;
//# sourceMappingURL=use-visual-element.js.map
