import * as _vue from "vue";

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; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { inject, Transition } from 'vue';
import classNames from '../_util/classNames';
import PropTypes from '../_util/vue-types';
import backTopTypes from './backTopTypes';
import addEventListener from '../vc-util/Dom/addEventListener';
import getScroll from '../_util/getScroll';
import BaseMixin from '../_util/BaseMixin';
import getTransitionProps from '../_util/getTransitionProps';
import { ConfigConsumerProps } from '../config-provider';
import scrollTo from '../_util/scrollTo';

function getDefaultTarget() {
  return window;
}

var props = backTopTypes();
var BackTop = {
  name: 'ABackTop',
  inheritAttrs: false,
  mixins: [BaseMixin],
  props: _objectSpread(_objectSpread({}, props), {}, {
    visibilityHeight: PropTypes.number.def(400)
  }),
  setup: function setup() {
    return {
      configProvider: inject('configProvider', ConfigConsumerProps)
    };
  },
  data: function data() {
    this.scrollEvent = null;
    return {
      visible: false
    };
  },
  mounted: function mounted() {
    var _this = this;

    this.$nextTick(function () {
      var getTarget = _this.target || getDefaultTarget;
      _this.scrollEvent = addEventListener(getTarget(), 'scroll', _this.handleScroll);

      _this.handleScroll();
    });
  },
  beforeUnmount: function beforeUnmount() {
    if (this.scrollEvent) {
      this.scrollEvent.remove();
    }
  },
  methods: {
    getCurrentScrollTop: function getCurrentScrollTop() {
      var getTarget = this.target || getDefaultTarget;
      var targetNode = getTarget();

      if (targetNode === window) {
        return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
      }

      return targetNode.scrollTop;
    },
    scrollToTop: function scrollToTop(e) {
      var _this$target = this.target,
          target = _this$target === void 0 ? getDefaultTarget : _this$target;
      scrollTo(0, {
        getContainer: target
      });
      this.$emit('click', e);
    },
    handleScroll: function handleScroll() {
      var visibilityHeight = this.visibilityHeight,
          _this$target2 = this.target,
          target = _this$target2 === void 0 ? getDefaultTarget : _this$target2;
      var scrollTop = getScroll(target(), true);
      this.setState({
        visible: scrollTop > visibilityHeight
      });
    }
  },
  render: function render() {
    var customizePrefixCls = this.prefixCls,
        $slots = this.$slots;
    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('back-top', customizePrefixCls);
    var classString = classNames(prefixCls, this.$attrs.class);

    var defaultElement = _vue.createVNode("div", {
      "class": "".concat(prefixCls, "-content")
    }, [_vue.createVNode("div", {
      "class": "".concat(prefixCls, "-icon")
    }, null)]);

    var divProps = _objectSpread(_objectSpread({}, this.$attrs), {}, {
      onClick: this.scrollToTop,
      class: classString
    });

    var backTopBtn = this.visible ? _vue.createVNode("div", divProps, [$slots.default && $slots.default() || defaultElement]) : null;
    var transitionProps = getTransitionProps('fade');
    return _vue.createVNode(Transition, transitionProps, {
      default: function _default() {
        return [backTopBtn];
      }
    });
  }
};
/* istanbul ignore next */

BackTop.install = function (app) {
  app.component(BackTop.name, BackTop);
};

export default BackTop;