"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

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

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var Transition = /*#__PURE__*/function (_PureComponent) {
  _inherits(Transition, _PureComponent);

  var _super = _createSuper(Transition);

  function Transition(props) {
    var _this;

    _classCallCheck(this, Transition);

    _this = _super.call(this, props);

    _initialiseProps.call(_assertThisInitialized(_this));

    _this.state = {
      visible: false,
      classes: ""
    };
    return _this;
  } // 过渡时间不传入默认为0


  _createClass(Transition, [{
    key: "componentWillMount",
    value: function componentWillMount() {
      var _this$props = this.props,
          transitionName = _this$props.transitionName,
          animate = _this$props.animate,
          visible = _this$props.visible;

      if (!animate) {
        this.setState({
          visible: visible
        });
        return;
      }

      this._handlerAppearAnimate(this.props, transitionName);
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(props) {
      var transitionName = props.transitionName,
          animate = props.animate,
          visible = props.visible;

      if (!animate) {
        this.setState({
          visible: visible
        });
        return;
      }

      if (visible) {
        this._handlerEnterAnimate(props, transitionName);
      } else {
        this._handlerLeaveAnimate(props, transitionName);
      }
    } // 首次渲染的入场动画

  }, {
    key: "getClasses",
    value: // 类名统一配置
    function getClasses(type, transitionName) {
      var _classnames, _classnames2, _classnames3;

      var initClasses = (0, _classnames4["default"])((_classnames = {}, _defineProperty(_classnames, "".concat(transitionName, "-appear"), type === "appear"), _defineProperty(_classnames, "".concat(transitionName, "-enter"), type === "enter"), _defineProperty(_classnames, "".concat(transitionName, "-leave"), type === "leave"), _classnames));
      var activeClasses = (0, _classnames4["default"])((_classnames2 = {}, _defineProperty(_classnames2, "".concat(transitionName, "-appear-active"), type === "appear"), _defineProperty(_classnames2, "".concat(transitionName, "-enter-active"), type === "enter"), _defineProperty(_classnames2, "".concat(transitionName, "-leave-active"), type === "leave"), _classnames2));
      var endClasses = (0, _classnames4["default"])((_classnames3 = {}, _defineProperty(_classnames3, "".concat(transitionName, "-appear-end"), type === "appear"), _defineProperty(_classnames3, "".concat(transitionName, "-enter-end"), type === "enter"), _defineProperty(_classnames3, "".concat(transitionName, "-leave-end"), type === "leave"), _classnames3));
      return {
        initClasses: initClasses,
        activeClasses: activeClasses,
        endClasses: endClasses
      };
    }
  }, {
    key: "cloneChildren",
    value: function cloneChildren() {
      var classes = this.state.classes;
      var children = this.props.children; // @ts-ignore

      var className = children.props.className; // 通过React.cloneElement给子元素添加额外的props，

      return /*#__PURE__*/_react["default"].cloneElement( // @ts-ignore
      children, {
        className: "".concat(className, " ").concat(classes)
      });
    }
  }, {
    key: "render",
    value: function render() {
      var visible = this.state.visible;
      return visible && this.cloneChildren();
    }
  }]);

  return Transition;
}(_react.PureComponent);

exports["default"] = Transition;
Transition.defaultProps = {
  animate: true,
  visible: false,
  transitionName: "",
  appearTimeout: 0,
  appearActiveTimeout: 0,
  appearEndTimeout: 0,
  enterTimeout: 0,
  enterActiveTimeout: 0,
  enterEndTimeout: 0,
  leaveTimeout: 0,
  leaveEndTimeout: 0,
  leaveActiveTimeout: 0
};

var _initialiseProps = function _initialiseProps() {
  var _this2 = this;

  this._handlerAppearAnimate = function (props, transitionName) {
    var visible = props.visible,
        appearTimeout = props.appearTimeout,
        appearActiveTimeout = props.appearActiveTimeout,
        appearEndTimeout = props.appearEndTimeout;

    var _this2$getClasses = _this2.getClasses("appear", transitionName),
        initClasses = _this2$getClasses.initClasses,
        activeClasses = _this2$getClasses.activeClasses,
        endClasses = _this2$getClasses.endClasses;

    _this2.setState({
      visible: visible,
      classes: initClasses
    });

    setTimeout(function () {
      _this2.setState({
        classes: activeClasses
      });
    }, appearTimeout);
    setTimeout(function () {
      _this2.setState({
        classes: endClasses
      });
    }, appearActiveTimeout + appearTimeout);
    setTimeout(function () {
      _this2.setState({
        classes: ""
      });
    }, appearEndTimeout + appearActiveTimeout + appearTimeout);
  };

  this._handlerEnterAnimate = function (props, transitionName) {
    var visible = props.visible,
        enterTimeout = props.enterTimeout,
        enterActiveTimeout = props.enterActiveTimeout,
        enterEndTimeout = props.enterEndTimeout;

    var _this2$getClasses2 = _this2.getClasses("enter", transitionName),
        initClasses = _this2$getClasses2.initClasses,
        activeClasses = _this2$getClasses2.activeClasses,
        endClasses = _this2$getClasses2.endClasses;

    _this2.setState({
      visible: visible,
      classes: initClasses
    });

    var enterTimer = setTimeout(function () {
      _this2.setState({
        classes: activeClasses
      });

      clearTimeout(enterTimer);
    }, enterTimeout);
    var enterActiveTimer = setTimeout(function () {
      _this2.setState({
        classes: endClasses
      });

      clearTimeout(enterActiveTimer);
    }, enterActiveTimeout + enterTimeout);
    var enterEndTimer = setTimeout(function () {
      _this2.setState({
        classes: ""
      });

      clearTimeout(enterEndTimer);
    }, enterEndTimeout + enterActiveTimeout + enterTimeout);
  };

  this._handlerLeaveAnimate = function (props, transitionName) {
    var visible = props.visible,
        leaveTimeout = props.leaveTimeout,
        leaveActiveTimeout = props.leaveActiveTimeout,
        leaveEndTimeout = props.leaveEndTimeout;

    var _this2$getClasses3 = _this2.getClasses("leave", transitionName),
        initClasses = _this2$getClasses3.initClasses,
        activeClasses = _this2$getClasses3.activeClasses,
        endClasses = _this2$getClasses3.endClasses;

    _this2.setState({
      classes: initClasses
    });

    var leaveTimer = setTimeout(function () {
      _this2.setState({
        classes: activeClasses
      });

      clearTimeout(leaveTimer);
    }, leaveTimeout);
    var leaveActiveTimer = setTimeout(function () {
      _this2.setState({
        classes: endClasses
      });

      clearTimeout(leaveActiveTimer);
    }, leaveActiveTimeout + leaveTimeout);
    var leaveEndTimer = setTimeout(function () {
      _this2.setState({
        visible: visible,
        classes: ""
      });

      clearTimeout(leaveEndTimer);
    }, leaveEndTimeout + leaveActiveTimeout + leaveTimeout);
  };
};