"use strict";

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

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

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

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

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 _react = _interopRequireWildcard(require("react"));

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

var _omit = _interopRequireDefault(require("lodash/omit"));

var _animate = _interopRequireDefault(require("../animate"));

var _addEventListener = _interopRequireDefault(require("../_util/addEventListener"));

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

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

var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));

function getDefaultTarget() {
  return window;
}

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

  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.scrollToTop = function (e) {
      var _this$props = _this.props,
          _this$props$target = _this$props.target,
          target = _this$props$target === void 0 ? getDefaultTarget : _this$props$target,
          onClick = _this$props.onClick;
      (0, _scrollTo["default"])(0, {
        getContainer: target
      });

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

    _this.handleScroll = function () {
      var _this$props2 = _this.props,
          visibilityHeight = _this$props2.visibilityHeight,
          _this$props2$target = _this$props2.target,
          target = _this$props2$target === void 0 ? getDefaultTarget : _this$props2$target;
      var scrollTop = (0, _getScroll["default"])(target(), true);

      _this.setState({
        visible: scrollTop > visibilityHeight
      });
    };

    return _this;
  }

  (0, _createClass2["default"])(BackTop, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props$target2 = this.props.target,
          target = _this$props$target2 === void 0 ? getDefaultTarget : _this$props$target2;
      this.scrollEvent = (0, _addEventListener["default"])(target(), 'scroll', this.handleScroll);
      this.handleScroll();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.scrollEvent) {
        this.scrollEvent.remove();
      }
    }
  }, {
    key: "setScrollTop",
    value: function setScrollTop(value) {
      var _this$props$target3 = this.props.target,
          target = _this$props$target3 === void 0 ? getDefaultTarget : _this$props$target3;
      var targetNode = target();

      if (targetNode === window) {
        document.body.scrollTop = value;
        document.documentElement.scrollTop = value;
      } else {
        targetNode.scrollTop = value;
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          customizePrefixCls = _this$props3.prefixCls,
          _this$props3$classNam = _this$props3.className,
          className = _this$props3$classNam === void 0 ? '' : _this$props3$classNam,
          propsVisible = _this$props3.visible,
          children = _this$props3.children;
      var getPrefixCls = this.context.getPrefixCls;
      var stateVisible = this.state.visible;
      var prefixCls = getPrefixCls('back-top', customizePrefixCls);
      var classString = (0, _classnames["default"])(prefixCls, className);

      var defaultElement = /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-content")
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-icon")
      })); // fix https://fb.me/react-unknown-prop


      var divProps = (0, _omit["default"])(this.props, ['prefixCls', 'className', 'children', 'visibilityHeight', 'target', 'visible']);
      var visible = 'visible' in this.props ? propsVisible : stateVisible;
      var backTopBtn = visible ? /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({}, divProps, {
        className: classString,
        onClick: this.scrollToTop
      }), children || defaultElement) : null;
      return /*#__PURE__*/_react["default"].createElement(_animate["default"], {
        component: "",
        transitionName: "fade"
      }, backTopBtn);
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _ConfigContext["default"];
    }
  }]);
  return BackTop;
}(_react.PureComponent);

exports["default"] = BackTop;
BackTop.displayName = 'BackTop';
BackTop.defaultProps = {
  visibilityHeight: 400
};
//# sourceMappingURL=index.js.map
