import { defineComponent, ref, reactive, watch, nextTick, onMounted, onActivated, onDeactivated, onBeforeUnmount, createVNode, Transition, withDirectives, mergeProps, vShow } from 'vue';
import { VerticalAlignTopOutlined } from '@ant-design/icons-vue';
import '../hooks/index.mjs';
import { getTransitionProps } from '../_util/components/transition.mjs';
import getScroll from '../_util/getScroll.mjs';
import scrollTo from '../_util/scrollTo.mjs';
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame.mjs';
import '../_util/props-util/index.mjs';
import { useInjectFloatButtonGroupContext } from './context.mjs';
import useStyle from './style/index.mjs';
import { backTopProps } from './interface.mjs';
import FloatButton, { floatButtonPrefixCls } from './FloatButton.mjs';
import initDefaultProps from '../_util/props-util/initDefaultProps.mjs';
import useConfigInject from '../config-provider/hooks/useConfigInject.mjs';

const BackTop = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ABackTop",
  inheritAttrs: false,
  props: initDefaultProps(backTopProps(), {
    visibilityHeight: 400,
    target: () => window,
    duration: 450,
    type: "default",
    shape: "circle"
  }),
  // emits: ['click'],
  setup(props, {
    slots,
    attrs,
    emit
  }) {
    const {
      prefixCls,
      direction
    } = useConfigInject(floatButtonPrefixCls, props);
    const [wrapSSR] = useStyle(prefixCls);
    const domRef = ref();
    const state = 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(0, {
        getContainer: target,
        duration
      });
      emit("click", e);
    };
    const handleScroll = throttleByAnimationFrame((e) => {
      const {
        visibilityHeight
      } = props;
      const scrollTop = 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);
    };
    watch(() => props.target, () => {
      scrollRemove();
      nextTick(() => {
        bindScrollEvent();
      });
    });
    onMounted(() => {
      nextTick(() => {
        bindScrollEvent();
      });
    });
    onActivated(() => {
      nextTick(() => {
        bindScrollEvent();
      });
    });
    onDeactivated(() => {
      scrollRemove();
    });
    onBeforeUnmount(() => {
      scrollRemove();
    });
    const floatButtonGroupContext = useInjectFloatButtonGroupContext();
    return () => {
      const defaultElement = createVNode("div", {
        "class": `${prefixCls.value}-content`
      }, [createVNode("div", {
        "class": `${prefixCls.value}-icon`
      }, [createVNode(VerticalAlignTopOutlined, null, null)])]);
      const floatButtonProps = {
        ...attrs,
        shape: (floatButtonGroupContext == null ? void 0 : floatButtonGroupContext.shape.value) || props.shape,
        onClick: scrollToTop,
        class: {
          [`${prefixCls.value}`]: true,
          [`${attrs.class}`]: attrs.class,
          [`${prefixCls.value}-rtl`]: direction.value === "rtl"
        }
      };
      const transitionProps = getTransitionProps("fade");
      return wrapSSR(createVNode(Transition, transitionProps, {
        default: () => [withDirectives(createVNode(FloatButton, mergeProps(floatButtonProps, {
          "ref": domRef
        }), {
          icon: () => createVNode(VerticalAlignTopOutlined, null, null),
          default: () => {
            var _a;
            return ((_a = slots.default) == null ? void 0 : _a.call(slots)) || defaultElement;
          }
        }), [[vShow, state.visible]])]
      }));
    };
  }
});

export { BackTop as default };
//# sourceMappingURL=BackTop.mjs.map
