import _extends from "@babel/runtime/helpers/esm/extends";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import React, { Component } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import Transition from 'react-transition-group/Transition';
export function getTransitionProps(props, options) {
  var timeout = props.timeout,
      _props$style = props.style,
      style = _props$style === void 0 ? {} : _props$style;
  return {
    duration: style.transitionDuration || typeof timeout === 'number' ? timeout : timeout[options.mode],
    delay: style.transitionDelay
  };
}

var Collapse =
/*#__PURE__*/
function (_Component) {
  _inherits(Collapse, _Component);

  function Collapse() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Collapse);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Collapse)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "getWrapper", function (el) {
      if (el) _this.wrapper = el;
    });

    _defineProperty(_assertThisInitialized(_this), "handleEnter", function (node) {
      node.style.height = _this.props.collapsedHeight;

      if (_this.props.onEnter) {
        _this.props.onEnter(node);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleEntering", function (node) {
      var _this$props = _this.props,
          timeout = _this$props.timeout,
          getAutoDuration = _this$props.getAutoDuration;
      var wrapperHeight = _this.wrapper ? _this.wrapper.clientHeight : 0;

      var _getTransitionProps = getTransitionProps(_this.props, {
        mode: 'enter'
      }),
          transitionDuration = _getTransitionProps.duration;

      if (timeout === 'auto') {
        var autoDuration = getAutoDuration(wrapperHeight);
        node.style.transitionDuration = "".concat(autoDuration, "ms");
        _this.autoTransitionDuration = autoDuration;
      } else {
        node.style.transitionDuration = typeof transitionDuration === 'string' ? transitionDuration : "".concat(transitionDuration, "ms");
      }

      node.style.height = "".concat(wrapperHeight, "px");

      if (_this.props.onEntering) {
        _this.props.onEntering(node);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleEntered", function (node) {
      node.style.height = 'auto';

      if (_this.props.onEntered) {
        _this.props.onEntered(node);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleExit", function (node) {
      var wrapperHeight = _this.wrapper ? _this.wrapper.clientHeight : 0;
      node.style.height = "".concat(wrapperHeight, "px");

      if (_this.props.onExit) {
        _this.props.onExit(node);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleExiting", function (node) {
      var _this$props2 = _this.props,
          timeout = _this$props2.timeout,
          getAutoDuration = _this$props2.getAutoDuration;
      var wrapperHeight = _this.wrapper ? _this.wrapper.clientHeight : 0;

      var _getTransitionProps2 = getTransitionProps(_this.props, {
        mode: 'exit'
      }),
          transitionDuration = _getTransitionProps2.duration;

      if (timeout === 'auto') {
        var autoDuration = getAutoDuration(wrapperHeight);
        node.style.transitionDuration = "".concat(autoDuration, "ms");
        _this.autoTransitionDuration = autoDuration;
      } else {
        node.style.transitionDuration = typeof transitionDuration === 'string' ? transitionDuration : "".concat(transitionDuration, "ms");
      }

      node.style.height = _this.props.collapsedHeight;

      if (_this.props.onExiting) {
        _this.props.onExiting(node);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "addEndListener", function (node, next) {
      if (_this.props.timeout === 'auto') {
        _this.timer = setTimeout(next, _this.autoTransitionDuration || 0);
      }
    });

    return _this;
  }

  _createClass(Collapse, [{
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      clearTimeout(this.timer);
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props3 = this.props,
          prefixCls = _this$props3.prefixCls,
          timeout = _this$props3.timeout,
          children = _this$props3.children,
          Wrapper = _this$props3.wrapper,
          wrapperProps = _this$props3.wrapperProps,
          className = _this$props3.className,
          collapsedHeight = _this$props3.collapsedHeight,
          style = _this$props3.style,
          other = _objectWithoutProperties(_this$props3, ["prefixCls", "timeout", "children", "wrapper", "wrapperProps", "className", "collapsedHeight", "style"]);

      return React.createElement(Transition, _extends({
        onEntering: this.handleEntering,
        onEnter: this.handleEnter,
        onEntered: this.handleEntered,
        onExiting: this.handleExiting,
        onExit: this.handleExit,
        addEndListener: this.addEndListener,
        timeout: timeout === 'auto' ? null : timeout
      }, other), function (state) {
        return React.createElement(Wrapper, _extends({
          className: classNames(prefixCls, _defineProperty({}, "".concat(prefixCls, "-entered"), state === 'entered'), className),
          style: _objectSpread({}, style, {
            minHeight: collapsedHeight
          })
        }, wrapperProps), React.createElement("div", {
          className: "".concat(prefixCls, "-wrapper-inner"),
          ref: _this2.getWrapper
        }, children));
      });
    }
  }]);

  return Collapse;
}(Component);

_defineProperty(Collapse, "propTypes", {
  /** 收缩后剩余高度, 如'5px', '10rem' */
  collapsedHeight: PropTypes.string,
  prefixCls: PropTypes.string,

  /** 是否收缩 */
  in: PropTypes.bool,

  /** 参考react-transition-group/Transition */
  onEnter: PropTypes.func,

  /** 参考react-transition-group/Transition */
  onEntered: PropTypes.func,

  /** 参考react-transition-group/Transition */
  onEntering: PropTypes.func,

  /** 参考react-transition-group/Transition */
  onExit: PropTypes.func,

  /** 参考react-transition-group/Transition */
  onExiting: PropTypes.func,

  /** 动画时间 */
  timeout: PropTypes.oneOfType([PropTypes.number, PropTypes.shape({
    enter: PropTypes.number,
    exit: PropTypes.number
  }), PropTypes.oneOf(['auto'])]),

  /** 动画时间计算方法 function(height: number) => time: number */
  getAutoDuration: PropTypes.func
});

_defineProperty(Collapse, "defaultProps", {
  wrapper: 'div',
  prefixCls: 'ne-collapse',
  timeout: 200,
  collapsedHeight: '0px',
  getAutoDuration: function getAutoDuration(height) {
    return height * 2;
  }
});

export { Collapse as default };