'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var iconsVue = require('@ant-design/icons-vue');
require('../../../utils/index.cjs');
require('../../../vue-components/index.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
var index = require('../style/index.cjs');
var context = require('./context.cjs');
var props = require('./props.cjs');
var FloatButton = require('./FloatButton.cjs');
var constants = require('./constants.cjs');
var initDefaultProps = require('../../../utils/vue/initDefaultProps.cjs');
var scrollTo = require('../../../utils/scroll/scrollTo.cjs');
var throttleByAnimationFrame = require('../../../utils/throttleByAnimationFrame.cjs');
var getScroll = require('../../../utils/scroll/getScroll.cjs');
var transition = require('../../../vue-components/transition/src/transition.cjs');

var backTop = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ABackTop",
  inheritAttrs: false,
  props: initDefaultProps.initDefaultProps(props.backTopProps(), {
    visibilityHeight: 400,
    target: () => window,
    duration: 450,
    type: "default",
    shape: "circle"
  }),
  // emits: ['click'],
  setup(props, {
    slots,
    attrs,
    emit
  }) {
    const {
      prefixCls,
      direction
    } = useConfigInject.default(constants.floatButtonPrefixCls, props);
    const [wrapSSR] = index.default(prefixCls);
    const domRef = vue.ref();
    const state = vue.reactive({
      visible: props.visibilityHeight === 0,
      scrollEvent: null
    });
    const getDefaultTarget = () => domRef.value && domRef.value.ownerDocument ? domRef.value.ownerDocument : window;
    const scrollToTop = (e) => {
      const {
        target = getDefaultTarget,
        duration
      } = props;
      scrollTo.scrollTo(0, {
        getContainer: target,
        duration
      });
      emit("click", e);
    };
    const handleScroll = throttleByAnimationFrame.throttleByAnimationFrame((e) => {
      const {
        visibilityHeight
      } = props;
      const scrollTop = getScroll.getScroll(e.target, true);
      state.visible = scrollTop >= visibilityHeight;
    });
    const bindScrollEvent = () => {
      const {
        target
      } = props;
      const getTarget = target || getDefaultTarget;
      const container = getTarget();
      handleScroll({
        target: container
      });
      container == null ? void 0 : container.addEventListener("scroll", handleScroll);
    };
    const scrollRemove = () => {
      const {
        target
      } = props;
      const getTarget = target || getDefaultTarget;
      const container = getTarget();
      handleScroll.cancel();
      container == null ? void 0 : container.removeEventListener("scroll", handleScroll);
    };
    vue.watch(() => props.target, () => {
      scrollRemove();
      vue.nextTick(() => {
        bindScrollEvent();
      });
    });
    vue.onMounted(() => {
      vue.nextTick(() => {
        bindScrollEvent();
      });
    });
    vue.onActivated(() => {
      vue.nextTick(() => {
        bindScrollEvent();
      });
    });
    vue.onDeactivated(() => {
      scrollRemove();
    });
    vue.onBeforeUnmount(() => {
      scrollRemove();
    });
    const floatButtonGroupContext = context.useInjectFloatButtonGroupContext();
    return () => {
      const {
        description,
        type,
        shape,
        tooltip,
        badge
      } = props;
      const floatButtonProps = {
        ...attrs,
        shape: (floatButtonGroupContext == null ? void 0 : floatButtonGroupContext.shape.value) || shape,
        onClick: scrollToTop,
        class: {
          [`${prefixCls.value}`]: true,
          [`${attrs.class}`]: attrs.class,
          [`${prefixCls.value}-rtl`]: direction.value === "rtl"
        },
        description,
        type,
        tooltip,
        badge
      };
      const transitionProps = transition.getTransitionProps("fade");
      return wrapSSR(vue.createVNode(vue.Transition, transitionProps, {
        default: () => [vue.withDirectives(vue.createVNode(FloatButton.default, {
          ...floatButtonProps,
          "ref": domRef
        }, {
          icon: () => {
            var _a;
            return ((_a = slots.icon) == null ? void 0 : _a.call(slots)) || vue.createVNode(iconsVue.VerticalAlignTopOutlined, null, null);
          }
        }), [[vue.vShow, state.visible]])]
      }));
    };
  }
});

exports.default = backTop;
//# sourceMappingURL=BackTop.cjs.map
