"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 _classnames = _interopRequireDefault(require("classnames"));

var _icon = _interopRequireDefault(require("../icon"));

var _scrollTo = require("./utils/scrollTo");

var _getScrollHeight = require("./utils/getScrollHeight");

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

var _rcMotion = _interopRequireDefault(require("../components/rc-motion"));

var _ReactNodeValidate = require("../utils/ReactNodeValidate");

var CircleFill = _icon.default.CircleFill,
    SquareFill = _icon.default.SquareFill;

var Backtop = function Backtop(props) {
  var visibilityHeight = props.visibilityHeight,
      className = props.className,
      children = props.children,
      toTopCallBack = props.toTopCallBack,
      onClick = props.onClick,
      bottom = props.bottom,
      target = props.target,
      right = props.right,
      shape = props.shape;

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      visible = _useState2[0],
      setVisible = _useState2[1];

  var _useContext = (0, _react.useContext)(_context.ConfigContext),
      getPrefixClassName = _useContext.getPrefixClassName;

  var backTopCls = getPrefixClassName('back-top'); // 样式整合

  var lastCls = (0, _classnames.default)(backTopCls, className, (0, _defineProperty2.default)({}, "".concat(backTopCls, "-").concat(shape), shape)); // 位置样式

  var positionSty = {
    right: right,
    bottom: bottom
  }; // 传入的是HTML 元素

  var ref = /*#__PURE__*/_react.default.createRef(); // 获取默认的target


  var getDefaultTarget = function getDefaultTarget() {
    var _ref$current;

    return (ref === null || ref === void 0 ? void 0 : (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.ownerDocument) ? ref.current.ownerDocument : window;
  }; // 滚动条滚动


  var handleScroll = function handleScroll(e) {
    var result = (0, _getScrollHeight.getScrollHeight)(e === null || e === void 0 ? void 0 : e.currentTarget);
    var scrollVisible = result > visibilityHeight;
    setVisible(scrollVisible);
  }; // 初始化滚动事件


  var initScrollEvent = function initScrollEvent(container) {
    container === null || container === void 0 ? void 0 : container.addEventListener('scroll', function (e) {
      handleScroll(e);
    });
    handleScroll({
      currentTarget: container
    });
  };

  (0, _react.useEffect)(function () {
    var getTarget = target || getDefaultTarget;
    var container = getTarget();
    initScrollEvent(container);
    return function () {
      container === null || container === void 0 ? void 0 : container.removeEventListener('scroll', function (e) {
        handleScroll(e);
      });
    };
  }, [target]); // 事件触发

  var handleScrollTop = function handleScrollTop(e) {
    (0, _scrollTo.scrollTo)({
      position: 0,
      getContainer: target || getDefaultTarget,
      callBack: toTopCallBack
    });

    if (typeof onClick === 'function') {
      onClick(e);
    }
  }; // 渲染children ---finished


  var renderChildrenDom = function renderChildrenDom() {
    var renderDefaultChild = shape === 'circle' ? /*#__PURE__*/_react.default.createElement(CircleFill, null) : /*#__PURE__*/_react.default.createElement(SquareFill, null);
    return /*#__PURE__*/_react.default.createElement(_rcMotion.default, {
      motionName: "fade",
      removeOnLeave: true
    }, function (_ref) {
      var motionClassName = _ref.className;
      var childNode = children || renderDefaultChild;
      return /*#__PURE__*/_react.default.createElement("div", null, (0, _ReactNodeValidate.cloneElement)(childNode, function (_ref2) {
        var cln = _ref2.cln;
        return {
          className: (0, _classnames.default)(motionClassName, cln, "".concat(backTopCls, "-child"))
        };
      }));
    });
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, visible && /*#__PURE__*/_react.default.createElement("div", {
    className: lastCls,
    onClick: handleScrollTop,
    style: positionSty,
    ref: ref
  }, renderChildrenDom()));
};

Backtop.displayName = 'Backtop';
Backtop.defaultProps = {
  bottom: 50,
  right: 50,
  shape: 'square',
  visibilityHeight: 100
};
var _default = Backtop;
exports.default = _default;