"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const tslib_es6 = require("../../../../../../../../tslib@2.6.2/node_modules/tslib/tslib.es6.js");
const index = require("../../../../../../../../framesync@6.0.1/node_modules/framesync/dist/es/index.js");
const React = require("react");
const usePresence = require("../../../components/AnimatePresence/use-presence.js");
const LayoutGroupContext = require("../../../context/LayoutGroupContext.js");
const SwitchLayoutGroupContext = require("../../../context/SwitchLayoutGroupContext.js");
const state = require("../../../projection/node/state.js");
const scaleBorderRadius = require("../../../projection/styles/scale-border-radius.js");
const scaleBoxShadow = require("../../../projection/styles/scale-box-shadow.js");
const scaleCorrection = require("../../../projection/styles/scale-correction.js");
var MeasureLayoutWithContext = (
  /** @class */
  function(_super) {
    tslib_es6.__extends(MeasureLayoutWithContext2, _super);
    function MeasureLayoutWithContext2() {
      return _super !== null && _super.apply(this, arguments) || this;
    }
    MeasureLayoutWithContext2.prototype.componentDidMount = function() {
      var _this = this;
      var _a = this.props, visualElement = _a.visualElement, layoutGroup = _a.layoutGroup, switchLayoutGroup = _a.switchLayoutGroup, layoutId = _a.layoutId;
      var projection = visualElement.projection;
      scaleCorrection.addScaleCorrector(defaultScaleCorrectors);
      if (projection) {
        if (layoutGroup === null || layoutGroup === void 0 ? void 0 : layoutGroup.group)
          layoutGroup.group.add(projection);
        if ((switchLayoutGroup === null || switchLayoutGroup === void 0 ? void 0 : switchLayoutGroup.register) && layoutId) {
          switchLayoutGroup.register(projection);
        }
        projection.root.didUpdate();
        projection.addEventListener("animationComplete", function() {
          _this.safeToRemove();
        });
        projection.setOptions(tslib_es6.__assign(tslib_es6.__assign({}, projection.options), { onExitComplete: function() {
          return _this.safeToRemove();
        } }));
      }
      state.globalProjectionState.hasEverUpdated = true;
    };
    MeasureLayoutWithContext2.prototype.getSnapshotBeforeUpdate = function(prevProps) {
      var _this = this;
      var _a = this.props, layoutDependency = _a.layoutDependency, visualElement = _a.visualElement, drag = _a.drag, isPresent = _a.isPresent;
      var 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()) {
          index.default.postRender(function() {
            var _a2;
            if (!((_a2 = projection.getStack()) === null || _a2 === void 0 ? void 0 : _a2.members.length)) {
              _this.safeToRemove();
            }
          });
        }
      }
      return null;
    };
    MeasureLayoutWithContext2.prototype.componentDidUpdate = function() {
      var projection = this.props.visualElement.projection;
      if (projection) {
        projection.root.didUpdate();
        if (!projection.currentAnimation && projection.isLead()) {
          this.safeToRemove();
        }
      }
    };
    MeasureLayoutWithContext2.prototype.componentWillUnmount = function() {
      var _a = this.props, visualElement = _a.visualElement, layoutGroup = _a.layoutGroup, promoteContext = _a.switchLayoutGroup;
      var projection = visualElement.projection;
      if (projection) {
        projection.scheduleCheckAfterUnmount();
        if (layoutGroup === null || layoutGroup === void 0 ? void 0 : layoutGroup.group)
          layoutGroup.group.remove(projection);
        if (promoteContext === null || promoteContext === void 0 ? void 0 : promoteContext.deregister)
          promoteContext.deregister(projection);
      }
    };
    MeasureLayoutWithContext2.prototype.safeToRemove = function() {
      var safeToRemove = this.props.safeToRemove;
      safeToRemove === null || safeToRemove === void 0 ? void 0 : safeToRemove();
    };
    MeasureLayoutWithContext2.prototype.render = function() {
      return null;
    };
    return MeasureLayoutWithContext2;
  }(React.Component)
);
function MeasureLayout(props) {
  var _a = tslib_es6.__read(usePresence.usePresence(), 2), isPresent = _a[0], safeToRemove = _a[1];
  var layoutGroup = React.useContext(LayoutGroupContext.LayoutGroupContext);
  return React.createElement(MeasureLayoutWithContext, tslib_es6.__assign({}, props, { layoutGroup, switchLayoutGroup: React.useContext(SwitchLayoutGroupContext.SwitchLayoutGroupContext), isPresent, safeToRemove }));
}
var defaultScaleCorrectors = {
  borderRadius: tslib_es6.__assign(tslib_es6.__assign({}, 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=MeasureLayout.js.map
