"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 _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

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

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

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

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

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

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

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

var _rcAnimate = _interopRequireDefault(require("rc-animate"));

var _addEventListener = _interopRequireDefault(require("rc-util/lib/Dom/addEventListener"));

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

var _omit = _interopRequireDefault(require("omit.js"));

var _throttleByAnimationFrame = require("../_util/throttleByAnimationFrame");

var _configProvider = require("../config-provider");

var _getScroll = _interopRequireDefault(require("../_util/getScroll"));

var _scrollTo = _interopRequireDefault(require("../_util/scrollTo"));

var __decorate = void 0 && (void 0).__decorate || function (decorators, target, key, desc) {
  var c = arguments.length,
      r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
      d;
  if ((typeof Reflect === "undefined" ? "undefined" : (0, _typeof2["default"])(Reflect)) === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);else for (var i = decorators.length - 1; i >= 0; i--) {
    if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  }
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};

var BackTop = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(BackTop, _React$Component);

  var _super = (0, _createSuper2["default"])(BackTop);

  function BackTop() {
    var _this;

    (0, _classCallCheck2["default"])(this, BackTop);
    _this = _super.apply(this, arguments);
    _this.state = {
      visible: false
    };

    _this.getDefaultTarget = function () {
      return _this.node && _this.node.ownerDocument ? _this.node.ownerDocument : window;
    };

    _this.saveDivRef = function (node) {
      _this.node = node;
    };

    _this.scrollToTop = function (e) {
      var _this$props = _this.props,
          onClick = _this$props.onClick,
          target = _this$props.target;
      (0, _scrollTo["default"])(0, {
        getContainer: target || _this.getDefaultTarget
      });

      if (typeof onClick === 'function') {
        onClick(e);
      }
    };

    _this.renderBackTop = function (_ref) {
      var getPrefixCls = _ref.getPrefixCls,
          direction = _ref.direction;
      var _this$props2 = _this.props,
          customizePrefixCls = _this$props2.prefixCls,
          _this$props2$classNam = _this$props2.className,
          className = _this$props2$classNam === void 0 ? '' : _this$props2$classNam;
      var prefixCls = getPrefixCls('back-top', customizePrefixCls);
      var classString = (0, _classnames["default"])(prefixCls, className, (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-rtl"), direction === 'rtl')); // fix https://fb.me/react-unknown-prop

      var divProps = (0, _omit["default"])(_this.props, ['prefixCls', 'className', 'children', 'visibilityHeight', 'target', 'visible']);
      return /*#__PURE__*/React.createElement("div", (0, _extends2["default"])({}, divProps, {
        className: classString,
        onClick: _this.scrollToTop,
        ref: _this.saveDivRef
      }), _this.renderChildren({
        prefixCls: prefixCls
      }));
    };

    return _this;
  }

  (0, _createClass2["default"])(BackTop, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.bindScrollEvent();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var target = this.props.target;

      if (prevProps.target !== target) {
        this.bindScrollEvent();
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.scrollEvent) {
        this.scrollEvent.remove();
      }

      this.handleScroll.cancel();
    }
  }, {
    key: "bindScrollEvent",
    value: function bindScrollEvent() {
      var _this2 = this;

      if (this.scrollEvent) {
        this.scrollEvent.remove();
      }

      var target = this.props.target;
      var getTarget = target || this.getDefaultTarget;
      var container = getTarget();
      this.scrollEvent = (0, _addEventListener["default"])(container, 'scroll', function (e) {
        _this2.handleScroll(e);
      });
      this.handleScroll({
        target: container
      });
    }
  }, {
    key: "getVisible",
    value: function getVisible() {
      if ('visible' in this.props) {
        return this.props.visible;
      }

      return this.state.visible;
    }
  }, {
    key: "handleScroll",
    value: function handleScroll(e) {
      var _this$props$visibilit = this.props.visibilityHeight,
          visibilityHeight = _this$props$visibilit === void 0 ? 0 : _this$props$visibilit;
      var scrollTop = (0, _getScroll["default"])(e.target, true);
      this.setState({
        visible: scrollTop > visibilityHeight
      });
    }
  }, {
    key: "renderChildren",
    value: function renderChildren(_ref2) {
      var prefixCls = _ref2.prefixCls;
      var children = this.props.children;
      var defaultElement = /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-content")
      }, /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-icon")
      }));
      return /*#__PURE__*/React.createElement(_rcAnimate["default"], {
        component: "",
        transitionName: "fade"
      }, this.getVisible() ? /*#__PURE__*/React.createElement("div", null, children || defaultElement) : null);
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement(_configProvider.ConfigConsumer, null, this.renderBackTop);
    }
  }]);
  return BackTop;
}(React.Component);

exports["default"] = BackTop;
BackTop.defaultProps = {
  visibilityHeight: 400
};

__decorate([(0, _throttleByAnimationFrame.throttleByAnimationFrameDecorator)()], BackTop.prototype, "handleScroll", null);