import { defineComponent, shallowRef, ref, computed, onMounted } from 'vue';
import { useThrottleFn, useEventListener } from '@vueuse/core';
import { ElIcon } from '../../icon/index.mjs';
import { easeInOutCubic } from '../../../utils/animation.mjs';
import { throwError } from '../../../utils/error.mjs';
import { CaretTop } from '@element-plus/icons-vue';
import { backtopProps, backtopEmits } from './backtop2.mjs';

const COMPONENT_NAME = "ElBacktop";
var script = defineComponent({
  name: COMPONENT_NAME,
  components: {
    ElIcon,
    CaretTop
  },
  props: backtopProps,
  emits: backtopEmits,
  setup(props, { emit }) {
    const el = shallowRef(document.documentElement);
    const container = shallowRef(document);
    const visible = ref(false);
    const styleBottom = computed(() => `${props.bottom}px`);
    const styleRight = computed(() => `${props.right}px`);
    const scrollToTop = () => {
      if (!el.value)
        return;
      const beginTime = Date.now();
      const beginValue = el.value.scrollTop;
      const frameFunc = () => {
        if (!el.value)
          return;
        const progress = (Date.now() - beginTime) / 500;
        if (progress < 1) {
          el.value.scrollTop = beginValue * (1 - easeInOutCubic(progress));
          requestAnimationFrame(frameFunc);
        } else {
          el.value.scrollTop = 0;
        }
      };
      requestAnimationFrame(frameFunc);
    };
    const handleScroll = () => {
      if (el.value)
        visible.value = el.value.scrollTop >= props.visibilityHeight;
    };
    const handleClick = (event) => {
      scrollToTop();
      emit("click", event);
    };
    const handleScrollThrottled = useThrottleFn(handleScroll, 300);
    onMounted(() => {
      var _a;
      if (props.target) {
        el.value = (_a = document.querySelector(props.target)) != null ? _a : void 0;
        if (!el.value) {
          throwError(COMPONENT_NAME, `target is not existed: ${props.target}`);
        }
        container.value = el.value;
      }
      useEventListener(container, "scroll", handleScrollThrottled);
    });
    return {
      visible,
      styleBottom,
      styleRight,
      handleClick
    };
  }
});

export { script as default };
//# sourceMappingURL=backtop.vue_vue_type_script_lang.mjs.map
