"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var React = _interopRequireWildcard(require("react"));

var _classnames2 = _interopRequireDefault(require("classnames"));

var _reactTransitionGroup = require("react-transition-group");

var _utils = require("../utils");

var _events = _interopRequireDefault(require("../utils/events"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var TransitionState;

(function (TransitionState) {
  TransitionState["UNMOUNTED"] = "unmounted";
  TransitionState["ENTER"] = "enter";
  TransitionState["ENTERING"] = "entering";
  TransitionState["ENTERED"] = "entered";
  TransitionState["EXIT"] = "exit";
  TransitionState["EXITING"] = "exiting";
  TransitionState["EXITED"] = "exited";
})(TransitionState || (TransitionState = {}));

var animationEndName = 'animationend';
var transitionEndName = 'transitionend';

var Transition = function Transition(props) {
  var _classnames;

  var visible = props.visible,
      tranisitionName = props.tranisitionName,
      duration = props.duration,
      forceRender = props.forceRender,
      destroy = props.destroy,
      children = props.children,
      _onEnter = props.onEnter,
      onEnterActive = props.onEnterActive,
      onEnterEnd = props.onEnterEnd,
      onLeave = props.onLeave,
      onLeaveActive = props.onLeaveActive,
      onLeaveEnd = props.onLeaveEnd;
  var nodeRef = React.useRef();

  var _React$useState = React.useState(TransitionState.UNMOUNTED),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      state = _React$useState2[0],
      setState = _React$useState2[1];

  var callbackRef = React.useRef(_utils.noop);
  var unmounted = TransitionState.UNMOUNTED === state;
  var enter = TransitionState.ENTER === state;
  var entering = TransitionState.ENTERING === state;
  var leave = TransitionState.EXIT === state;
  var leaving = TransitionState.EXITING === state;
  var exited = TransitionState.EXITED === state;
  var running = enter || entering || leave || leaving;
  var className = (0, _classnames2.default)((_classnames = {}, (0, _defineProperty2.default)(_classnames, "".concat(tranisitionName, "-enter"), enter || entering), (0, _defineProperty2.default)(_classnames, "".concat(tranisitionName, "-enter-active"), entering), (0, _defineProperty2.default)(_classnames, "".concat(tranisitionName, "-leave"), leave || leaving), (0, _defineProperty2.default)(_classnames, "".concat(tranisitionName, "-leave-active"), leaving), _classnames));
  var timeout = running && duration && duration > 0 ? "".concat(props.duration, "ms") : undefined;
  var style = {
    animationDuration: timeout,
    WebkitAnimationDuration: timeout,
    transitionDuration: timeout,
    WebkitTransitionDuration: timeout,
    display: (unmounted || exited) && !visible && !destroy ? 'none' : undefined
  };

  var setNodeRef = function setNodeRef(node) {
    nodeRef.current = node;
  };

  React.useImperativeHandle(props.nodeRef, function () {
    return nodeRef.current;
  });
  React.useEffect(function () {
    return function () {
      if (!nodeRef.current) return;

      _events.default.off(nodeRef.current, animationEndName, callbackRef.current);

      _events.default.off(nodeRef.current, transitionEndName, callbackRef.current);
    };
  }, []);
  return /*#__PURE__*/React.createElement(_reactTransitionGroup.Transition, {
    nodeRef: nodeRef,
    in: visible,
    addEndListener: function addEndListener(next) {
      if (!nodeRef.current) return;
      callbackRef.current = next;

      _events.default.on(nodeRef.current, animationEndName, callbackRef.current);

      _events.default.on(nodeRef.current, transitionEndName, callbackRef.current);
    },
    mountOnEnter: !forceRender,
    unmountOnExit: destroy,
    onEnter: function onEnter() {
      setState(TransitionState.ENTER);
      _onEnter === null || _onEnter === void 0 ? void 0 : _onEnter();
    },
    onEntering: function onEntering() {
      setState(TransitionState.ENTERING);
      onEnterActive === null || onEnterActive === void 0 ? void 0 : onEnterActive();
    },
    onEntered: function onEntered() {
      setState(TransitionState.ENTERED);
      onEnterEnd === null || onEnterEnd === void 0 ? void 0 : onEnterEnd();
    },
    onExit: function onExit() {
      setState(TransitionState.EXIT);
      onLeave === null || onLeave === void 0 ? void 0 : onLeave();
    },
    onExiting: function onExiting() {
      setState(TransitionState.EXITING);
      onLeaveActive === null || onLeaveActive === void 0 ? void 0 : onLeaveActive();
    },
    onExited: function onExited() {
      setState(TransitionState.EXITED);
      onLeaveEnd === null || onLeaveEnd === void 0 ? void 0 : onLeaveEnd();
    }
  }, function () {
    if ( /*#__PURE__*/React.isValidElement(children)) {
      return /*#__PURE__*/React.cloneElement(children, {
        ref: setNodeRef,
        className: (0, _classnames2.default)(children.props.className, className),
        style: _objectSpread(_objectSpread({}, children.props.style), style)
      });
    }

    return children === null || children === void 0 ? void 0 : children({
      className: className,
      style: style,
      visible: visible
    }, setNodeRef);
  });
};

var _default = Transition;
exports.default = _default;