import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";

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) { _defineProperty(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; }

import { useScroll } from '@use-gesture/react';
import { createBEM } from '@zarm-design/bem';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import Transition from '../transition';
import { canUseDOM, getMountContainer, getScrollContainer, getScrollTop, renderToContainer, scrollTo } from '../utils/dom';

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 = _slicedToArray(_React$useState, 2),
      visible = _React$useState2[0],
      setVisible = _React$useState2[1];

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

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

    return canUseDOM ? 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 canUseDOM ? 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 = getScrollTop(scrollContainer) || 0;
    var speed = props.speed || 0;
    var duration = speed <= 0 || speed === Infinity ? 0 : scrollTop / (speed / 10 * 1000);
    scrollTo(scrollContainer, 0, 0, duration);
  };

  useScroll(function (event) {
    var scrollTop = getScrollTop(event.target) || 0;
    setVisible(scrollTop > visibleDistance);
  }, {
    target: scrollContainer
  });
  React.useEffect(function () {
    var scrollTop = getScrollTop(scrollContainer);
    setVisible(scrollTop > visibleDistance);
  }, [scrollContainer]);
  return renderToContainer(mountContainer, /*#__PURE__*/React.createElement(Transition, {
    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
};
export default BackTop;