import { useContext as u, useRef as l, useInsertionEffect as k, useEffect as S } from "react";
import { LazyContext as L } from "../../context/LazyContext.es.js";
import { MotionConfigContext as O } from "../../context/MotionConfigContext.es.js";
import { MotionContext as R } from "../../context/MotionContext/index.es.js";
import { PresenceContext as b } from "../../context/PresenceContext.es.js";
import { SwitchLayoutGroupContext as H } from "../../context/SwitchLayoutGroupContext.es.js";
import { isRefObject as z } from "../../utils/is-ref-object.es.js";
import { useIsomorphicLayoutEffect as P } from "../../utils/use-isomorphic-effect.es.js";
import { optimizedAppearDataAttribute as B } from "../../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/animation/optimized-appear/data-id.es.js";
function W(o, f, n, e, s, a) {
  var h, A;
  const { visualElement: y } = u(R), c = u(L), i = u(b), d = u(O), C = d.reducedMotion, M = d.skipAnimations, r = l(null), w = l(!1);
  e = e || c.renderer, !r.current && e && (r.current = e(o, {
    visualState: f,
    parent: y,
    props: n,
    presenceContext: i,
    blockInitialAnimation: i ? i.initial === !1 : !1,
    reducedMotionConfig: C,
    skipAnimations: M,
    isSVG: a
  }), w.current && r.current && (r.current.manuallyAnimateOnMount = !0));
  const t = r.current, I = u(H);
  t && !t.projection && s && (t.type === "html" || t.type === "svg") && G(r.current, n, s, I);
  const g = l(!1);
  k(() => {
    t && g.current && t.update(n, i);
  });
  const m = n[B], p = l(!!m && typeof window < "u" && !((h = window.MotionHandoffIsComplete) != null && h.call(window, m)) && ((A = window.MotionHasOptimisedAnimation) == null ? void 0 : A.call(window, m)));
  return P(() => {
    w.current = !0, t && (g.current = !0, window.MotionIsMounted = !0, t.updateFeatures(), t.scheduleRenderMicrotask(), p.current && t.animationState && t.animationState.animateChanges());
  }), S(() => {
    t && (!p.current && t.animationState && t.animationState.animateChanges(), p.current && (queueMicrotask(() => {
      var x;
      (x = window.MotionHandoffMarkAsComplete) == null || x.call(window, m);
    }), p.current = !1), t.enteringChildren = void 0);
  }), t;
}
function G(o, f, n, e) {
  const { layoutId: s, layout: a, drag: y, dragConstraints: c, layoutScroll: i, layoutRoot: d, layoutAnchor: C, layoutCrossfade: M } = f;
  o.projection = new n(o.latestValues, f["data-framer-portal-id"] ? void 0 : j(o.parent)), o.projection.setOptions({
    layoutId: s,
    layout: a,
    alwaysMeasureLayout: !!y || c && z(c),
    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: e,
    crossfade: M,
    layoutScroll: i,
    layoutRoot: d,
    layoutAnchor: C
  });
}
function j(o) {
  if (o)
    return o.options.allowProjection !== !1 ? o.projection : j(o.parent);
}
export {
  W as useVisualElement
};
//# sourceMappingURL=use-visual-element.es.js.map
