import { jsx as m } from "react/jsx-runtime";
import { useContext as u, Component as l } from "react";
import { usePresence as c } from "../../../components/AnimatePresence/use-presence.es.js";
import { LayoutGroupContext as f } from "../../../context/LayoutGroupContext.es.js";
import { SwitchLayoutGroupContext as d } from "../../../context/SwitchLayoutGroupContext.es.js";
import { globalProjectionState as h } from "../../../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/projection/node/state.es.js";
import { frame as y } from "../../../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/frameloop/frame.es.js";
import { microtask as g } from "../../../../../../../../motion-dom@12.38.0/node_modules/motion-dom/dist/es/frameloop/microtask.es.js";
let i = !1;
class v extends l {
  /**
   * This only mounts projection nodes for components that
   * need measuring, we might want to do it for all components
   * in order to incorporate transforms
   */
  componentDidMount() {
    const { visualElement: t, layoutGroup: o, switchLayoutGroup: e, layoutId: s } = this.props, { projection: n } = t;
    n && (o.group && o.group.add(n), e && e.register && s && e.register(n), i && n.root.didUpdate(), n.addEventListener("animationComplete", () => {
      this.safeToRemove();
    }), n.setOptions({
      ...n.options,
      layoutDependency: this.props.layoutDependency,
      onExitComplete: () => this.safeToRemove()
    })), h.hasEverUpdated = !0;
  }
  getSnapshotBeforeUpdate(t) {
    const { layoutDependency: o, visualElement: e, drag: s, isPresent: n } = this.props, { projection: r } = e;
    return r && (r.isPresent = n, t.layoutDependency !== o && r.setOptions({
      ...r.options,
      layoutDependency: o
    }), i = !0, s || t.layoutDependency !== o || o === void 0 || t.isPresent !== n ? r.willUpdate() : this.safeToRemove(), t.isPresent !== n && (n ? r.promote() : r.relegate() || y.postRender(() => {
      const p = r.getStack();
      (!p || !p.members.length) && this.safeToRemove();
    }))), null;
  }
  componentDidUpdate() {
    const { visualElement: t, layoutAnchor: o } = this.props, { projection: e } = t;
    e && (e.options.layoutAnchor = o, e.root.didUpdate(), g.postRender(() => {
      !e.currentAnimation && e.isLead() && this.safeToRemove();
    }));
  }
  componentWillUnmount() {
    const { visualElement: t, layoutGroup: o, switchLayoutGroup: e } = this.props, { projection: s } = t;
    i = !0, s && (s.scheduleCheckAfterUnmount(), o && o.group && o.group.remove(s), e && e.deregister && e.deregister(s));
  }
  safeToRemove() {
    const { safeToRemove: t } = this.props;
    t && t();
  }
  render() {
    return null;
  }
}
function E(a) {
  const [t, o] = c(), e = u(f);
  return m(v, { ...a, layoutGroup: e, switchLayoutGroup: u(d), isPresent: t, safeToRemove: o });
}
export {
  E as MeasureLayout
};
//# sourceMappingURL=MeasureLayout.es.js.map
