import { useContext as f, useRef as l, useInsertionEffect as h, useEffect as j } from "react";
import { optimizedAppearDataAttribute as I } from "../../animation/optimized-appear/data-id.es.js";
import { LazyContext as A } from "../../context/LazyContext.es.js";
import { MotionConfigContext as S } from "../../context/MotionConfigContext.es.js";
import { MotionContext as L } from "../../context/MotionContext/index.es.js";
import { PresenceContext as R } from "../../context/PresenceContext.es.js";
import { SwitchLayoutGroupContext as b } from "../../context/SwitchLayoutGroupContext.es.js";
import { isRefObject as k } from "../../utils/is-ref-object.es.js";
import { useIsomorphicLayoutEffect as H } from "../../utils/use-isomorphic-effect.es.js";
function J(o, s, n, i, u) {
  var y, M;
  const { visualElement: a } = f(L), p = f(A), e = f(R), m = f(S).reducedMotion, r = l(null);
  i = i || p.renderer, !r.current && i && (r.current = i(o, {
    visualState: s,
    parent: a,
    props: n,
    presenceContext: e,
    blockInitialAnimation: e ? e.initial === !1 : !1,
    reducedMotionConfig: m
  }));
  const t = r.current, x = f(b);
  t && !t.projection && u && (t.type === "html" || t.type === "svg") && z(r.current, n, u, x);
  const C = l(!1);
  h(() => {
    t && C.current && t.update(n, e);
  });
  const c = n[I], d = l(!!c && !((y = window.MotionHandoffIsComplete) != null && y.call(window, c)) && ((M = window.MotionHasOptimisedAnimation) == null ? void 0 : M.call(window, c)));
  return H(() => {
    t && (C.current = !0, window.MotionIsMounted = !0, t.updateFeatures(), t.scheduleRenderMicrotask(), d.current && t.animationState && t.animationState.animateChanges());
  }), j(() => {
    t && (!d.current && t.animationState && t.animationState.animateChanges(), d.current && (queueMicrotask(() => {
      var w;
      (w = window.MotionHandoffMarkAsComplete) == null || w.call(window, c);
    }), d.current = !1), t.enteringChildren = void 0);
  }), t;
}
function z(o, s, n, i) {
  const { layoutId: u, layout: a, drag: p, dragConstraints: e, layoutScroll: m, layoutRoot: r, layoutCrossfade: t } = s;
  o.projection = new n(o.latestValues, s["data-framer-portal-id"] ? void 0 : g(o.parent)), o.projection.setOptions({
    layoutId: u,
    layout: a,
    alwaysMeasureLayout: !!p || e && k(e),
    visualElement: o,
    /**
     * TODO: Update options in an effect. This could be tricky as it'll be too late
     * to update by the time layout animations run.
     * We also need to fix this safeToRemove by linking it up to the one returned by usePresence,
     * ensuring it gets called if there's no potential layout animations.
     *
     */
    animationType: typeof a == "string" ? a : "both",
    initialPromotionConfig: i,
    crossfade: t,
    layoutScroll: m,
    layoutRoot: r
  });
}
function g(o) {
  if (o)
    return o.options.allowProjection !== !1 ? o.projection : g(o.parent);
}
export {
  J as useVisualElement
};
//# sourceMappingURL=use-visual-element.es.js.map
