"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const inheritsLoose = require("../../../../@babel_runtime@7.19.0/node_modules/@babel/runtime/helpers/esm/inheritsLoose.js");
const React = require("react");
const PropTypes = require("prop-types");
const Transition = require("./Transition.js");
const TransitionGroupContext = require("./TransitionGroupContext.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
const PropTypes__default = /* @__PURE__ */ _interopDefaultLegacy(PropTypes);
var _leaveRenders, _enterRenders;
function areChildrenDifferent(oldChildren, newChildren) {
  if (oldChildren === newChildren)
    return false;
  if (React__default.default.isValidElement(oldChildren) && React__default.default.isValidElement(newChildren) && oldChildren.key != null && oldChildren.key === newChildren.key) {
    return false;
  }
  return true;
}
var modes = {
  out: "out-in",
  in: "in-out"
};
var callHook = function callHook2(element, name, cb) {
  return function() {
    var _element$props;
    element.props[name] && (_element$props = element.props)[name].apply(_element$props, arguments);
    cb();
  };
};
var leaveRenders = (_leaveRenders = {}, _leaveRenders[modes.out] = function(_ref) {
  var current = _ref.current, changeState = _ref.changeState;
  return React__default.default.cloneElement(current, {
    in: false,
    onExited: callHook(current, "onExited", function() {
      changeState(Transition.ENTERING, null);
    })
  });
}, _leaveRenders[modes.in] = function(_ref2) {
  var current = _ref2.current, changeState = _ref2.changeState, children = _ref2.children;
  return [current, React__default.default.cloneElement(children, {
    in: true,
    onEntered: callHook(children, "onEntered", function() {
      changeState(Transition.ENTERING);
    })
  })];
}, _leaveRenders);
var enterRenders = (_enterRenders = {}, _enterRenders[modes.out] = function(_ref3) {
  var children = _ref3.children, changeState = _ref3.changeState;
  return React__default.default.cloneElement(children, {
    in: true,
    onEntered: callHook(children, "onEntered", function() {
      changeState(Transition.ENTERED, React__default.default.cloneElement(children, {
        in: true
      }));
    })
  });
}, _enterRenders[modes.in] = function(_ref4) {
  var current = _ref4.current, children = _ref4.children, changeState = _ref4.changeState;
  return [React__default.default.cloneElement(current, {
    in: false,
    onExited: callHook(current, "onExited", function() {
      changeState(Transition.ENTERED, React__default.default.cloneElement(children, {
        in: true
      }));
    })
  }), React__default.default.cloneElement(children, {
    in: true
  })];
}, _enterRenders);
var SwitchTransition = /* @__PURE__ */ function(_React$Component) {
  inheritsLoose(SwitchTransition2, _React$Component);
  function SwitchTransition2() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.state = {
      status: Transition.ENTERED,
      current: null
    };
    _this.appeared = false;
    _this.changeState = function(status, current) {
      if (current === void 0) {
        current = _this.state.current;
      }
      _this.setState({
        status,
        current
      });
    };
    return _this;
  }
  var _proto = SwitchTransition2.prototype;
  _proto.componentDidMount = function componentDidMount() {
    this.appeared = true;
  };
  SwitchTransition2.getDerivedStateFromProps = function getDerivedStateFromProps(props, state) {
    if (props.children == null) {
      return {
        current: null
      };
    }
    if (state.status === Transition.ENTERING && props.mode === modes.in) {
      return {
        status: Transition.ENTERING
      };
    }
    if (state.current && areChildrenDifferent(state.current, props.children)) {
      return {
        status: Transition.EXITING
      };
    }
    return {
      current: React__default.default.cloneElement(props.children, {
        in: true
      })
    };
  };
  _proto.render = function render() {
    var _this$props = this.props, children = _this$props.children, mode = _this$props.mode, _this$state = this.state, status = _this$state.status, current = _this$state.current;
    var data = {
      children,
      current,
      changeState: this.changeState,
      status
    };
    var component;
    switch (status) {
      case Transition.ENTERING:
        component = enterRenders[mode](data);
        break;
      case Transition.EXITING:
        component = leaveRenders[mode](data);
        break;
      case Transition.ENTERED:
        component = current;
    }
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(TransitionGroupContext.Provider, {
      value: {
        isMounting: !this.appeared
      },
      children: component
    });
  };
  return SwitchTransition2;
}(React__default.default.Component);
SwitchTransition.propTypes = process.env.NODE_ENV !== "production" ? {
  mode: PropTypes__default.default.oneOf([modes.in, modes.out]),
  children: PropTypes__default.default.oneOfType([PropTypes__default.default.element.isRequired])
} : {};
SwitchTransition.defaultProps = {
  mode: modes.out
};
const SwitchTransition$1 = SwitchTransition;
exports.default = SwitchTransition$1;
exports.modes = modes;
//# sourceMappingURL=SwitchTransition.js.map
