"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 = require("@use-gesture/react");

var _bem = require("@zarm-design/bem");

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

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

var _transition = _interopRequireDefault(require("../transition"));

var _dom = require("../utils/dom");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var BackTop = function BackTop(props) {
  var className = props.className,
      style = props.style,
      visibleDistance = props.visibleDistance,
      destroy = props.destroy,
      onClick = props.onClick,
      children = props.children;

  var _React$useState = React.useState(false),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      visible = _React$useState2[0],
      setVisible = _React$useState2[1];

  var _React$useContext = React.useContext(_configProvider.ConfigContext),
      prefixCls = _React$useContext.prefixCls,
      globalMountContainer = _React$useContext.mountContainer,
      globalScrollContainer = _React$useContext.scrollContainer;

  var bem = (0, _bem.createBEM)('back-top', {
    prefixCls: prefixCls
  });
  var scrollContainer = React.useMemo(function () {
    var _props$scrollContaine;

    return _dom.canUseDOM ? (0, _dom.getScrollContainer)((_props$scrollContaine = props.scrollContainer) !== null && _props$scrollContaine !== void 0 ? _props$scrollContaine : globalScrollContainer) : undefined;
  }, [props.scrollContainer, globalScrollContainer]);
  var mountContainer = React.useMemo(function () {
    var _props$mountContainer;

    return _dom.canUseDOM ? (0, _dom.getMountContainer)((_props$mountContainer = props.mountContainer) !== null && _props$mountContainer !== void 0 ? _props$mountContainer : globalMountContainer) : undefined;
  }, [props.mountContainer, globalMountContainer]);

  var scrollToTop = function scrollToTop(event) {
    onClick === null || onClick === void 0 ? void 0 : onClick(event);
    if (!scrollContainer) return;
    var scrollTop = (0, _dom.getScrollTop)(scrollContainer) || 0;
    var speed = props.speed || 0;
    var duration = speed <= 0 || speed === Infinity ? 0 : scrollTop / (speed / 10 * 1000);
    (0, _dom.scrollTo)(scrollContainer, 0, 0, duration);
  };

  (0, _react.useScroll)(function (event) {
    var scrollTop = (0, _dom.getScrollTop)(event.target) || 0;
    setVisible(scrollTop > visibleDistance);
  }, {
    target: scrollContainer
  });
  React.useEffect(function () {
    var scrollTop = (0, _dom.getScrollTop)(scrollContainer);
    setVisible(scrollTop > visibleDistance);
  }, [scrollContainer]);
  return (0, _dom.renderToContainer)(mountContainer, /*#__PURE__*/React.createElement(_transition.default, {
    visible: visible,
    duration: props.duration,
    destroy: destroy,
    tranisitionName: "".concat(prefixCls, "-fade")
  }, /*#__PURE__*/React.createElement("div", {
    className: bem([className]),
    style: _objectSpread({
      position: mountContainer !== document.body ? 'absolute' : 'fixed'
    }, style),
    onClick: scrollToTop
  }, children)));
};

BackTop.defaultProps = {
  speed: 100,
  visibleDistance: 400,
  destroy: true
};
var _default = BackTop;
exports.default = _default;