"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const usePresence = require("./dist-es-components-AnimatePresence-use-presence.cjs");
const LayoutGroupContext = require("./dist-es-context-LayoutGroupContext.cjs");
const SwitchLayoutGroupContext = require("./dist-es-context-SwitchLayoutGroupContext.cjs");
const state = require("./dist-es-projection-node-state.cjs");
const scaleBorderRadius = require("./dist-es-projection-styles-scale-border-radius.cjs");
const scaleBoxShadow = require("./dist-es-projection-styles-scale-box-shadow.cjs");
const scaleCorrection = require("./dist-es-projection-styles-scale-correction.cjs");
const microtask = require("./dist-es-frameloop-microtask.cjs");
const frame = require("./dist-es-frameloop-frame.cjs");
class MeasureLayoutWithContext extends React.Component {
  /**
   * 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, layoutGroup, switchLayoutGroup, layoutId } = this.props;
    const { projection } = visualElement;
    scaleCorrection.addScaleCorrector(defaultScaleCorrectors);
    if (projection) {
      if (layoutGroup.group)
        layoutGroup.group.add(projection);
      if (switchLayoutGroup && switchLayoutGroup.register && layoutId) {
        switchLayoutGroup.register(projection);
      }
      projection.root.didUpdate();
      projection.addEventListener("animationComplete", () => {
        this.safeToRemove();
      });
      projection.setOptions({
        ...projection.options,
        onExitComplete: () => this.safeToRemove()
      });
    }
    state.globalProjectionState.hasEverUpdated = true;
  }
  getSnapshotBeforeUpdate(prevProps) {
    const { layoutDependency, visualElement, drag, isPresent } = this.props;
    const projection = visualElement.projection;
    if (!projection)
      return null;
    projection.isPresent = isPresent;
    if (drag || prevProps.layoutDependency !== layoutDependency || layoutDependency === void 0) {
      projection.willUpdate();
    } else {
      this.safeToRemove();
    }
    if (prevProps.isPresent !== isPresent) {
      if (isPresent) {
        projection.promote();
      } else if (!projection.relegate()) {
        frame.frame.postRender(() => {
          const stack = projection.getStack();
          if (!stack || !stack.members.length) {
            this.safeToRemove();
          }
        });
      }
    }
    return null;
  }
  componentDidUpdate() {
    const { projection } = this.props.visualElement;
    if (projection) {
      projection.root.didUpdate();
      microtask.microtask.postRender(() => {
        if (!projection.currentAnimation && projection.isLead()) {
          this.safeToRemove();
        }
      });
    }
  }
  componentWillUnmount() {
    const { visualElement, layoutGroup, switchLayoutGroup: promoteContext } = this.props;
    const { projection } = visualElement;
    if (projection) {
      projection.scheduleCheckAfterUnmount();
      if (layoutGroup && layoutGroup.group)
        layoutGroup.group.remove(projection);
      if (promoteContext && promoteContext.deregister)
        promoteContext.deregister(projection);
    }
  }
  safeToRemove() {
    const { safeToRemove } = this.props;
    safeToRemove && safeToRemove();
  }
  render() {
    return null;
  }
}
function MeasureLayout(props) {
  const [isPresent, safeToRemove] = usePresence.usePresence();
  const layoutGroup = React.useContext(LayoutGroupContext.LayoutGroupContext);
  return jsxRuntime.jsx(MeasureLayoutWithContext, { ...props, layoutGroup, switchLayoutGroup: React.useContext(SwitchLayoutGroupContext.SwitchLayoutGroupContext), isPresent, safeToRemove });
}
const defaultScaleCorrectors = {
  borderRadius: {
    ...scaleBorderRadius.correctBorderRadius,
    applyTo: [
      "borderTopLeftRadius",
      "borderTopRightRadius",
      "borderBottomLeftRadius",
      "borderBottomRightRadius"
    ]
  },
  borderTopLeftRadius: scaleBorderRadius.correctBorderRadius,
  borderTopRightRadius: scaleBorderRadius.correctBorderRadius,
  borderBottomLeftRadius: scaleBorderRadius.correctBorderRadius,
  borderBottomRightRadius: scaleBorderRadius.correctBorderRadius,
  boxShadow: scaleBoxShadow.correctBoxShadow
};
exports.MeasureLayout = MeasureLayout;
//# sourceMappingURL=dist-es-motion-features-layout-MeasureLayout.cjs.map
